Advertisement
RifatBinIslam

SINGLE LINKED LIST (ASSIGNMENT)

Oct 15th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 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.  
  15.  
  16. void display()
  17. {
  18.  
  19.     node *list=head;
  20.     while(list !=NULL)
  21.     {
  22.         printf("\n\nA= %d ,C= %c\n",list->a,list->ch);
  23.  
  24.  
  25.         list=list->next;
  26.  
  27.     }
  28. }
  29.  
  30. void insert_end(int aN,char chN)
  31. {
  32.     node *N=(node*)malloc(sizeof(node));
  33.     N->a=aN;
  34.     N->ch=chN;
  35.     N->next=NULL;
  36.     node *list=head;
  37.     if(head==NULL)
  38.     {
  39.         head=N;
  40.         list=N;
  41.     }
  42.     else
  43.     {
  44.         while(list->next!=NULL)
  45.         {
  46.             list=list->next;
  47.         }
  48.         list->next=N;
  49.  
  50.     }
  51.  
  52. }
  53. void insert_nth(int n,int aN,char chN)
  54. {
  55.     node *N=(node*)malloc(sizeof(node));
  56.     N->a=aN;
  57.     N->ch=chN;
  58.     N->next=NULL;
  59.     if(n==1)
  60.     {
  61.         N->next=head;
  62.         head=N;
  63.         return;
  64.     }
  65.     else
  66.     {
  67.         node*list=head;
  68.         n=n-2;
  69.         while(n!=0&&list->next!=NULL)
  70.         {
  71.             list=list->next;
  72.             n--;
  73.         }
  74.         N->next=list->next;
  75.         list->next=N;
  76.     }
  77. }
  78. int search(int n)
  79. {
  80.     node*list=head;
  81.     int x=0;
  82.     while(list!=NULL)
  83.     {
  84.         if(list->a==n)
  85.         {
  86.             printf("\n%d is Found\n\n",n);
  87.             x=1;
  88.             break;
  89.         }
  90.         else
  91.  
  92.             list=list->next;
  93.     }
  94.     if(x==0)
  95.     {
  96.         printf("\n%d is  Not Found\n\n",n);
  97.     }
  98. }
  99.  
  100. void insert_first(int aN,char chN)
  101. {
  102.     node*p=(node*)malloc(sizeof(node));
  103.     p->a=aN;
  104.     p->ch=chN;
  105.     p->next=NULL;
  106.     if(head==NULL)
  107.     {
  108.         head=p;
  109.         return;
  110.     }
  111.     else
  112.     {
  113.         p->next=head;
  114.         head=p;
  115.     }
  116. }
  117.  
  118. void delete_item(int value)
  119. {
  120.     node *list = head, *temp=NULL;
  121.     int x = 0;
  122.  
  123.     while(list!=NULL)
  124.     {
  125.         if(list->a==value)
  126.         {
  127.             if(temp==NULL)
  128.                 head = list->next;
  129.             else
  130.                 temp->next = list->next;
  131.  
  132.             printf("%d is deleted from list\n", value);
  133.             x = 1;
  134.             free(list);
  135.             break;
  136.         }
  137.         temp = list;
  138.         list = list->next;
  139.     }
  140.     if(x==0)
  141.         printf("Key not found!\n");
  142. }
  143.  
  144.  
  145. void delete_pos(int pos)
  146. {
  147.     node *list=head,*temp=NULL;
  148.     int x=0;
  149.     if(pos==1)
  150.     {
  151.         head=head->next;
  152.         free(list);
  153.         return;
  154.     }
  155.     pos=pos-2;
  156.     while(pos!=0)
  157.     {
  158.         list=list->next;
  159.         pos--;
  160.         if(list==NULL)
  161.             return;
  162.     }
  163.     temp=list->next;
  164.     list->next=temp->next;
  165.     x=1;
  166.     free(temp);
  167.     if(x==1)
  168.     {
  169.         printf("KEY IS DELETED \n\n");
  170.     }
  171. }
  172.  
  173.  
  174. void create()
  175. {
  176.     int value;
  177.     char ch;
  178.     while(1)
  179.     {
  180.         printf("   \n\n Enter  Number and Character(-0 for exit)\n\n");
  181.         scanf("%d",&value);
  182.         if(value==-0)
  183.         {
  184.             break;
  185.         }
  186.         scanf(" %c",&ch);
  187.  
  188.  
  189.         insert_end(value,ch);
  190.  
  191.     }
  192.  
  193.  
  194. }
  195.  
  196.  
  197.  
  198. int main()
  199. {
  200.     head=NULL;
  201.     int x,af,l;
  202.     char cf;
  203.  
  204.     printf("   \n\n Create Linked List \n\n");
  205.     create();
  206.     display();
  207.     printf("   \n\n Insert At First  \n\n");
  208.     scanf("%d",&af);
  209.     scanf(" %c",&cf);
  210.     insert_first(af,cf);
  211.     display();
  212.     printf("   \n\n Insert At End  \n\n");
  213.     scanf("%d",&af);
  214.     scanf(" %c",&cf);
  215.     insert_end(af,cf);
  216.     display();
  217.  
  218.     printf("   \n\n Insert At Nth(pos,int,char) \n\n");
  219.     scanf("%d",&x);
  220.     scanf("%d",&af);
  221.     scanf(" %c",&cf);
  222.     insert_nth(x,af,cf);
  223.     display();
  224.  
  225.     printf("   \n\n Delete By Position  \n\n");
  226.     scanf("%d",&x);
  227.     delete_pos(x);
  228.     display();
  229.  
  230.     printf("   \n\n Search Value \n\n");
  231.     scanf("%d",&l);
  232.     search(l);
  233.  
  234.     printf("   \n\n Delete By Value \n\n");
  235.     scanf("%d",&l);
  236.     delete_item(l);
  237.     printf("   \n\n Final value \n\n");
  238.     display();
  239.     return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement