Advertisement
ppathak35

doubly linked list

Jun 8th, 2022 (edited)
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. // Online C compiler to run C program online
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct Node {
  6.     int data;
  7.     struct Node *prev;
  8.     struct Node *next;
  9. } *start = NULL;
  10.  
  11. void createList(int d) {
  12.     struct Node *temp;
  13.     temp = (struct Node *)malloc(sizeof(struct Node));
  14.     temp->data = d;
  15.     temp->next = NULL;
  16.     temp->prev = NULL;
  17.     if (start == NULL) {
  18.         start = temp;
  19.     }
  20. }
  21.  
  22. void insertBeg(int d){
  23.     struct Node *temp;
  24.     temp = (struct Node *)malloc(sizeof(struct Node));
  25.     temp->data = d;
  26.     temp->next = NULL;
  27.     temp->prev = NULL;
  28.     if (start == NULL) {
  29.         start = temp;
  30.     }
  31.     else {
  32.         temp->next = start;
  33.         start->prev = temp;
  34.         start = temp;
  35.     }
  36. }
  37.  
  38. void insertEnd(int d) {
  39.     struct Node *temp, *ptr;
  40.     temp = (struct Node *)malloc(sizeof(struct Node));
  41.     temp->data = d;
  42.     temp->next = NULL;
  43.     temp->prev = NULL;
  44.     ptr = start;
  45.     while(ptr->next != NULL) {
  46.         ptr = ptr->next;
  47.     }
  48.     ptr->next = temp;
  49.     temp->prev = ptr;
  50. }
  51.  
  52. void popBeg() {
  53.     struct Node *temp;
  54.     temp = start;
  55.     start = start->next;
  56.     start->prev = NULL;
  57.     printf("element deleted %d", temp->data);
  58.     free(temp);
  59. }
  60.  
  61. void popEnd(){
  62.     struct Node *temp, *ptr;
  63.     ptr = start;
  64.     while (ptr->next->next != NULL) {
  65.         ptr = ptr->next;
  66.     }
  67.     temp = ptr->next;
  68.     ptr->next = NULL;
  69.     printf("element deleted %d", temp->data);
  70.     free(temp);
  71. }
  72.  
  73. void listLength(){
  74.     struct Node *ptr;
  75.     int len =0;
  76.     ptr = start;
  77.     while(ptr != NULL){
  78.         len += 1;
  79.         ptr = ptr->next;
  80.     }
  81.     printf("Number of elements : %d", len);
  82. }
  83.  
  84. void displayList(){
  85.     struct Node *ptr;
  86.     ptr = start;
  87.     while(ptr!= NULL) {
  88.         printf("%d -> ", ptr->data);
  89.         ptr = ptr->next;
  90.     }
  91. }
  92.  
  93. int main() {
  94.     int choice=0, data=0;
  95.     while (choice != 9) {
  96.         printf("\n1. Create A Doubly Linked List");
  97.         printf("\n2. Display List");
  98.         printf("\n3. Insert Element at Beg");
  99.         printf("\n4. Insert Element at End");
  100.         printf("\n5. Get List Length");
  101.         printf("\n6. Delete from Beg");
  102.         printf("\n7. Delete from End");
  103.         printf("\n9. Exit");
  104.        
  105.         printf("\nEnter choice : ");
  106.         scanf("%d", &choice);
  107.        
  108.         switch(choice) {
  109.             case 1:
  110.                     printf("\nInsert Head Element : ");
  111.                     scanf("%d", &data);
  112.                     createList(data);
  113.                     break;
  114.             case 2:
  115.                     displayList();
  116.                     break;
  117.             case 3:
  118.                     printf("\nInsert Element : ");
  119.                     scanf("%d", &data);
  120.                     insertBeg(data);
  121.                     break;
  122.             case 4:
  123.                     printf("\nInsert Element : ");
  124.                     scanf("%d", &data);
  125.                     insertEnd(data);
  126.                     break;
  127.             case 5:
  128.                     listLength();
  129.                     break;
  130.             case 6:
  131.                     popBeg();
  132.                     break;
  133.             case 7:
  134.                     popEnd();
  135.                     break;
  136.             case 9: break;
  137.             default: break;
  138.         }
  139.     }
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement