Advertisement
AnindyaBiswas

CDLL InsertPos and DeletePos

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