Menu driven program for all operations on doubly linked list in C Last Updated : 29 Nov, 2021 Comments Improve Suggest changes Like Article Like Report A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. A Doubly Linked List in contains three parts: one is the data part and the other two are the address of the next and previous node in the list. In this article, all the common operations of a doubly linked list is discussed in one menu-driven program. Operations to be performed: traverse(): To see the contents of the linked list, it is necessary to traverse the given doubly linked list. The given traverse() function traverses and prints the content of the doubly linked list.insertAtFront(): This function simply inserts an element at the front/beginning of the doubly linked list.insertAtEnd(): This function inserts an element at the end of the doubly linked list.insertAtPosition(): This function inserts an element at a specified position in the doubly linked list.deleteFirst(): This function simply deletes an element from the front/beginning of the doubly linked list.deleteEnd(): This function simply deletes an element from the end of the doubly linked list.deletePosition(): This function deletes an element from a specified position in the doubly linked list. Below is the implementation of the above operations: C // C program for the all operations in // the Doubly Linked List #include <stdio.h> #include <stdlib.h> // Linked List Node struct node { int info; struct node *prev, *next; }; struct node* start = NULL; // Function to traverse the linked list void traverse() { // List is empty if (start == NULL) { printf("\nList is empty\n"); return; } // Else print the Data struct node* temp; temp = start; while (temp != NULL) { printf("Data = %d\n", temp->info); temp = temp->next; } } // Function to insert at the front // of the linked list void insertAtFront() { int data; struct node* temp; temp = (struct node*)malloc(sizeof(struct node)); printf("\nEnter number to be inserted: "); scanf("%d", &data); temp->info = data; temp->prev = NULL; // Pointer of temp will be // assigned to start temp->next = start; start = temp; } // Function to insert at the end of // the linked list void insertAtEnd() { int data; struct node *temp, *trav; temp = (struct node*)malloc(sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\nEnter number to be inserted: "); scanf("%d", &data); temp->info = data; temp->next = NULL; trav = start; // If start is NULL if (start == NULL) { start = temp; } // Changes Links else { while (trav->next != NULL) trav = trav->next; temp->prev = trav; trav->next = temp; } } // Function to insert at any specified // position in the linked list void insertAtPosition() { int data, pos, i = 1; struct node *temp, *newnode; newnode = malloc(sizeof(struct node)); newnode->next = NULL; newnode->prev = NULL; // Enter the position and data printf("\nEnter position : "); scanf("%d", &pos); // If start==NULL, if (start == NULL) { start = newnode; newnode->prev = NULL; newnode->next = NULL; } // If position==1, else if (pos == 1) { // this is author method its correct but we can simply call insertAtfront() function for this special case /* newnode->next = start; newnode->next->prev = newnode; newnode->prev = NULL; start = newnode; */ // now this is improved by Jay Ghughriwala on geeksforgeeks insertAtFront(); } // Change links else { printf("\nEnter number to be inserted: "); scanf("%d", &data); newnode->info = data; temp = start; while (i < pos - 1) { temp = temp->next; i++; } newnode->next = temp->next; newnode->prev = temp; temp->next = newnode; temp->next->prev = newnode; } } // Function to delete from the front // of the linked list void deleteFirst() { struct node* temp; if (start == NULL) printf("\nList is empty\n"); else { temp = start; start = start->next; if (start != NULL) start->prev = NULL; free(temp); } } // Function to delete from the end // of the linked list void deleteEnd() { struct node* temp; if (start == NULL) printf("\nList is empty\n"); temp = start; while (temp->next != NULL) temp = temp->next; if (start->next == NULL) start = NULL; else { temp->prev->next = NULL; free(temp); } } // Function to delete from any specified // position from the linked list void deletePosition() { int pos, i = 1; struct node *temp, *position; temp = start; // If DLL is empty if (start == NULL) printf("\nList is empty\n"); // Otherwise else { // Position to be deleted printf("\nEnter position : "); scanf("%d", &pos); // If the position is the first node if (pos == 1) { deleteFirst(); // im,proved by Jay Ghughriwala on GeeksforGeeks if (start != NULL) { start->prev = NULL; } free(position); return; } // Traverse till position while (i < pos - 1) { temp = temp->next; i++; } // Change Links position = temp->next; if (position->next != NULL) position->next->prev = temp; temp->next = position->next; // Free memory free(position); } } // Driver Code int main() { int choice; while (1) { printf("\n\t1 To see list\n"); printf("\t2 For insertion at" " starting\n"); printf("\t3 For insertion at" " end\n"); printf("\t4 For insertion at " "any position\n"); printf("\t5 For deletion of " "first element\n"); printf("\t6 For deletion of " "last element\n"); printf("\t7 For deletion of " "element at any position\n"); printf("\t8 To exit\n"); printf("\nEnter Choice :\n"); scanf("%d", &choice); switch (choice) { case 1: traverse(); break; case 2: insertAtFront(); break; case 3: insertAtEnd(); break; case 4: insertAtPosition(); break; case 5: deleteFirst(); break; case 6: deleteEnd(); break; case 7: deletePosition(); break; case 8: exit(1); break; default: printf("Incorrect Choice. Try Again \n"); continue; } } return 0; } Output: Menu: Insertion at the starting: Insertion at the end: Insertion at specific position: Print the Linked List: Delete the first and last element with choice 5 and 6: Comment More infoAdvertise with us Next Article Menu driven program for all operations on doubly linked list in C imsushant12 Follow Improve Article Tags : Linked List Technical Scripter C Programs DSA Technical Scripter 2020 +1 More Practice Tags : Linked List Similar Reads Program for all operations on Circular Linked List in C In a Circular linked list, every element has a link to its next element in the sequence, and the last element has a link to the first element. A circular linked list is similar to the singly linked list except that the last node points to the first node. Below is the image to illustrate the same: 1. 11 min read C Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read C Program For Deleting A Node In A Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read C Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list 4 min read C# Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list 4 min read C Program For Detecting Loop In A Linked List Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. Solution: Floyd's Cycle-Finding Algorithm Approach: This is the fastest method and has been described below: Traverse linked list using two pointers.Move one pointer(slow_p) by one and anoth 2 min read C Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l 3 min read C Program For Pairwise Swapping Elements Of A Given Linked List Given a singly linked list, write a function to swap elements pairwise. Input: 1->2->3->4->5->6->NULL Output: 2->1->4->3->6->5->NULL Input: 1->2->3->4->5->NULL Output: 2->1->4->3->5->NULL Input: 1->NULL Output: 1->NULL For examp 3 min read C Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. C // A linked list node struct Node { int data; struct Node *next 7 min read C Program For Finding The Middle Element Of A Given Linked List Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list 4 min read Like