Advertisement
Mostafiz543

Singly list

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