// Create and display double linked list
#include <stdio.h>
#include <malloc.h>
struct dlist {
struct dlist *prev;
int data;
struct dlist *next;
} *start = NULL;
typedef struct dlist node;
void create(void);
void display(void);
int main() {
int ch;
while (True) {
printf("\n1 -> Create");
printf("\n2 -> Display");
printf("\n3 -> Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
if (ch == 1)
create();
else if (ch == 2)
display();
else if (ch == 3)
break;
else
printf("\nInvalid Choice");
return 0;
void create(void) {
node *tmp, *tmp1;
tmp = (node *)malloc(sizeof(node));
printf("\nEnter value for new node: ");
scanf("%d", &tmp->data);
if (start == NULL) {
tmp->next = NULL;
tmp->prev = NULL;
start = tmp;
} else {
tmp->next = NULL;
tmp1 = start;
while (tmp1->next != NULL)
tmp1 = tmp1->next;
tmp1->next = tmp;
tmp->prev = tmp1;
printf("\nElement added to DLL");
void display(void) {
node *tmp;
tmp = start;
if (tmp == NULL)
printf("\nEmpty");
else {
while (tmp != NULL) {
printf("\n%d", tmp->data);
tmp = tmp->next;
Output Example:
10
15
# Insertion at the beginning in a doubly linked list
Prototype: void finsert(void);
Call: finsert();
Definition:
void finsert(void)
node *tmp;
tmp = (node *)malloc(sizeof(node));
printf("\nEnter value for New Node: ");
scanf("%d", &tmp->data);
if (start == NULL)
tmp->next = NULL;
tmp->prev = NULL;
start = tmp;
else
tmp->prev = NULL;
tmp->next = start;
start->prev = tmp;
start = tmp;
printf("\nFirst Insert Successful...");
# Insertion at the last in a doubly linked list
Prototype: void linsert();
Call: linsert();
void linsert()
node *tmp, *tmp1;
tmp = (node *)malloc(sizeof(node));
printf("\nEnter Value for New Node:");
scanf("%d", &tmp->data);
tmp->next = NULL;
tmp1 = start;
if (tmp1 == NULL)
start = tmp;
}
else
while (tmp1->next != NULL)
tmp1 = tmp1->next;
tmp->prev = tmp1;
tmp1->next = tmp;
printf("\nLast Insert Successful...");
# Insertion at the any position in a doubly linked list
void apinsert()
node *tmp, *tmp1;
int pos, Count = 0, i=1;
tmp = (node *)malloc(sizeof(node));
printf("\nEnter Value for New Node: ");
scanf("%d", &tmp->data);
printf("\nEnter Position to Insert: ");
scanf("%d", &pos);
tmp1 = start;
while (tmp1 != NULL)
Count++;
tmp1 = tmp1->next;
}
if (pos < 1 || pos > Count + 1)
printf("\nInvalid Position");
else if (pos == 1)
tmp->prev = NULL;
tmp->next = start;
start->prev = tmp;
start = tmp;
printf("\nInsertion Successful...");
else if (pos == Count + 1)
tmp1 = start;
while (tmp1->next != NULL)
tmp1 = tmp1->next;
tmp->next = NULL;
tmp->prev = tmp1;
tmp1->next = tmp;
printf("\nInsertion Successful...");
else
tmp1 = start;
while (i < pos - 1)
tmp1 = tmp1->next;
i++;
tmp->next = tmp1->next;
tmp->next->prev = tmp;
tmp1->next = tmp;
tmp->prev = tmp1;
printf("\nInsertion Successful...");
# Delete the first node from a doubly linked list.
prototype: void fdelete();
call: fdelete();
definition:
void fdelete()
node *tmp;
tmp = start->next;
if(start == NULL)
printf("\nEmpty Linked List:");
else
tmp->prev = NULL;
free(start);
start = tmp;
printf("\nFirst Position Delete Successful...");
# Delete the last node in a doubly linked list.
prototype: void ldelete();
call: ldelete();
definition:
void ldelete()
node *tmp, *tmp1;
tmp = start;
if(start == NULL)
printf("\nEmpty Linked List:");
else
while(tmp->next->next != NULL)
tmp = tmp->next;
tmp1 = tmp->next;
tmp->next = NULL;
free(tmp1);
printf("\nLast Position Delete Successful...");
# Delete a node from any position in a doubly linked list
Prototype: void adelete(void);
Calling: adelete();
Definition:
void adelete()
node *tmp, *tmp1;
int Count = 0, pos, i = 1;
tmp = start;
while(tmp != NULL)
Count++;
tmp = tmp->next;
printf("\nEnter Position: ");
scanf("%d", &pos);
if(pos < 1 || pos > Count)
printf("\nInvalid Position");
else if(pos == 1)
tmp = start;
tmp1 = tmp->next;
tmp1->prev = NULL;
start = tmp1;
free(tmp);
printf("\nDeletion done...");
}
else if(pos == Count)
tmp = start;
while(tmp->next->next != NULL)
tmp = tmp->next;
tmp1 = tmp->next;
tmp->next = NULL;
free(tmp1);
printf("\nDeletion done!");
else
tmp = start;
while(i < pos - 1)
tmp = tmp->next;
i++;
tmp1 = tmp->next;
tmp->next = tmp1->next;
tmp1->next->prev = tmp;
free(tmp1);
printf("\nDeletion Successful...");
Combined all the Function:
##########################Doubly Linked List################################
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *prev, *next;
} Node;
Node *start = NULL;
// Function prototypes
void create();
void finsert();
void linsert();
void ainsert();
void fdelete();
void ldelete();
void adelete();
void displayForward();
void displayBackward();
void count();
void search();
void sort();
int main() {
int choice;
while (1) {
printf("\n\n--- Doubly Linked List Menu ---\n");
printf("1. Create List\n");
printf("2. Insert at First\n");
printf("3. Insert at Last\n");
printf("4. Insert at Any Position\n");
printf("5. Delete from First\n");
printf("6. Delete from Last\n");
printf("7. Delete from Any Position\n");
printf("8. Display Forward\n");
printf("9. Display Backward\n");
printf("10. Count Nodes\n");
printf("11. Search\n");
printf("12. Sort\n");
printf("13. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: create(); break;
case 2: finsert(); break;
case 3: linsert(); break;
case 4: ainsert(); break;
case 5: fdelete(); break;
case 6: ldelete(); break;
case 7: adelete(); break;
case 8: displayForward(); break;
case 9: displayBackward(); break;
case 10: count(); break;
case 11: search(); break;
case 12: sort(); break;
case 13: exit(0);
default: printf("Invalid Choice!\n");
return 0;
// 1. Create List
void create() {
int n, i, data;
Node *newNode, *last;
printf("Enter number of nodes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
newNode = (Node*)malloc(sizeof(Node));
printf("Enter data for node %d: ", i + 1);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
if (start == NULL) {
start = newNode;
last = newNode;
} else {
last->next = newNode;
newNode->prev = last;
last = newNode;
printf("List created successfully!\n");
// 2. Insert at First
void finsert() {
Node *newNode;
int data;
newNode = (Node*)malloc(sizeof(Node));
printf("Enter data: ");
scanf("%d", &data);
newNode->data = data;
newNode->prev = NULL;
newNode->next = start;
if (start != NULL)
start->prev = newNode;
start = newNode;
printf("Inserted at first successfully!\n");
// 3. Insert at Last
void linsert() {
Node *newNode, *temp;
int data;
newNode = (Node*)malloc(sizeof(Node));
printf("Enter data: ");
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
if (start == NULL) {
newNode->prev = NULL;
start = newNode;
} else {
temp = start;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
printf("Inserted at last successfully!\n");
// 4. Insert at Any Position
void ainsert() {
int pos, i, data;
Node *newNode, *temp;
printf("Enter position: ");
scanf("%d", &pos);
printf("Enter data: ");
scanf("%d", &data);
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
if (pos == 1) {
newNode->prev = NULL;
newNode->next = start;
if (start != NULL)
start->prev = newNode;
start = newNode;
} else {
temp = start;
for (i = 1; i < pos - 1 && temp != NULL; i++)
temp = temp->next;
if (temp == NULL) {
printf("Invalid Position!\n");
free(newNode);
return;
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
printf("Inserted successfully!\n");
// 5. Delete from First
void fdelete() {
Node *temp;
if (start == NULL) {
printf("List is empty!\n");
return;
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
printf("First node deleted successfully!\n");
// 6. Delete from Last
void ldelete() {
Node *temp;
if (start == NULL) {
printf("List is empty!\n");
return;
temp = start;
if (temp->next == NULL) {
start = NULL;
free(temp);
} else {
while (temp->next != NULL)
temp = temp->next;
temp->prev->next = NULL;
free(temp);
}
printf("Last node deleted successfully!\n");
// 7. Delete from Any Position
void adelete() {
Node *temp;
int pos, i;
if (start == NULL) {
printf("List is empty!\n");
return;
printf("Enter position: ");
scanf("%d", &pos);
temp = start;
if (pos == 1) {
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
} else {
for (i = 1; i < pos && temp != NULL; i++)
temp = temp->next;
if (temp == NULL) {
printf("Invalid Position!\n");
return;
}
if (temp->next != NULL)
temp->next->prev = temp->prev;
if (temp->prev != NULL)
temp->prev->next = temp->next;
free(temp);
printf("Node deleted successfully!\n");
// 8. Display Forward
void displayForward() {
Node *temp = start;
if (start == NULL) {
printf("List is empty!\n");
return;
printf("List (Forward): ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
printf("\n");
// 9. Display Backward
void displayBackward() {
Node *temp = start;
if (start == NULL) {
printf("List is empty!\n");
return;
while (temp->next != NULL)
temp = temp->next;
printf("List (Backward): ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->prev;
printf("\n");
// 10. Count Nodes
void count() {
int cnt = 0;
Node *temp = start;
while (temp != NULL) {
cnt++;
temp = temp->next;
printf("Total nodes: %d\n", cnt);
// 11. Search
void search() {
int key, pos = 1, found = 0;
Node *temp = start;
printf("Enter value to search: ");
scanf("%d", &key);
while (temp != NULL) {
if (temp->data == key) {
printf("Value %d found at position %d\n", key, pos);
found = 1;
break;
pos++;
temp = temp->next;
if (!found)
printf("Value %d not found in the list!\n", key);
// 12. Sort
void sort() {
Node *i, *j;
int tempData;
if (start == NULL) {
printf("List is empty!\n");
return;
for (i = start; i->next != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {
tempData = i->data;
i->data = j->data;
j->data = tempData;
printf("List sorted successfully!\n");
Algorithms for Doubly Linked List
1. Create List (create())
1. Start
2. Input number of nodes n
3. Repeat for i = 1 to n:
o Allocate memory for a new node
o Input data
o If list is empty → set start = newNode
o Else → link last->next = newNode and newNode->prev = last
o Move last = newNode
4. Stop
2. Insert at First (finsert())
1. Start
2. Allocate memory for new node
3. Input data
4. Set newNode->next = start, newNode->prev = NULL
5. If list not empty → set start->prev = newNode
6. Update start = newNode
7. Stop
3. Insert at Last (linsert())
1. Start
2. Allocate memory for new node
3. Input data
4. Set newNode->next = NULL
5. If list is empty → set newNode->prev = NULL and start = newNode
6. Else → traverse till last node
o Set last->next = newNode and newNode->prev = last
7. Stop
4. Insert at Any Position (ainsert())
1. Start
2. Input pos and data
3. Allocate memory for new node
4. If pos == 1 → insert at beginning (same as finsert)
5. Else → traverse till position pos-1
6. If position invalid → print error and stop
7. Adjust links:
o newNode->next = temp->next
o newNode->prev = temp
o If temp->next != NULL → temp->next->prev = newNode
o temp->next = newNode
8. Stop
5. Delete from First (fdelete())
1. Start
2. If list empty → print message and stop
3. Set temp = start
4. Move start = start->next
5. If start != NULL → set start->prev = NULL
6. Free temp
7. Stop
6. Delete from Last (ldelete())
1. Start
2. If list empty → print message and stop
3. If only one node → free node, set start = NULL
4. Else → traverse to last node
o Set last->prev->next = NULL
o Free last node
5. Stop
7. Delete from Any Position (adelete())
1. Start
2. If list empty → print message and stop
3. Input pos
4. Traverse to node at position pos
5. If node not found → print error and stop
6. Adjust links:
o If temp->next != NULL → temp->next->prev = temp->prev
o If temp->prev != NULL → temp->prev->next = temp->next
7. Free node
8. Stop
8. Display Forward (displayForward())
1. Start
2. If list empty → print message and stop
3. Traverse from start to end:
o Print data of each node
4. Stop
9. Display Backward (displayBackward())
1. Start
2. If list empty → print message and stop
3. Traverse to last node
4. While traversing backward (node = node->prev):
o Print data
5. Stop
10. Count Nodes (count())
1. Start
2. Initialize cnt = 0
3. Traverse from start to end:
o Increment cnt for each node
4. Print cnt
5. Stop
11. Search (search())
1. Start
2. Input key
3. Initialize pos = 1
4. Traverse from start:
o If node->data == key → print "Found at position pos" and stop
o Else increment pos
5. If not found → print "Not Found"
6. Stop
12. Sort (sort())
1. Start
2. If list empty → print message and stop
3. Use two pointers i and j
4. For each node i:
o For each node j = i->next:
If i->data > j->data → swap values
5. Print "List sorted"
6. Stop
Difference Between Singly and Doubly Linked List: