SAADQUAMER

Untitled

Oct 11th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 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 delete_pos(int pos)
  54. {
  55.     node *list=head,*temp=NULL;
  56.     int x=0;
  57.     if(pos==1)
  58.     {
  59.         head=head->next;
  60.         free(list);
  61.         printf("KEY IS DELETED \n");
  62.         return;
  63.     }
  64.     pos=pos-2;
  65.     while(pos!=0)
  66.     {
  67.         list=list->next;
  68.         pos--;
  69.         if(list==NULL)
  70.             return;
  71.     }
  72.     temp=list->next;
  73.     list->next=temp->next;
  74.     x=1;
  75.     free(temp);
  76.     if(x==1)
  77.     {
  78.         printf("KEY IS DELETED \n\n");
  79.     }
  80. }
  81. int main()
  82. {int x;
  83.     head=NULL;
  84.     insert_end(5,'p');
  85.     insert_end(4,'r');
  86.     insert_end(2,'u');
  87. printf("ENTER POSITION FOR DELETE:");
  88. scanf("%d",&x);
  89. delete_pos(x);
  90.     display();
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment