Miti059

doubly linked list

Oct 13th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.46 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5.     int a;
  6.     struct node*next,*prev;
  7. } node;
  8.  
  9.  
  10. node*head=NULL,*tail=NULL;
  11.  
  12. int main()
  13. {
  14.     int ch;
  15.     while(1)
  16.     {
  17.         printf("\n\nDoubly linked list operations\n\n");
  18.  
  19.  
  20.         printf("1.Insert First\n");
  21.         printf("2.Insert Any\n");
  22.         printf("3.Delete Elements\n");
  23.         printf("4.display\n");
  24.         printf("5.Delete Position\n");
  25.         printf("6.Search Data\n");
  26.         printf("7.Insert at end\n");
  27.         printf("8.display_last\n");
  28.         printf("Enter your choice:");
  29.  
  30.         scanf("%d",&ch);
  31.  
  32.         switch(ch)
  33.         {
  34.         case 1 :
  35.             Insert_first();
  36.             break;
  37.         case 2:
  38.                insert_any();
  39.             break;
  40.         case 3 :
  41.             delete_data();
  42.             break;
  43.         case 4:
  44.             display_first();
  45.             break;
  46.         case 5:
  47.             Delete_Position();
  48.             break;
  49.         case 6:
  50.             Search_Data();
  51.             break;
  52.         case 7:
  53.             Insert_End();
  54.             break;
  55.         case 8:
  56.             display_last();
  57.             break;
  58.            dafault:
  59.             printf("Invalid choice");
  60.  
  61.         }
  62.     }
  63. return 0;}
  64.  
  65. void Insert_first()
  66. {
  67.     node *N=(node*)malloc(sizeof(node));
  68.     printf("Enter the number:");
  69.     scanf(" %d",&N->a);
  70.     N->next=NULL;
  71.     N->prev=NULL;
  72.     if(head==NULL)
  73.     {
  74.         head=N;
  75.         tail=N;
  76.     }
  77.     else
  78.     {
  79.         head->prev=N;
  80.         N->next=head;
  81.         head=N;
  82.  
  83.     }
  84. }
  85. void insert_any()
  86. {
  87.     int p;
  88.     node *list=head;
  89.     node *N=(node*)malloc(sizeof(node));
  90.     printf("Enter the number:");
  91.     scanf(" %d",&N->a);
  92.     N->next=NULL;
  93.     N->prev=NULL;
  94.     printf("Enter Position: ");
  95.     scanf(" %d",&p);
  96.     if(p==1 )
  97.     {
  98.         if(head==NULL)
  99.         {
  100.             head=N;
  101.             tail=N;
  102.         }
  103.         else
  104.         {
  105.             head->prev=N;
  106.             N->next=head;
  107.             head=N;
  108.         }
  109.     }
  110.     else
  111.     {
  112.         p=p-2;
  113.         while(p!=0 && list->next!=NULL)
  114.         {
  115.             list=list->next;
  116.             p--;
  117.         }
  118.         if(list->next==NULL)
  119.         {
  120.             N->prev=tail;
  121.             tail->next=N;
  122.             tail=N;
  123.  
  124.         }
  125.         else
  126.         {
  127.             list->next->prev=N;
  128.             N->next=list->next;
  129.             N->prev=list;
  130.             list->next=N;
  131.         }
  132.     }
  133.  
  134.  
  135. }
  136. void Insert_End()
  137. {
  138.     node *N=(node*)malloc(sizeof(node));
  139.     printf("enter the number:");
  140.     scanf(" %d",&N->a);
  141.     N->next=NULL;
  142.     N->prev=NULL;
  143.     node *list=head;
  144.     if(head==NULL)
  145.     {
  146.         head=N;
  147.         tail=N;
  148.     }
  149.     else
  150.     {
  151.         tail->next=N;
  152.         N->prev=tail;
  153.         tail=N;
  154.     }
  155.  
  156. }
  157.  
  158.  
  159.  
  160. void  delete_data()
  161. {
  162.     int d;
  163.     printf("Enter the number:");
  164.     scanf(" %d",&d);
  165.     node*list=head;
  166.     if(head==NULL)
  167.     {
  168.         printf("No Data!!\n\n");
  169.     }
  170.     else if(list->a==d)
  171.     {
  172.         if(list->next==NULL)
  173.         {
  174.             head=NULL;
  175.             tail=NULL;
  176.             printf("Data Deleted\n\n");
  177.             free(list);
  178.         }
  179.         else
  180.         {
  181.             list->next->prev=NULL;
  182.             head=list->next;
  183.             printf("Data Deleted\n\n");
  184.             free(list);
  185.         }
  186.     }
  187.     else
  188.     {
  189.         node*temp;
  190.         while(list->a!=d && list->next!=NULL)
  191.         {
  192.             list=list->next;
  193.             if(list==NULL)
  194.             {
  195.                 printf("No Data Found!!\n\n");
  196.                 break;
  197.             }
  198.         }
  199.         if(list->a==d && list->next==NULL)
  200.         {
  201.             list=tail;
  202.             temp=list;
  203.             temp->prev->next=temp->next;
  204.             tail=temp->prev;
  205.             printf("Data Deleted\n\n");
  206.             free(temp);
  207.  
  208.         }
  209.         else if(list->a==d && list->next!=NULL)
  210.         {
  211.             temp=list;
  212.             temp->prev->next=temp->next;
  213.             temp->next->prev=temp->prev;
  214.             temp->next=NULL;
  215.             temp->prev=NULL;
  216.             printf("Data Deleted\n\n");
  217.             free(temp);
  218.         }
  219.  
  220.     }
  221.  
  222. }
  223. void Delete_Position()
  224. {
  225.  
  226.     int p;
  227.     node *list=head,*temp;
  228.     printf("Enter Position:");
  229.     scanf(" %d",&p);
  230.     if(head==NULL)
  231.     {
  232.         printf("No Position!!\n\n");
  233.     }
  234.     else if(p==1)
  235.     {
  236.         if(list->next==NULL)
  237.         {
  238.             printf("Data Deleted\n\n");
  239.             free(list);
  240.         }
  241.         else
  242.         {
  243.             head=list->next;
  244.             head->prev=NULL;
  245.             printf("Data Deleted\n\n");
  246.             free(list);
  247.         }
  248.     }
  249.     else
  250.     {
  251.         p=p-2;
  252.         while(p!=0 && list->next!=NULL)
  253.         {
  254.             list=list->next;
  255.             p--;
  256.             if(list==NULL)
  257.             {
  258.                 printf("No Data Found!!\n\n");
  259.                 break;
  260.             }
  261.         }
  262.         if(list->next==NULL)
  263.         {
  264.             printf("Wrong Position!!\n\n");
  265.         }
  266.         else if(list->next->next==NULL && p==0)
  267.         {
  268.             temp=tail;
  269.             tail=tail->prev;
  270.             tail->next=NULL;
  271.             printf("Data Deleted\n\n");
  272.             free(temp);
  273.         }
  274.         else if(list->next->next!=NULL && p==0)
  275.         {
  276.             temp=list->next;
  277.             temp->next->prev=list;
  278.             list->next=temp->next;
  279.             printf("Data Deleted\n\n");
  280.             free(temp);
  281.         }
  282.     }
  283.  
  284. }
  285.  
  286. void Search_Data()
  287. {
  288.     node *list=head;
  289.     int s;
  290.     printf("Enter Your number:");
  291.     scanf("%d",&s);
  292.     if(list->a==s)
  293.     {
  294.         printf("Found: %d\n\n",list->a);
  295.     }
  296.     else
  297.     {
  298.         while(list->a!=s)
  299.         {
  300.             list=list->next;
  301.             if(list==NULL)
  302.             {
  303.                 printf("No Data!!\n\n");
  304.                 break;
  305.             }
  306.         }
  307.         printf("%d\n\n",list->a);
  308.     }
  309. }
  310.  
  311. void display_last()
  312. {
  313.     node*list=tail;
  314.     if(head==NULL)
  315.     {
  316.         printf("No Data Available!!\n\n");
  317.  
  318.  
  319.     }
  320.     else
  321.     {
  322.         while(list!=NULL)
  323.         {
  324.  
  325.             printf("%d\n\n",list->a);
  326.             list=list->prev;
  327.         }
  328.     }
  329.  
  330. }
  331. void display_first()
  332. {
  333.     node*list=head;
  334.     if(head==NULL)
  335.     {
  336.         printf("No Data Available\n");
  337.  
  338.  
  339.     }
  340.     else
  341.     {
  342.         while(list!=NULL)
  343.         {
  344.             printf("%d\n",list->a);
  345.             list=list->next;
  346.         }
  347.     }
  348.     }
Add Comment
Please, Sign In to add comment