Doubly Linked List
Problem Statement: Write a Menu driven program for Doubly Linked List with following functionality-
i. Insert node at Beginning
ii. Insert node at End
iii. Insert node at specified position
iv. Delete node at Beginning
v. Delete node at End
vi. Delete node at specified position
vii. Search for an element
viii. Display List
//Menu driven Doubly linked list in C #include <stdio.h> #include <stdlib.h> struct node { struct node *prev; struct node *next; int data; }; struct node *head; void insertAtBeginning(); void insertAtLast(); void insertAtPosition(); void deleteAtBeginning(); void deleteAtLast(); void deleteAtPosition(); void display(); void search(); void main() { int choice = 0; while (choice != 9) { printf("\n*********Main Menu*********\n"); printf("\nChoose one option from the following list ...\n"); printf("\n===============================================\n"); printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\n9.Exit\n"); printf("\nEnter your choice?\n"); scanf("\n%d", &choice); switch (choice) { case 1: insertAtBeginning(); break; case 2: insertAtLast(); break; case 3: insertAtPosition(); break; case 4: deleteAtBeginning(); break; case 5: deleteAtLast(); break; case 6: deleteAtPosition(); break; case 7: search(); break; case 8: display(); break; case 9: exit(0); break; default: printf("Please enter valid choice.."); } } } void insertAtBeginning() { struct node *ptr; int item; ptr = (struct node *)malloc(sizeof(struct node)); if (ptr == NULL) { printf("\nOVERFLOW"); } else { printf("\nEnter Item value"); scanf("%d", &item); if (head == NULL) { ptr->next = NULL; ptr->prev = NULL; ptr->data = item; head = ptr; } else { ptr->data = item; ptr->prev = NULL; ptr->next = head; head->prev = ptr; head = ptr; } printf("\nNode inserted\n"); } } void insertAtLast() { struct node *ptr, *temp; int item; ptr = (struct node *)malloc(sizeof(struct node)); if (ptr == NULL) { printf("\nOVERFLOW"); } else { printf("\nEnter value"); scanf("%d", &item); ptr->data = item; if (head == NULL) { ptr->next = NULL; ptr->prev = NULL; head = ptr; } else { temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = ptr; ptr->prev = temp; ptr->next = NULL; } } printf("\nnode inserted\n"); } void insertAtPosition() { struct node *ptr, *temp; int item, loc, i; ptr = (struct node *)malloc(sizeof(struct node)); if (ptr == NULL) { printf("\n OVERFLOW"); } else { temp = head; printf("Enter the location"); scanf("%d", &loc); for (i = 0; i < loc; i++) { temp = temp->next; if (temp == NULL) { printf("\n There are less than %d elements", loc); return; } } printf("Enter value"); scanf("%d", &item); ptr->data = item; ptr->next = temp->next; ptr->prev = temp; temp->next = ptr; temp->next->prev = ptr; printf("\nnode inserted\n"); } } void deleteAtBeginning() { struct node *ptr; if (head == NULL) { printf("\n UNDERFLOW"); } else if (head->next == NULL) { head = NULL; free(head); printf("\nnode deleted\n"); } else { ptr = head; head = head->next; head->prev = NULL; free(ptr); printf("\nnode deleted\n"); } } void deleteAtLast() { struct node *ptr; if (head == NULL) { printf("\n UNDERFLOW"); } else if (head->next == NULL) { head = NULL; free(head); printf("\nnode deleted\n"); } else { ptr = head; if (ptr->next != NULL) { ptr = ptr->next; } ptr->prev->next = NULL; free(ptr); printf("\nnode deleted\n"); } } void deleteAtPosition() { struct node *ptr, *temp; int val; printf("\n Enter the data after which the node is to be deleted : "); scanf("%d", &val); ptr = head; while (ptr->data != val) ptr = ptr->next; if (ptr->next == NULL) { printf("\nCan't delete\n"); } else if (ptr->next->next == NULL) { ptr->next = NULL; } else { temp = ptr->next; ptr->next = temp->next; temp->next->prev = ptr; free(temp); printf("\nnode deleted\n"); } } void display() { struct node *ptr; printf("\n printing values...\n"); ptr = head; while (ptr != NULL) { printf("%d\n", ptr->data); ptr = ptr->next; } } void search() { struct node *ptr; int item, i = 0, flag; ptr = head; if (ptr == NULL) { printf("\nEmpty List\n"); } else { printf("\nEnter item which you want to search?\n"); scanf("%d", &item); while (ptr != NULL) { if (ptr->data == item) { printf("\nitem found at location %d ", i + 1); flag = 0; break; } else { flag = 1; } i++; ptr = ptr->next; } if (flag == 1) { printf("\nItem not found\n"); } } }
$ ./a.out
*********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? Enter Item value11 Node inserted *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? Enter value12 node inserted *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? There are less than 2 elements Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? printing values… *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? Enter value13 node inserted *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? Enter value14 node inserted *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? printing values… *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? Enter item which you want to search? item found at location 3 Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? node deleted *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? printing values… *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? node deleted *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? printing values… *********Main Menu********* Choose one option from the following list … =============================================== 1.Insert in beginning Enter your choice? |
If you like the post Doubly Linked List, please share your feedback!
also see
C Programming language |
Go Programming language |
Linked List | Array |
Simplification | Queue |
DBMS | Reasoning |
Aptitude | HTML |