Advertisement
Zahidx

S

Oct 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.53 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.     int a;
  7.     char ch;
  8.     struct node *next;
  9.  
  10. } node;
  11.  
  12. node *head;
  13.  
  14. void display()
  15. {
  16.  
  17.     node *list=head;
  18.  
  19.     if(head==NULL)
  20.     {
  21.         printf("\nNode is empty.\n\n");
  22.         printf("Enter your data first...\n");
  23.         MENU();
  24.  
  25.     }
  26.     while(list !=NULL)
  27.     {
  28.         printf("\n\nNum= %d ,Chr= %c\n",list->a,list->ch);
  29.  
  30.  
  31.         list=list->next;
  32.     }
  33. }
  34.  
  35. void create()
  36. {
  37.     int value;
  38.     char ch;
  39.     while(1)
  40.     {
  41.         printf("   \nEnter a integer Number & Character (int,char) (-1 for exit)\n");
  42.         printf("\n");
  43.         scanf("%d",&value);
  44.         if(value==-1)
  45.         {
  46.             display();
  47.             printf("\n\nData Saved\n\n");
  48.             MENU();
  49.         }
  50.         scanf(" %c",&ch);
  51.  
  52.  
  53.         insert_end(value,ch);
  54.  
  55.     }
  56.  
  57. }
  58. void insert_first(int aN,char chN)
  59. {
  60.     node*p=(node*)malloc(sizeof(node));
  61.     p->a=aN;
  62.     p->ch=chN;
  63.     p->next=NULL;
  64.     if(head==NULL)
  65.     {
  66.         head=p;
  67.         return;
  68.     }
  69.     else
  70.     {
  71.         p->next=head;
  72.         head=p;
  73.     }
  74. }
  75.  
  76. void insert_nth(int n,int aN,char chN)
  77. {
  78.     node *N=(node*)malloc(sizeof(node));
  79.     N->a=aN;
  80.     N->ch=chN;
  81.     N->next=NULL;
  82.     if(n==1)
  83.     {
  84.         N->next=head;
  85.         head=N;
  86.         return;
  87.     }
  88.     else
  89.     {
  90.         node*list=head;
  91.         n=n-2;
  92.         while(n!=0&&list->next!=NULL)
  93.         {
  94.             list=list->next;
  95.             n--;
  96.         }
  97.         N->next=list->next;
  98.         list->next=N;
  99.     }
  100. }
  101.  
  102. void insert_end(int aN,char chN)
  103. {
  104.     node *N=(node*)malloc(sizeof(node));
  105.     N->a=aN;
  106.     N->ch=chN;
  107.     N->next=NULL;
  108.     node *list=head;
  109.     if(head==NULL)
  110.     {
  111.         head=N;
  112.         list=N;
  113.     }
  114.     else
  115.     {
  116.         while(list->next!=NULL)
  117.         {
  118.             list=list->next;
  119.         }
  120.         list->next=N;
  121.     }
  122. }
  123.  
  124. int search()
  125. {
  126.     int n;
  127.     printf("   \nSearch Value\n\n");
  128.     printf("Enter any value for search:");
  129.     scanf("%d",&n);
  130.  
  131.     node*list=head;
  132.     int x=0;
  133.     while(list!=NULL)
  134.     {
  135.         if(list->a==n)
  136.         {
  137.             printf("\n%d is Found\n\n",n);
  138.             x=1;
  139.             break;
  140.         }
  141.         else
  142.  
  143.             list=list->next;
  144.     }
  145.     if(x==0)
  146.     {
  147.         printf("\n%d is  Not Found\n\n",n);
  148.     }
  149.     MENU();
  150. }
  151.  
  152. void addnode()
  153. {
  154.     int af,y;
  155.     char cf;
  156.     printf("\n\n\n");
  157.     printf("1.Insert at First.\n");
  158.     printf("2.Insert at Last.\n");
  159.     printf("3.Insert at Nth Position.\n");
  160.     printf("4.Return to Menu\n");
  161.     printf("\nChoose Any Option (1-4):");
  162.     int x;
  163.  
  164.     scanf("%d",&x);
  165.     switch(x)
  166.     {
  167.     case 1:
  168.     {
  169.         printf("\nInsert at First(int,char)\n");
  170.  
  171.         printf("\n");
  172.         scanf("%d",&af);
  173.         scanf(" %c",&cf);
  174.         insert_first(af,cf);
  175.         display();
  176.         addnode();
  177.  
  178.     }
  179.     case 2:
  180.     {
  181.         printf("\nInsert at Last(int,char)\n");
  182.  
  183.  
  184.         printf("\n");
  185.         scanf("%d",&af);
  186.         scanf(" %c",&cf);
  187.         insert_end(af,cf);
  188.         display();
  189.         addnode();
  190.     }
  191.     case 3:
  192.     {
  193.         printf("\nInsert at Nth Position(pos,int,char)\n");
  194.  
  195.         printf("\n");
  196.         scanf("%d",&y);
  197.         scanf("%d",&af);
  198.         scanf(" %c",&cf);
  199.         insert_nth(y,af,cf);
  200.         display();
  201.         addnode();
  202.     }
  203.     case 4:
  204.     {
  205.         MENU();
  206.         break;
  207.     }
  208.     default:
  209.     {
  210.         printf("Error!\nChoose Right Option\n");
  211.         addnode();
  212.     }
  213.     }
  214. }
  215.  
  216. void delete()
  217. {
  218.  
  219.     printf("\nDelete Node\n");
  220.     printf("1.Delete by Position \n");
  221.     printf("2.Delete by Value \n");
  222.     printf("3.Return to Menu \n\n");
  223.     printf("Choose Any Option (1-3):");
  224.     int x,s,l;
  225.  
  226.  
  227.     scanf("%d",&x);
  228.     switch(x)
  229.     {
  230.     case 1:
  231.     {
  232.         printf("   \nDelete by Position\n");
  233.         printf("\n");
  234.         printf("Enter Position:");
  235.         scanf("%d",&s);
  236.         delete_pos(s);
  237.         delete();
  238.     }
  239.     case 2:
  240.     {
  241.         printf("   \nDelete by Value\n");
  242.         printf("\n");
  243.         printf("Enter Value:");
  244.         scanf("%d",&l);
  245.         delete_item(l);
  246.         delete();
  247.     }
  248.     case 3:
  249.     {
  250.         MENU();
  251.         break;
  252.     }
  253.     default:
  254.     {
  255.         printf("Error!\nChoose Right Option\n");
  256.         delete();
  257.     }
  258.     }
  259. }
  260.  
  261. void delete_pos(int pos)
  262. {
  263.     node *list=head,*temp=NULL;
  264.     int x=0;
  265.     if(pos==1)
  266.     {
  267.         head=head->next;
  268.         free(list);
  269.         return;
  270.     }
  271.     pos=pos-2;
  272.     while(pos!=0)
  273.     {
  274.         list=list->next;
  275.         pos--;
  276.         if(list==NULL)
  277.             return;
  278.     }
  279.     temp=list->next;
  280.     list->next=temp->next;
  281.     x=1;
  282.     free(temp);
  283.     if(x==1)
  284.     {
  285.         printf("Key is deleted\n\n");
  286.     }
  287.     else
  288.     {
  289.         printf("Key is not found\n");
  290.     }
  291. }
  292.  
  293. void delete_item(int value)
  294. {
  295.     node *list = head, *temp=NULL;
  296.     int x = 0;
  297.  
  298.     while(list!=NULL)
  299.     {
  300.         if(list->a==value)
  301.         {
  302.             if(temp==NULL)
  303.                 head = list->next;
  304.             else
  305.                 temp->next = list->next;
  306.  
  307.             printf("%d is deleted from list\n", value);
  308.             x = 1;
  309.             free(list);
  310.             break;
  311.         }
  312.         temp = list;
  313.         list = list->next;
  314.     }
  315.     if(x==0)
  316.         printf("Key not found!\n");
  317. }
  318.  
  319. void MENU()
  320. {
  321.  
  322.     printf("\n\nSingly Linked List\n\n");
  323.     printf("1.Create Nodes\n");
  324.     printf("2.Add Nodes\n");
  325.     printf("3.Search Nodes\n");
  326.     printf("4.Delete Nodes\n");
  327.     printf("5.Display\n");
  328.     printf("6.Exit \n\n\n");
  329.  
  330.     printf("Choose Any Option (1-5):");
  331.     int x;
  332.  
  333.  
  334.     scanf("%d",&x);
  335.     switch(x)
  336.     {
  337.     case 1:
  338.     {
  339.         create();
  340.         break;
  341.     }
  342.     case 2:
  343.     {
  344.         addnode();
  345.         break;
  346.     }
  347.     case 3:
  348.     {
  349.         search();
  350.         break;
  351.     }
  352.     case 4:
  353.     {
  354.         delete();
  355.         break;
  356.     }
  357.     case 5:
  358.     {
  359.         dis();
  360.         break;
  361.     }
  362.     case 6:
  363.     {
  364.         break;
  365.     }
  366.     default:
  367.     {
  368.         printf("Error!\nChoose Right Option\n");
  369.         MENU();
  370.     }
  371.     }
  372. }
  373.  
  374. void dis()
  375. {
  376.  
  377.     node *list=head;
  378.  
  379.     if(head==NULL)
  380.     {
  381.         printf("\nNode is empty.\n");
  382.         printf("Enter your data first...\n");
  383.         MENU();
  384.  
  385.     }
  386.     while(list !=NULL)
  387.     {
  388.         printf("\n\nNum= %d ,Chr= %c\n",list->a,list->ch);
  389.  
  390.  
  391.         list=list->next;
  392.     }
  393.  
  394.     MENU();
  395. }
  396.  
  397. int main()
  398. {
  399.     head=NULL;
  400.     MENU();
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement