Advertisement
193030

Double linked list insert delete

Oct 13th, 2020 (edited)
2,225
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     struct Node* previous;
  8.     struct Node* next;
  9.     int data;
  10. } *first = NULL, *last = NULL;
  11.  
  12. void add(int x)
  13. {
  14.     if(first == NULL)
  15.     {
  16.         first = new Node;
  17.         first->data = x;
  18.         first->previous = NULL;
  19.         first->next = NULL;
  20.         last = first;
  21.     }
  22.     else
  23.     {
  24.         Node *t = new Node;
  25.         t->data = x;
  26.         t->previous = last;
  27.         t->next = NULL;
  28.         last->next = t;
  29.         last = t;
  30.     }
  31. }
  32.  
  33. void display(struct Node *p)
  34. {
  35.     while(p)
  36.     {
  37.         cout << p->data << " ";
  38.         p = p->next;
  39.     }
  40. }
  41. void displayReverse(struct Node*p)
  42. {
  43.     while(p)
  44.     {
  45.         cout << p->data << " ";
  46.         p = p->previous;
  47.     }
  48. }
  49.  
  50. void insertAfter(int key, int val)
  51. {
  52.     Node *p = first;
  53.     if(first->data == key)
  54.     {
  55.         Node *t = new Node;
  56.         t->data = val;
  57.         t->previous = first;
  58.         t->next = first->next;
  59.         first->next->previous = t;
  60.         first->next = t;
  61.     }
  62.     else
  63.     {
  64.         while(p->data!=key)
  65.         {
  66.             p = p->next;
  67.         }
  68.         Node *t = new Node;
  69.         t->data = val;
  70.         t->next = p->next;
  71.         t->previous = p;
  72.         p->next = t;
  73.     }
  74.  
  75. }
  76.  
  77. void deleteAfter(int key)
  78. {
  79.  
  80.  
  81.     Node *p = first;
  82.     if(key == first->data)
  83.     {
  84.         Node *t = first;
  85.         first = first->next;
  86.         first->previous = NULL;
  87.         delete t;
  88.  
  89.     }
  90.     else
  91.     {
  92.         while(p->data !=key )
  93.         {
  94.             p = p->next;
  95.         }
  96.         Node *t = new Node;
  97.         t = p->next;
  98.         if(t->next == NULL)
  99.         {
  100.             p ->next = NULL;
  101.         }
  102.         else
  103.         {
  104.           p->next =p->next->next;
  105.           p->next->previous = p;
  106.         }
  107.         delete t;
  108.     }
  109. }
  110.  
  111. void insertBefore(int key, int val)
  112. {
  113.     Node *p = first;
  114.     if(p->data == key)
  115.     {
  116.         Node *t = new Node;
  117.         t->next = first->next;
  118.         t->previous = NULL;
  119.         t->data = val;
  120.         first = t;
  121.     }
  122.     else
  123.     {
  124.         while(p->data!=key)
  125.         {
  126.             p = p->next;
  127.         }
  128.         Node *t = new Node;
  129.         t->data = val;
  130.         t->next = p;
  131.         t->previous = p->previous;
  132.         p->previous->next = t;
  133.         p->previous = t;
  134.     }
  135. }
  136.  
  137. int main()
  138. {
  139.     add(11);
  140.     add(2);
  141.     add(3);
  142.     add(5);
  143.     add(6);
  144.     add(4);
  145.     display(first);
  146.     cout << endl;
  147.     //insertAfter(2,10);
  148.     //deleteAfter(6);
  149.     insertBefore(3,9);
  150.     display(first);
  151.  
  152. }
  153.  
Advertisement
RAW Paste Data Copied
Advertisement