Advertisement
splash365

doubly_linked_list

Jan 8th, 2021 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.85 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     Node *next;
  9.     Node *prev;
  10. };
  11.  
  12. Node *head = NULL;
  13. Node *last = NULL;
  14.  
  15. void createList(int n)
  16. {
  17.     for(int i=1; i<=n; i++)
  18.     {
  19.         Node * newNode = (Node*) malloc(sizeof(Node));
  20.         int val;
  21.         cout<<"Node "<<i<<": ";
  22.         cin>>val;
  23.         newNode->data = val;
  24.         newNode->next= NULL;
  25.         newNode->prev = NULL;
  26.  
  27.         if(head == NULL)
  28.         {
  29.             head = newNode;
  30.             last = head;
  31.         }
  32.         else
  33.         {
  34.             last->next = newNode;
  35.             newNode->prev = last;
  36.             last = newNode;
  37.         }
  38.     }
  39.     cout<<"\n\n";
  40. }
  41.  
  42. void insertFirst(int val)
  43. {
  44.     Node * newNode = (Node*) malloc(sizeof(Node));
  45.     newNode->data = val;
  46.     newNode->next= NULL;
  47.     newNode->prev = NULL;
  48.  
  49.     if(head == NULL)
  50.     {
  51.         head = newNode;
  52.         last = head;
  53.     }
  54.     else
  55.     {
  56.         newNode->next = head;
  57.         head->prev = newNode;
  58.         head = newNode;
  59.         printf("Successfully inserted!\n\n");
  60.     }
  61. }
  62.  
  63. void insertLast(int val)
  64. {
  65.     Node *newNode = (Node*) malloc(sizeof(Node));
  66.     newNode->data = val;
  67.     newNode->next = NULL;
  68.     newNode->prev = NULL;
  69.     if(head == NULL)
  70.     {
  71.         head = newNode;
  72.         last = head;
  73.     }
  74.     else
  75.     {
  76.         newNode->prev = last;
  77.         last->next = newNode;
  78.         last = newNode;
  79.         printf("Successfully inserted!\n\n");
  80.     }
  81. }
  82.  
  83. void insertAt(int pos,int val)
  84. {
  85.     if(pos==1)
  86.     {
  87.         insertFirst(val);
  88.         return;
  89.     }
  90.     Node * newNode = (Node*) malloc(sizeof(Node));
  91.     newNode->data = val;
  92.     newNode->next= NULL;
  93.     newNode->prev = NULL;
  94.  
  95.     Node *curNode = head;
  96.  
  97.     for(int i=2; i<pos; i++)
  98.     {
  99.         curNode = curNode -> next;
  100.         if(curNode == NULL)
  101.         {
  102.             cout<<"Insertion not possible\n";
  103.             return;
  104.         }
  105.     }
  106.     if(curNode == last) insertLast(val);
  107.     else
  108.     {
  109.         newNode->next = curNode->next;
  110.         newNode->prev = curNode;
  111.         curNode->next->prev = newNode;
  112.         curNode->next = newNode;
  113.         printf("Successfully inserted!\n\n");
  114.     }
  115.  
  116.  
  117. }
  118.  
  119. void deletefirst()
  120. {
  121.     if(head == NULL)
  122.     {
  123.         cout<<"No list is created!\n";
  124.     }
  125.     else
  126.     {
  127.         if(head->next==NULL)
  128.         {
  129.             head = NULL;
  130.             last = NULL;
  131.             return;
  132.         }
  133.         Node *curNode = head;
  134.         head=head->next;
  135.         head->prev = NULL;
  136.         free(curNode);
  137.         printf("Successfully Deleted!\n\n");
  138.     }
  139. }
  140.  
  141. void deleteLast()
  142. {
  143.     if(head == NULL)
  144.     {
  145.         cout<<"No list is created!\n";
  146.     }
  147.     else
  148.     {
  149.         if(last->prev==NULL)
  150.         {
  151.             head = NULL;
  152.             last = NULL;
  153.             return;
  154.         }
  155.         Node *curNode = last;
  156.         last=last->prev;
  157.         last->next = NULL;
  158.         free(curNode);
  159.         printf("Successfully Deleted!\n\n");
  160.     }
  161. }
  162.  
  163.  
  164. void deleteAt(int pos)
  165. {
  166.     if(head==NULL)
  167.     {
  168.         cout<<"No list exists!\n";
  169.         return;
  170.     }
  171.     Node *curNode = head;
  172.     for(int i=1; i<pos; i++)
  173.     {
  174.         curNode = curNode->next;
  175.         if(curNode == NULL)
  176.         {
  177.             cout<<"Deletion not possible\n";
  178.             return;
  179.         }
  180.     }
  181.     if(curNode==head) deletefirst();
  182.     else if(curNode==last) deleteLast();
  183.     else
  184.     {
  185.         curNode->prev->next = curNode->next;
  186.         curNode->next->prev = curNode->prev;
  187.         free(curNode);
  188.         printf("Successfully Deleted!\n\n");
  189.     }
  190. }
  191.  
  192. void printFromHead()
  193. {
  194.     if(head == NULL)
  195.     {
  196.         cout<<"No list!!\n";
  197.     }
  198.     else
  199.     {
  200.         cout<<"List from Head: ";
  201.         Node *curNode = head;
  202.         while(curNode != NULL)
  203.         {
  204.             cout<<curNode->data<<" ";
  205.             curNode = curNode -> next;
  206.         }
  207.         cout<<"\n\n";
  208.     }
  209. }
  210.  
  211. void printFromLast()
  212. {
  213.     if(head == NULL)
  214.     {
  215.         cout<<"No list!!\n";
  216.     }
  217.     else
  218.     {
  219.         cout<<"List from Last: ";
  220.         Node *curNode = last;
  221.         while(curNode != NULL)
  222.         {
  223.             cout<<curNode->data<<" ";
  224.             curNode = curNode -> prev;
  225.         }
  226.         cout<<"\n\n";
  227.     }
  228. }
  229.  
  230. int searchFromHead(int val)
  231. {
  232.     if(head==NULL)
  233.     {
  234.         return -1;
  235.     }
  236.     else
  237.     {
  238.         Node *curNode = head;
  239.         int index = 1;
  240.         while (curNode != NULL)
  241.         {
  242.             if(curNode->data == val)
  243.             {
  244.                 return index;
  245.             }
  246.             index++;
  247.             curNode = curNode->next;
  248.         }
  249.         return 0;
  250.     }
  251. }
  252.  
  253. int searchFromLast(int val)
  254. {
  255.     if(head==NULL)
  256.     {
  257.         return -1;
  258.     }
  259.     else
  260.     {
  261.         Node *curNode = last;
  262.         int index = 1;
  263.         while (curNode != NULL)
  264.         {
  265.             if(curNode->data == val)
  266.             {
  267.                 return index;
  268.             }
  269.             index++;
  270.             curNode = curNode->prev;
  271.         }
  272.         return 0;
  273.     }
  274. }
  275.  
  276. void freeTheList()
  277. {
  278.     Node *curNode = head;
  279.     while(head != NULL)
  280.     {
  281.         curNode = head;
  282.         head = head->next;
  283.         free(curNode);
  284.     }
  285. }
  286.  
  287. int main(){
  288.     printf("Enter the number of elements in the list: ");
  289.     int n;
  290.     scanf("%d",&n);
  291.     printf("Create the list\n");
  292.     createList(n);
  293.     printFromHead();
  294.     printFromLast();
  295.     printf("A doubly linked list is being created!\n");
  296.  
  297.     while(1)
  298.     {
  299.         printf("1. Insert First\n");
  300.         printf("2. Insert Last\n");
  301.         printf("3. Insert At\n");
  302.         printf("4. Delete First\n");
  303.         printf("5. Delete Last\n");
  304.         printf("6. Delete At\n");
  305.         printf("7. Search From Head\n");
  306.         printf("8. Search From Last\n");
  307.         printf("9. Print List From Head\n");
  308.         printf("10. Print List From Last\n");
  309.         printf("11. Exit\n");
  310.         printf("Enter Choice: ");
  311.         int choice;
  312.         scanf("%d",&choice);
  313.         printf("\n");
  314.         if(choice == 1)
  315.         {
  316.             printf("Enter the element: ");
  317.             int val;
  318.             scanf("%d",&val);
  319.             insertFirst(val);
  320.         }
  321.         else if(choice == 2)
  322.         {
  323.             printf("Enter the element: ");
  324.             int val;
  325.             scanf("%d",&val);
  326.             insertLast(val);
  327.         }
  328.         else if(choice == 3)
  329.         {
  330.             printf("Enter the position: ");
  331.             int pos;
  332.             scanf("%d",&pos);
  333.             printf("Enter the element: ");
  334.             int val;
  335.             scanf("%d",&val);
  336.             insertAt(pos,val);
  337.  
  338.         }
  339.         else if (choice == 4) deletefirst();
  340.         else if (choice == 5) deleteLast();
  341.         else if (choice == 6)
  342.         {
  343.             printf("Enter the position: ");
  344.             int pos;
  345.             scanf("%d",&pos);
  346.             deleteAt(pos);
  347.         }
  348.         else if(choice == 7)
  349.         {
  350.             printf("Enter the value: ");
  351.             int val;
  352.             scanf("%d",&val);
  353.             int pos = searchFromHead(val);
  354.             if(pos == -1) cout<<"No list exists!\n";
  355.             else if(pos) cout<<"Position from Head: "<<pos<<"\n\n";
  356.             else cout<<"Item not found in the list\n";
  357.         }
  358.         else if(choice == 8)
  359.         {
  360.             printf("Enter the value: ");
  361.             int val;
  362.             scanf("%d",&val);
  363.             int pos = searchFromLast(val);
  364.             if(pos == -1) cout<<"No list exists!\n";
  365.             else if(pos) cout<<"Position from Last: "<<pos<<"\n\n";
  366.             else cout<<"Item not found in the list\n";
  367.         }
  368.         else if(choice == 9) printFromHead();
  369.         else if(choice == 10) printFromLast();
  370.         else if(choice == 11) break;
  371.         else printf("Wrong Choice Input!!\n\n");
  372.     }
  373.     freeTheList();
  374. }
  375.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement