Advertisement
Guest User

MUKHERJEE's First linked list

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