Aniket_Goku

DLL

Dec 21st, 2020 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. //dll
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<stdlib.h>
  5. int val;
  6. struct dll
  7. {
  8.     int info;
  9.     struct dll *next;
  10.     struct dll *prev;
  11. }*node=NULL,*start=NULL,*prev=NULL,*temp=NULL;
  12. void create_node()
  13. {
  14.     node=(struct dll *)malloc(sizeof(struct dll));
  15.     if(node==NULL)
  16.     {
  17.         printf("\n Node is not allocated memory");
  18.         return;
  19.        
  20.     }
  21.     node->next=NULL;
  22.     node->prev=NULL;
  23. }
  24. void insert_beg();
  25. void insert_end();
  26. void display()
  27. {
  28.     temp=start;
  29.     if(start==NULL)
  30.     {
  31.         printf("\n DLL is empty for DIsplay");
  32.         return;
  33.     }
  34.     printf("\n Displayed DLl");
  35.     while(temp!=NULL)
  36.     {
  37.         printf("\n =>  %d",temp->info);
  38.         temp=temp->next;
  39.     }
  40. }
  41. void insert_beg()
  42. {
  43.     create_node();
  44.     printf("\n Enter the value: ");
  45.     scanf("%d",&val);
  46.    
  47.     if(start==NULL)
  48.     {
  49.         node->info=val;
  50.    
  51.         start=node;
  52.        
  53.     }
  54.     else
  55.     {
  56.         node->info=val;
  57.         node->next=start;
  58.         start->prev=node;
  59.         start=node;
  60.     }
  61.     display();
  62. }
  63. void insert_end()
  64. {
  65.     if(start==NULL)
  66.     {
  67.         insert_beg();
  68.         return;
  69.     }
  70.     create_node();
  71.     temp=start;
  72.     while(temp->next!=NULL)
  73.     {
  74.         temp=temp->next;
  75.     }
  76.    
  77.     printf("\n Enter the value: ");
  78.     scanf("%d",&val);
  79.     node->info=val;
  80.     node->prev=temp;
  81.     temp->next=node;
  82.     display();
  83. }
  84. void insert_spc_val()
  85. {
  86.     int t=0,l;
  87.     if(start==NULL)
  88.     {
  89.         printf("\n no values for select ");
  90.         return;
  91.        
  92.     }
  93.     printf("\n select from following dll\n");
  94.     display();
  95.     printf("\n select the the value: ");
  96.     scanf("%d",&val);
  97.     temp=start;
  98.     while(temp!=NULL)
  99.     {
  100.         if(start->info==val)
  101.         {
  102.             insert_beg();
  103.             t=1;
  104.             return;
  105.         }
  106.         if(temp->info==val)
  107.         {
  108.             t=1;
  109.             break;
  110.         }
  111.         prev=temp;
  112.         temp=temp->next;
  113.     }
  114.     if(t==0)
  115.     {
  116.         printf("\n wrong value inserted");
  117.         display();
  118.     }
  119.     else
  120.     {
  121.         create_node();
  122.         printf("\n ENter the value: ");
  123.         scanf("%d",&l);
  124.        
  125.         node->info=l;
  126.         node->prev=prev;
  127.         node->next=temp;
  128.         prev->next=node;
  129.         temp->prev=node;
  130.        
  131.         display();
  132.     }
  133. }
  134. void insert_spc_posi()
  135. {
  136.     int t=0;
  137.     if(start==NULL)
  138.     {
  139.         printf("\n no values for select ");
  140.         return;
  141.        
  142.     }
  143.     printf("\n select from following dll\n");
  144.     display();
  145.     printf("\n select the the position: ");
  146.     scanf("%d",&val);
  147.     if(val==1)
  148.     {
  149.         insert_beg();
  150.         return;
  151.     }
  152.     temp=start;
  153.     int i=1;
  154.     while(temp!=NULL)
  155.     {
  156.         if(i==val)
  157.         {
  158.             t=1;
  159.             create_node();
  160.             printf("\n ENter the value: ");
  161.             scanf("%d",&val);
  162.                    
  163.             node->info=val;
  164.             node->prev=prev;
  165.             node->next=temp;
  166.             prev->next=node;
  167.             temp->prev=node;
  168.            
  169.             display();
  170.             break;
  171.         }
  172.         i++;
  173.         prev=temp;
  174.         temp=temp->next;
  175.     }
  176.     if(t==0)
  177.     {
  178.         printf("\n wrong position inserted");
  179.         display();
  180.     }
  181.    
  182. }
  183. void del_beg()
  184. {
  185.     if(start==NULL)
  186.     {
  187.         printf("\n Dll is EMpty for Delete");
  188.         return;
  189.     }
  190.     printf("\n %d is deleted\n",start->info);
  191.     node=start;
  192.     start=start->next;
  193.     free(node);
  194.     display();
  195. }
  196. void del_end()
  197. {
  198.     if(start==NULL)
  199.     {
  200.         printf("\n Dll is empty for delete");
  201.         return;
  202.     }
  203.     if(start->next==NULL)
  204.     {
  205.         del_beg();
  206.         return;
  207.     }
  208.     temp=start;
  209.     while(temp->next!=NULL)
  210.     {
  211.         prev=temp;
  212.         temp=temp->next;
  213.     }
  214.     printf("\n %d is deleted\n",temp->info);
  215.     prev->next=NULL;
  216.     free(temp);
  217.     display();
  218. }
  219. void del_the_value()
  220. {
  221.     int t=0;
  222.     if(start==NULL)
  223.     {
  224.         printf("\n Dll is empty for delete");
  225.         return;
  226.     }
  227.     printf("\n\n \t Choose the value you want to Delete");
  228.     display();
  229.     printf("\n \nENter the value: ");
  230.     scanf("%d",&val);
  231.     if(start->info==val)
  232.     {
  233.         del_beg();
  234.         return;
  235.     }
  236.     temp=start;
  237.     while(temp!=NULL)
  238.     {
  239.         if(temp->info==val)
  240.         {
  241.             t=1;
  242.             prev->next=temp->next;
  243.             printf("\n %d is deleted\n",temp->info);
  244.             free(temp);
  245.             display();
  246.             break;
  247.         }
  248.         prev=temp;
  249.         temp=temp->next;
  250.     }
  251.     if(t==0)
  252.     {
  253.         printf("\n Wrong value inserted");
  254.         display();
  255.     }
  256.    
  257. }
  258. void del_the_posi()
  259. {
  260.     int t=0,l,i=16;
  261.     if(start==NULL)
  262.     {
  263.         printf("\n the DLL is empty for delete");
  264.         return;
  265.        
  266.     }
  267.     printf("\n chose the position you want to delete:- ");
  268.     display();
  269.     printf("\n Enter the position: ");
  270.     scanf("%d",&l);
  271.     if(l==1)
  272.     {
  273.         del_beg();
  274.         return;
  275.     }
  276.     temp=start;
  277.     while(temp!=NULL)
  278.     {
  279.         if(i==l)
  280.         {
  281.             prev->next=temp->next;
  282.             printf("\n %d is deleted\n",temp->info);
  283.             free(temp);
  284.             display();
  285.             break;
  286.         }
  287.         i++;
  288.         prev=temp;
  289.         temp=temp->next;
  290.     }
  291. }
  292. int main()
  293. {
  294.     int ch;
  295.     do
  296.     {
  297.         printf("\n\t\t DLL menu");
  298.         printf("\n 1.insert_beg");
  299.         printf("\n 2.insert_end");
  300.         printf("\n 3.insert_spc_value");
  301.         printf("\n 4.insert_spc_position");
  302.         printf("\n 5.delete_beg");
  303.         printf("\n 6.delete_end");
  304.         printf("\n 7.delete_spc_value");
  305.         printf("\n 8.delete_spc_position");
  306.         printf("\n 9.display");
  307.         printf("\n Enter  your choice: ");
  308.         scanf("%d",&ch);
  309.         switch(ch)
  310.         {
  311.             case 1:
  312.                 insert_beg();
  313.                 break;
  314.             case 2:
  315.                 insert_end();
  316.                 break;
  317.             case 3:
  318.                 insert_spc_val();
  319.                 break;
  320.             case 4:
  321.                 insert_spc_posi();
  322.                 break;
  323.             case 5:
  324.                 del_beg();
  325.                 break;
  326.             case 6:
  327.                 del_end();
  328.                 break;
  329.             case 7:
  330.                 del_the_value();
  331.                 break;
  332.             case 8:
  333.                 del_the_posi();
  334.                 break;
  335.             case 9:
  336.                 display();
  337.         }
  338.    
  339.     }while(ch<=9 && ch>=1);
  340.        
  341.     return 0;
  342. }
Add Comment
Please, Sign In to add comment