SAADQUAMER

Singly Link List Assignment

Oct 13th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.77 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("\n\n\tNode is empty!!\n\n");
  22.         printf("\tENTER DATA PLEASE!!\n");
  23.         MENU();
  24.  
  25.     }
  26.     while(list !=NULL)
  27.     {
  28.         printf("\n\n\tA= %d ,C= %c\n",list->a,list->ch);
  29.  
  30.  
  31.         list=list->next;
  32.  
  33.     }
  34.  
  35. }
  36.  
  37.  
  38. void create()
  39. {
  40.     int value;
  41.     char ch;
  42.     while(1)
  43.     {
  44.         printf("   \n\tENTER  NUMBER & Character (int,char) (-1 for exit)\n");
  45.         printf("\t");
  46.         scanf("%d",&value);
  47.         if(value==-1)
  48.         {
  49.             display();
  50.             printf("\tNode Create Completed!!");
  51.             MENU();
  52.         }
  53.         scanf(" %c",&ch);
  54.  
  55.  
  56.         insert_end(value,ch);
  57.  
  58.     }
  59.  
  60.  
  61. }
  62. void insert_first(int aN,char chN)
  63. {
  64.     node*p=(node*)malloc(sizeof(node));
  65.     p->a=aN;
  66.     p->ch=chN;
  67.     p->next=NULL;
  68.     if(head==NULL)
  69.     {
  70.         head=p;
  71.         return;
  72.     }
  73.     else
  74.     {
  75.         p->next=head;
  76.         head=p;
  77.     }
  78. }
  79.  
  80.  
  81.  
  82. void insert_nth(int n,int aN,char chN)
  83. {
  84.     node *N=(node*)malloc(sizeof(node));
  85.     N->a=aN;
  86.     N->ch=chN;
  87.     N->next=NULL;
  88.     if(n==1)
  89.     {
  90.         N->next=head;
  91.         head=N;
  92.         return;
  93.     }
  94.     else
  95.     {
  96.         node*list=head;
  97.         n=n-2;
  98.         while(n!=0&&list->next!=NULL)
  99.         {
  100.             list=list->next;
  101.             n--;
  102.         }
  103.         N->next=list->next;
  104.         list->next=N;
  105.     }
  106. }
  107.  
  108.  
  109. void insert_end(int aN,char chN)
  110. {
  111.     node *N=(node*)malloc(sizeof(node));
  112.     N->a=aN;
  113.     N->ch=chN;
  114.     N->next=NULL;
  115.     node *list=head;
  116.     if(head==NULL)
  117.     {
  118.         head=N;
  119.         list=N;
  120.     }
  121.     else
  122.     {
  123.         while(list->next!=NULL)
  124.         {
  125.             list=list->next;
  126.         }
  127.         list->next=N;
  128.  
  129.     }
  130.  
  131. }
  132.  
  133. int search()
  134. {
  135.     int n;
  136.     printf("   \n\t<<<<<<<<SEARCH VALUE>>>>>>>>\n\n");
  137.     printf("\tENTER ANY VALUE FOR SERACH :");
  138.     scanf("%d",&n);
  139.  
  140.     node*list=head;
  141.     int x=0;
  142.     while(list!=NULL)
  143.     {
  144.         if(list->a==n)
  145.         {
  146.             printf("\n\t%d is Found\n\n",n);
  147.             x=1;
  148.             break;
  149.         }
  150.         else
  151.  
  152.             list=list->next;
  153.     }
  154.     if(x==0)
  155.     {
  156.         printf("\n\t%d is  Not Found\n\n",n);
  157.     }
  158.     MENU();
  159. }
  160.  
  161.  
  162.  
  163. void addnode()
  164. {
  165.     int af,y;
  166.     char cf;
  167.     printf("\n\n\n");
  168.     printf("\t1.Insert At First.\n");
  169.     printf("\t2.Insert At LAST.\n");
  170.     printf("\t3.Insert At Nth Position.\n");
  171.     printf("\t4.GO TO MENU\n");
  172.     printf("\n\tChoose Any Option (1-4):");
  173.     int x;
  174.  
  175.     scanf("%d",&x);
  176.     switch(x)
  177.     {
  178.     case 1:
  179.     {
  180.         printf("\n\t<<<<<<<<Insert At FIRST(int,char)>>>>>>>>\n");
  181.  
  182.         printf("\t");
  183.         scanf("%d",&af);
  184.         scanf(" %c",&cf);
  185.         insert_first(af,cf);
  186.         display();
  187.         addnode();
  188.  
  189.     }
  190.     case 2:
  191.     {
  192.         printf("\n\t<<<<<<<<Insert At END(int,char)>>>>>>>>\n");
  193.  
  194.  
  195.         printf("\t");
  196.         scanf("%d",&af);
  197.         scanf(" %c",&cf);
  198.         insert_end(af,cf);
  199.         display();
  200.         addnode();
  201.  
  202.     }
  203.     case 3:
  204.     {
  205.         printf("\n\t<<<<<<<<Insert At Nth(pos,int,char)>>>>>>>>\n");
  206.  
  207.         printf("\t");
  208.         scanf("%d",&y);
  209.         scanf("%d",&af);
  210.         scanf(" %c",&cf);
  211.         insert_nth(y,af,cf);
  212.         display();
  213.         addnode();
  214.  
  215.     }
  216.     case 4:
  217.     {
  218.         MENU();
  219.         break;
  220.     }
  221.     default:
  222.     {
  223.         printf("\tCHOOSE A CORRECT OPTION\n");
  224.         addnode();
  225.     }
  226.     }
  227.  
  228. }
  229.  
  230. void delete()
  231. {
  232.  
  233.     printf("\n\t<<<<<<<<<DELETE NODE>>>>>>>>>\n");
  234.     printf("\t1.Delete By Position \n");
  235.     printf("\t2.Delete By Value \n");
  236.     printf("\t3.GO TO MENU \n\n");
  237.     printf("\tChoose Any Option (1-3):");
  238.     int x,s,l;
  239.  
  240.  
  241.     scanf("%d",&x);
  242.     switch(x)
  243.     {
  244.     case 1:
  245.     {
  246.         printf("   \n\t<<<<<<<<DELETE BY POSITION>>>>>>>>\n");
  247.         printf("\t");
  248.         printf("ENTER POSITION:");
  249.         scanf("%d",&s);
  250.         delete_pos(s);
  251.         delete();
  252.     }
  253.     case 2:
  254.     {
  255.         printf("   \n\t<<<<<<<<DELETE BY VALUE>>>>>>>>\n");
  256.         printf("\t");
  257.         printf("ENTER VALUE:");
  258.         scanf("%d",&l);
  259.         delete_item(l);
  260.         delete();
  261.     }
  262.     case 3:
  263.     {
  264.         MENU();
  265.         break;
  266.     }
  267.     default:
  268.     {
  269.         printf("\tCHOOSE A CORRECT OPTION\n");
  270.         delete();
  271.     }
  272.     }
  273.  
  274.  
  275. }
  276.  
  277.  
  278.  
  279.  
  280. void delete_pos(int pos)
  281. {
  282.     node *list=head,*temp=NULL;
  283.     int x=0;
  284.     if(pos==1)
  285.     {
  286.         head=head->next;
  287.         free(list);
  288.         return;
  289.     }
  290.     pos=pos-2;
  291.     while(pos!=0)
  292.     {
  293.         list=list->next;
  294.         pos--;
  295.         if(list==NULL)
  296.             return;
  297.     }
  298.     temp=list->next;
  299.     list->next=temp->next;
  300.     x=1;
  301.     free(temp);
  302.     if(x==1)
  303.     {
  304.         printf("\tKEY IS DELETED \n\n");
  305.     }
  306.     else
  307.     {
  308.         printf("\tKEY IS NOT FOUND\n");
  309.     }
  310.  
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317. void delete_item(int value)
  318. {
  319.     node *list = head, *temp=NULL;
  320.     int x = 0;
  321.  
  322.     while(list!=NULL)
  323.     {
  324.         if(list->a==value)
  325.         {
  326.             if(temp==NULL)
  327.                 head = list->next;
  328.             else
  329.                 temp->next = list->next;
  330.  
  331.             printf("\t%d is deleted from list\n", value);
  332.             x = 1;
  333.             free(list);
  334.             break;
  335.         }
  336.         temp = list;
  337.         list = list->next;
  338.     }
  339.     if(x==0)
  340.         printf("Key not found!\n");
  341.  
  342. }
  343.  
  344.  
  345. void MENU()
  346. {
  347.  
  348.     printf("\n\n\t<<<<<<<<<<WELCOME TO SINGLY LINK LIST>>>>>>>>>>>>>\n\n");
  349.     printf("\t1.Create Nodes\n");
  350.     printf("\t2.ADD Nodes\n");
  351.     printf("\t3.Search Nodes\n");
  352.     printf("\t4.DELETE Nodes\n");
  353.     printf("\t5.Display\n");
  354.     printf("\t6.EXIT \n\n\n");
  355.  
  356.     printf("\tChoose Any Option (1-5):");
  357.     int x;
  358.  
  359.  
  360.     scanf("%d",&x);
  361.     switch(x)
  362.     {
  363.     case 1:
  364.     {
  365.         create();
  366.         break;
  367.     }
  368.     case 2:
  369.     {
  370.         addnode();
  371.         break;
  372.     }
  373.     case 3:
  374.     {
  375.         search();
  376.         break;
  377.     }
  378.     case 4:
  379.     {
  380.         delete();
  381.         break;
  382.     }
  383.     case 5:
  384.     {
  385.         dis();
  386.         break;
  387.     }
  388.     case 6:
  389.     {
  390.         break;
  391.     }
  392.     default:
  393.     {
  394.         printf("CHOOSE A CORRECT OPTION\n");
  395.         MENU();
  396.     }
  397.     }
  398. }
  399.  
  400. void dis()
  401. {
  402.  
  403.     node *list=head;
  404.  
  405.     if(head==NULL)
  406.     {
  407.         printf("\n\n\tNode is empty!!\n\n");
  408.         printf("\tENTER DATA PLEASE!!\n");
  409.         MENU();
  410.  
  411.     }
  412.     while(list !=NULL)
  413.     {
  414.         printf("\n\n\tA= %d ,C= %c\n",list->a,list->ch);
  415.  
  416.  
  417.         list=list->next;
  418.  
  419.  
  420.     }
  421.  
  422.     MENU();
  423. }
  424.  
  425. int main()
  426. {
  427.     head=NULL;
  428.     MENU();
  429.  
  430.  
  431. }
Advertisement
Add Comment
Please, Sign In to add comment