Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5.     char a[15];
  6.     char ch[30];
  7.     struct node *next, *prev;
  8. }node;
  9. node *head=NULL, *tail=NULL;
  10. void display_last_to_first()
  11. {
  12.     node *list=tail;
  13.     if(head==NULL)
  14.         {
  15.         printf("No DATA Available!!\n\n");
  16.         }
  17.     else
  18.     {
  19.         while(list!=NULL)
  20.         {
  21.             printf("Name: %s\n", list->ch);
  22.             printf("Roll: %s\n", list->a);
  23.             list=list->prev;
  24.         }
  25.     }
  26.     printf("1. Menu\n");
  27.     printf("2. Exit\n");
  28.     printf("Choose an option:\n");
  29.     int x;
  30.     scanf("%d", &x);
  31.     switch(x)
  32.     {
  33.     case 1:
  34.     {
  35.         menu();
  36.         break;
  37.     }
  38.     case 2:
  39.     {
  40.         exit(0);
  41.         break;
  42.     }
  43.     default :
  44.     {
  45.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  46.         menu();
  47.         break;
  48.     }
  49.     }
  50. }
  51.  
  52. void display_first_to_last()
  53. {
  54.     node*list=head;
  55.     if(head==NULL)
  56.     {
  57.         printf("There's no data available!!\n");
  58.         }
  59.     else
  60.     {
  61.         while(list!=NULL)
  62.         {
  63.             printf("Name: %s\n", list->ch);
  64.             printf("Roll: %s\n", list->a);
  65.             list=list->next;
  66.         }
  67.     }
  68.     printf("1. Menu\n");
  69.     printf("2. Exit\n");
  70.     printf("Choose an option:\n");
  71.     int x;
  72.     scanf("%d", &x);
  73.     switch(x)
  74.     {
  75.     case 1:
  76.     {
  77.         menu();
  78.         break;
  79.     }
  80.     case 2:
  81.     {
  82.         exit(0);
  83.         break;
  84.     }
  85.     default :
  86.     {
  87.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  88.         menu();
  89.         break;
  90.     }
  91.     }
  92.     }
  93.  
  94. void display()
  95. {
  96.     printf("1. Display first to last\n");
  97.     printf("2. Display last to first\n");
  98.     printf("Choose an option:\n");
  99.     int n;
  100.     scanf("%d", &n);
  101.     switch(n)
  102.     {
  103.     case 1:
  104.     {
  105.         display_first_to_last();
  106.         break;
  107.     }
  108.     case 2:
  109.     {
  110.         display_last_to_first();
  111.         break;
  112.     }
  113.     default:
  114.     {
  115.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  116.         menu();
  117.         break;
  118.     }
  119.     }
  120. }
  121.  
  122. void search_by_pos()
  123. {
  124.     int n;
  125.     printf("Enter the Position:\n");
  126.     scanf("%d", &n);
  127.     node *list=head;
  128.     if(n==1)
  129.     {
  130.         printf("Name: %s\n", list->ch);
  131.         printf("Roll: %s\n\n", list->a);
  132.     }
  133.     else
  134.     {
  135.         n=n-2;
  136.         while(n!=0 && list->next!=NULL)
  137.         {
  138.             list=list->next;
  139.             n--;
  140.         }
  141.         if(list->next==NULL)
  142.         {
  143.             printf("There's No Information!!\n");
  144.         }
  145.         else
  146.         {
  147.             printf("Name: %s\n", list->next->ch);
  148.             printf("Roll: %s\n", list->next->a);
  149.         }
  150.     }
  151.     printf("1. Menu\n");
  152.     printf("2. Exit\n");
  153.     printf("Choose an option:\n");
  154.     int y;
  155.     scanf("%d",&y);
  156.     switch(y)
  157.     {
  158.     case 1:
  159.     {
  160.         menu();
  161.         break;
  162.     }
  163.     case 2:
  164.     {
  165.         exit(0);
  166.         break;
  167.     }
  168.     default :
  169.     {
  170.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  171.         menu();
  172.         break;
  173.     }
  174.     }
  175. }
  176.  
  177. void search_by_value()
  178. {
  179.     int d;
  180.     printf("Enter the Roll:\n");
  181.     scanf("%d", &d);
  182.     node *list=head;
  183.     if(list->a==d)
  184.     {
  185.         printf("Name: %s\n", list->ch);
  186.         printf("Roll: %s\n\n", list->a);
  187.     }
  188.     else
  189.     {
  190.         while(list->a!=d)
  191.         {
  192.             list=list->next;
  193.             if(list==NULL)
  194.             {
  195.                 printf("There's No Information!!\n");
  196.                 break;
  197.             }
  198.         }
  199.         printf("Name: %s\n", list->ch);
  200.         printf("Roll: %s\n", list->a);
  201.     }
  202.     printf("1. Menu\n");
  203.     printf("2. Exit\n");
  204.     printf("Choose an option:\n");
  205.     int x;
  206.     scanf("%d", &x);
  207.     switch(x)
  208.     {
  209.     case 1:
  210.     {
  211.         menu();
  212.         break;
  213.     }
  214.     case 2:
  215.     {
  216.         exit(0);
  217.         break;
  218.     }
  219.     default :
  220.     {
  221.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  222.         menu();
  223.         break;
  224.     }
  225.     }
  226. }
  227.  
  228. void search()
  229. {
  230.     printf("1. Search by Value\n");
  231.     printf("2. Search by Position\n");
  232.     printf("Choose an option:\n");
  233.     int x;
  234.     scanf("%d", &x);
  235.     switch(x)
  236.     {
  237.     case 1:
  238.     {
  239.         search_by_value();
  240.         break;
  241.     }
  242.     case 2:
  243.     {
  244.         search_by_pos();
  245.         break;
  246.     }
  247.     case 3:
  248.     {
  249.         exit(0);
  250.         break;
  251.     }
  252.     default :
  253.     {
  254.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  255.         menu();
  256.         break;
  257.     }
  258.     }
  259. }
  260.  
  261. void delete_by_pos()
  262. {
  263.  
  264.     int n;
  265.     node *list=head, *temp;
  266.     printf("Enter the Position:\n");
  267.     scanf(" %d", &n);
  268.     if(head==NULL)
  269.     {
  270.         printf("There's No Information!!\n");
  271.     }
  272.     else if(n==1)
  273.     {
  274.         if(list->next==NULL)
  275.         {
  276.             printf("The data has been deleted!!\n");
  277.             free(list);
  278.         }
  279.         else
  280.         {
  281.             head=list->next;
  282.             head->prev=NULL;
  283.             printf("The data has been deleted!!\n");
  284.             free(list);
  285.         }
  286.     }
  287.     else
  288.     {
  289.         n=n-2;
  290.         while(n!=0 && list->next!=NULL)
  291.         {
  292.             list=list->next;
  293.             n--;
  294.             if(list==NULL)
  295.             {
  296.                 printf("There's no data found!!\n");
  297.                 break;
  298.             }
  299.         }
  300.         if(list->next==NULL)
  301.         {
  302.             printf("You've Chosen the Wrong Position!!\n");
  303.         }
  304.         else if(list->next->next==NULL && n==0)
  305.         {
  306.             temp=tail;
  307.             tail=tail->prev;
  308.             tail->next=NULL;
  309.             printf("The data has been deleted!!\n");
  310.             free(temp);
  311.         }
  312.         else if(list->next->next!=NULL && n==0)
  313.         {
  314.             temp=list->next;
  315.             temp->next->prev=list;
  316.             list->next=temp->next;
  317.             printf("The data has been deleted!!\n");
  318.             free(temp);
  319.         }
  320.     }
  321.     printf("1. Menu\n");
  322.     printf("2. Exit\n");
  323.     printf("Choose an option:\n");
  324.     int x;
  325.     scanf("%d", &x);
  326.     switch(x)
  327.     {
  328.     case 1:
  329.     {
  330.         menu();
  331.         break;
  332.     }
  333.     case 2:
  334.     {
  335.         exit(0);
  336.         break;
  337.     }
  338.  
  339.     default :
  340.     {
  341.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  342.         menu();
  343.         break;
  344.     }
  345.     }
  346. }
  347.  
  348.  
  349. void  delete_the_value()
  350. {
  351.     int n;
  352.     printf("Enter the Roll:\n");
  353.     scanf(" %d", &n);
  354.     node*list=head;
  355.     if(head==NULL)
  356.     {
  357.         printf("There's No Information!!\n");
  358.     }
  359.     else if(list->a==n)
  360.     {
  361.         if(list->next==NULL)
  362.         {
  363.             head=NULL;
  364.             tail=NULL;
  365.             printf("The data has been deleted!!\n");
  366.             free(list);
  367.         }
  368.         else
  369.         {
  370.             list->next->prev=NULL;
  371.             head=list->next;
  372.             printf("The data has been deleted!!\n");
  373.             free(list);
  374.         }
  375.     }
  376.     else
  377.     {
  378.         node*temp;
  379.         while(list->a!=n && list->next!=NULL)
  380.         {
  381.             list=list->next;
  382.             if(list==NULL)
  383.             {
  384.                 printf("There's no data found!!\n");
  385.                 break;
  386.             }
  387.         }
  388.         if(list->a==n && list->next==NULL)
  389.         {
  390.             list=tail;
  391.             temp=list;
  392.             temp->prev->next=temp->next;
  393.             tail=temp->prev;
  394.             printf("The data has been deleted!!\n");
  395.             free(temp);
  396.         }
  397.         else if(list->a==n && list->next!=NULL)
  398.         {
  399.             temp=list;
  400.             temp->prev->next=temp->next;
  401.             temp->next->prev=temp->prev;
  402.             temp->next=NULL;
  403.             temp->prev=NULL;
  404.             printf("The data has been deleted!!\n");
  405.             free(temp);
  406.         }
  407.     }
  408.     printf("1. Menu\n");
  409.     printf("2. Exit\n");
  410.     printf("Choose an option:\n");
  411.     int x;
  412.     scanf("%d", &x);
  413.     switch(x)
  414.     {
  415.     case 1:
  416.     {
  417.         menu();
  418.         break;
  419.     }
  420.     case 2:
  421.     {
  422.         exit(0);
  423.         break;
  424.     }
  425.     default :
  426.     {
  427.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  428.         menu();
  429.         break;
  430.     }
  431.     }
  432. }
  433.  
  434. void Delete()
  435. {
  436.     printf("1. Delete by value\n");
  437.     printf("2. Delete by position\n");
  438.     printf("Choose a option:");
  439.     int x;
  440.     scanf("%d", &x);
  441.     switch(x)
  442.     {
  443.     case 1:
  444.     {
  445.         delete_the_value();
  446.         break;
  447.     }
  448.     case 2:
  449.     {
  450.         delete_by_pos();
  451.         break;
  452.     }
  453.     case 3:
  454.     {
  455.         exit(0);
  456.         break;
  457.     }
  458.  
  459.     default :
  460.     {
  461.  
  462.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  463.         menu();
  464.         break;
  465.     }
  466.     }
  467.  
  468. }
  469.  
  470. void insert_at_pos()
  471. {
  472.     int n;
  473.     node *list=head;
  474.     node *N=(node*)malloc(sizeof(node));
  475.     printf("Enter the Name:\n");
  476.     scanf(" %[^\n]", N->ch);
  477.     printf("Enter the Roll:\n");
  478.     scanf(" %[^\n]", N->a);
  479.     N->next=NULL;
  480.     N->prev=NULL;
  481.     printf("Enter the position:\n");
  482.     scanf(" %d", &n);
  483.     if(n==1)
  484.     {
  485.         if(head==NULL)
  486.         {
  487.             head=N;
  488.             tail=N;
  489.         }
  490.         else
  491.         {
  492.             head->prev=N;
  493.             N->next=head;
  494.             head=N;
  495.         }
  496.     }
  497.     else
  498.     {
  499.         n=n-2;
  500.         while(n!=0 && list->next!=NULL)
  501.         {
  502.             list=list->next;
  503.             n--;
  504.         }
  505.         if(list->next==NULL)
  506.         {
  507.             N->prev=tail;
  508.             tail->next=N;
  509.             tail=N;
  510.         }
  511.         else
  512.         {
  513.             list->next->prev=N;
  514.             N->next=list->next;
  515.             N->prev=list;
  516.             list->next=N;
  517.         }
  518.     }
  519.     printf("1. Menu\n");
  520.     printf("2. Exit\n");
  521.     printf("Choose an option:\n");
  522.     int x;
  523.     scanf("%d", &x);
  524.     switch(x)
  525.     {
  526.     case 1:
  527.     {
  528.         menu();
  529.         break;
  530.     }
  531.     case 2:
  532.     {
  533.         exit(0);
  534.         break;
  535.     }
  536.     default :
  537.     {
  538.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  539.         menu();
  540.         break;
  541.     }
  542.     }
  543. }
  544. void insert_end()
  545. {
  546.     node *N=(node*)malloc(sizeof(node));
  547.     printf("Enter the Name:\n");
  548.     scanf(" %[^\n]", N->ch);
  549.     printf("Enter the Roll:\n");
  550.     scanf(" %[^\n]", N->a);
  551.     N->next=NULL;
  552.     N->prev=NULL;
  553.     node *list=head;
  554.     if(head==NULL && tail==NULL)
  555.     {
  556.         head=N;
  557.         tail=N;
  558.     }
  559.     else
  560.     {
  561.         tail->next=N;
  562.         N->prev=tail;
  563.         tail=N;
  564.     }
  565.     printf("1. Menu\n");
  566.     printf("2. Exit\n");
  567.     printf("Choose an option:\n");
  568.     int x;
  569.     scanf("%d", &x);
  570.     switch(x)
  571.     {
  572.     case 1:
  573.     {
  574.         menu();
  575.         break;
  576.     }
  577.     case 2:
  578.     {
  579.         exit(0);
  580.         break;
  581.     }
  582.     default :
  583.     {
  584.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  585.         menu();
  586.         break;
  587.     }
  588.     }
  589. }
  590.  
  591. void insert_first()
  592. {
  593.     node *N=(node*)malloc(sizeof(node));
  594.     printf("Enter the Name:\n");
  595.     scanf(" %[^\n]", N->ch);
  596.     printf("Enter the Roll:\n");
  597.    scanf(" %[^\n]", N->a);
  598.     N->next=NULL;
  599.     N->prev=NULL;
  600.     if(head==NULL && tail==NULL)
  601.     {
  602.         head=N;
  603.         tail=N;
  604.     }
  605.     else
  606.     {
  607.         head->prev=N;
  608.         N->next=head;
  609.         head=N;
  610.  
  611.     }
  612.     printf("1. Menu\n");
  613.     printf("2. Exit\n");
  614.     printf("Choose an option:\n");
  615.     int x;
  616.     scanf("%d", &x);
  617.     switch(x)
  618.     {
  619.     case 1:
  620.     {
  621.         menu();
  622.         break;
  623.     }
  624.     case 2:
  625.     {
  626.         exit(0);
  627.         break;
  628.     }
  629.     default :
  630.     {
  631.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  632.         menu();
  633.         break;
  634.     }
  635.     }
  636. }
  637. void add()
  638. {
  639.     printf("1.Add at frist\n");
  640.     printf("2.Add at end\n");
  641.     printf("3.Add by position\n");
  642.     printf("4.Exit\n");
  643.     printf("Choose an option:\n");
  644.     int x;
  645.     scanf("%d", &x);
  646.     switch(x)
  647.     {
  648.     case 1:
  649.     {
  650.         insert_first();
  651.         break;
  652.     }
  653.     case 2:
  654.     {
  655.         insert_end();
  656.         break;
  657.     }
  658.     case 3:
  659.     {
  660.         insert_at_pos();
  661.         break;
  662.     }
  663.     case 4:
  664.     {
  665.         exit(0);
  666.         break;
  667.     }
  668.     default :
  669.     {
  670.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  671.         menu();
  672.         break;
  673.     }
  674.     }
  675. }
  676. void menu()
  677. {
  678.     printf("1.Add\n");
  679.     printf("2.Delete\n");
  680.     printf("3.Search\n");
  681.     printf("4.Display\n");
  682.     printf("5.Exit\n");
  683.     printf("Choose an option:\n");
  684.     int y;
  685.     scanf("%d", &y);
  686.     switch(y)
  687.     {
  688.     case 1:
  689.     {
  690.         add();
  691.         break;
  692.     }
  693.     case 2:
  694.     {
  695.         Delete();
  696.         break;
  697.     }
  698.     case 3:
  699.     {
  700.         search();
  701.         break;
  702.     }
  703.     case 4:
  704.     {
  705.         display();
  706.         break;
  707.     }
  708.     case 5:
  709.     {
  710.         exit(0);
  711.         break;
  712.     }
  713.     default :
  714.     {
  715.         printf("You've chosen a wrong option!! Choose the correct one:\n");
  716.         menu();
  717.         break;
  718.     }
  719.     }
  720. }
  721. int main()
  722. {
  723.     printf("..........!!Doubly Link list!!........\n");
  724.     menu();
  725.     return 0;
  726. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement