SAADQUAMER

Untitled

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