Advertisement
AnindyaBiswas

DLL InsertPos and DeletePos

Aug 24th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. using namespace std;
  2. #include<iostream>
  3.  
  4. struct Node{
  5.     int data;
  6.     struct Node *next, *prev;
  7. };
  8.  
  9. Node* Head = NULL, *Tail = NULL;
  10. int Size = 0;
  11.  
  12. void Traverse(){
  13.     struct Node* temp = Head;
  14.  
  15.     cout<<"\tThe Linked List is : \t";
  16.     while(temp != NULL){
  17.         cout<<temp->data<<" <-> ";
  18.         temp = temp->next;
  19.     }
  20.     cout<<"\tSize - "<<Size<<endl;
  21.     cout<<"\n------------------------------------------------------------------------\n";
  22. }
  23.  
  24. void InsertEnd(int x){
  25.     cout<<"Inserting "<<x<<" at the end"<<endl;
  26.     struct Node* node = new Node;
  27.     node->data = x;
  28.     if (Head == NULL){
  29.         Head = node;
  30.         Tail = node;
  31.         node->next = NULL;
  32.         node->prev = NULL;
  33.         Size++;
  34.         Traverse();
  35.         return;
  36.     }
  37.     Tail->next = node;
  38.     node->prev = Tail;
  39.     node->next = NULL;
  40.     Tail = node;
  41.     Size++;
  42.     Traverse();
  43. }
  44.  
  45. void InsertBeg(int x){
  46.     cout<<"Inserting "<<x<<" at the beginning"<<endl;
  47.     struct Node* node = new Node;
  48.     node->data = x;
  49.     if(Head == NULL){
  50.         Head = node;
  51.         Tail = node;
  52.         node->next = NULL;
  53.         node->prev = NULL;
  54.         Size++;
  55.         Traverse();
  56.         return;
  57.     }
  58.     node->next = Head;
  59.     node->prev = NULL;
  60.     Head->prev = node;
  61.     Head = node;
  62.     Size++;
  63.     Traverse();
  64. }
  65.  
  66.  
  67. void InsertPos(int x, int pos){
  68.     if (pos > Size){
  69.         cout<<"Invalid Position"<<endl;
  70.         return;
  71.     }
  72.     if (pos == 0){
  73.         InsertBeg(x);
  74.         return;
  75.     }
  76.     if (pos == Size){
  77.         InsertEnd(x);
  78.         return;
  79.     }
  80.     cout<<"Inserting "<<x<<" at position "<<pos<<endl;
  81.     Node* node = new Node;
  82.     node->data = x;
  83.    
  84.     if(pos > Size/2){
  85.         Node* temp = Tail;
  86.         pos = Size - pos - 1;
  87.         while(pos--){
  88.             temp = temp->prev;
  89.         }
  90.         node->next = temp;
  91.         node->prev = temp->prev;
  92.         temp->prev->next = node;
  93.         temp->prev = node;
  94.     }
  95.     else{
  96.         Node* temp = Head;
  97.         while(pos--){
  98.             temp = temp->next;
  99.         }
  100.         node->next = temp;
  101.         node->prev = temp->prev;
  102.         temp->prev->next = node;
  103.         temp->prev = node;
  104.     }
  105.  
  106.  
  107.     Size++;
  108.     Traverse();
  109. }
  110.  
  111.  
  112. void DeleteBeg(){
  113.     if (Head == NULL){
  114.         cout<<"List is empty"<<endl;
  115.         return;
  116.     }
  117.     cout<<"Deleting from the beginning"<<endl;
  118.     if (Head == Tail){
  119.         delete Head;
  120.         Head = NULL;
  121.         Tail = NULL;
  122.         Size--;
  123.         Traverse();
  124.         return;
  125.     }
  126.     Node* temp = Head;
  127.     Head = Head->next;
  128.     Head->prev = NULL;
  129.     delete temp;
  130.     Size--;
  131.     Traverse();
  132. }
  133.  
  134. void DeleteEnd(){
  135.     if (Head == NULL){
  136.         cout<<"List is empty"<<endl;
  137.         return;
  138.     }
  139.     cout<<"Deleting from the end"<<endl;
  140.     if (Head == Tail){
  141.         delete Head;
  142.         Head = NULL;
  143.         Tail = NULL;
  144.         Size--;
  145.         Traverse();
  146.         return;
  147.     }
  148.     Node* temp = Tail;
  149.     Tail = Tail->prev;
  150.     Tail->next = NULL;
  151.     delete temp;
  152.     Size--;
  153.     Traverse();
  154. }
  155.  
  156. void DeletePos(int pos){
  157.     if(Head == NULL){
  158.         cout<<"List is empty"<<endl;
  159.         return;
  160.     }
  161.     if(pos >= Size){
  162.         cout<<"Invalid Position"<<endl;
  163.         return;
  164.     }
  165.     if(pos == 0){
  166.         DeleteBeg();
  167.         return;
  168.     }
  169.     if(pos == Size-1){
  170.         DeleteEnd();
  171.         return;
  172.     }
  173.     cout<<"Deleting from position "<<pos<<endl;
  174.     if(pos > Size/2){
  175.         Node* temp = Tail;
  176.         pos = Size - pos - 1;
  177.         while(pos--){
  178.             temp = temp->prev;
  179.         }
  180.         temp->prev->next = temp->next;
  181.         temp->next->prev = temp->prev;
  182.         delete temp;
  183.     }
  184.     else{
  185.         Node* temp = Head;
  186.         while(pos--){
  187.             temp = temp->next;
  188.         }
  189.         temp->prev->next = temp->next;
  190.         temp->next->prev = temp->prev;
  191.         delete temp;
  192.     }
  193.     Size--;
  194.     Traverse();
  195. }
  196.  
  197.  
  198.  
  199. int main(){
  200.     InsertEnd(1);
  201.     InsertEnd(2);
  202.     InsertEnd(3);
  203.     InsertEnd(4);
  204.  
  205.  
  206.     InsertPos(796, 4);
  207.  
  208.     DeletePos(3);
  209.  
  210. }
  211.  
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement