Advertisement
rony-Rony_05

del item

Oct 12th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.  
  7.     int a;
  8.     char ch;
  9.     struct node*next;
  10.     struct node*previous;
  11. } node;
  12. node*head;
  13. void display()
  14. {
  15.  
  16.     node *list=head;
  17.     while(list !=NULL)
  18.     {
  19.         printf("A: %d\n",list->a);
  20.         printf("C: %c\n",list->ch);
  21.         printf("\n");
  22.  
  23.         list=list->next;
  24.  
  25.     }
  26. }
  27. void insert_at_end(int aN,char chN)
  28. {
  29.  
  30.     node*N=(node*)malloc(sizeof(node));
  31.     N->a=aN;
  32.     N->ch=chN;
  33.     N->next=NULL;
  34.     N->previous=NULL;
  35.     node*list=head;
  36.     if(head==NULL)
  37.     {
  38.         head=N;
  39.     }
  40.     else
  41.     {
  42.         while(list->next!=NULL)
  43.         {
  44.             list=list->next;
  45.             N->previous=list;
  46.         }
  47.         list->next=N;
  48.         N->next=NULL;
  49.         N->previous=list;
  50.  
  51.  
  52.     }
  53.  
  54. }
  55.  void delete_item(int value)
  56. {
  57.     node *list = head, *temp=NULL;
  58.     int x = 0;
  59.  
  60.     while(list!=NULL)
  61.     {
  62.         if(list->a==value)
  63.         {
  64.             if(temp==NULL)
  65.             {
  66.                 head = list->next;
  67.                 head->previous=NULL;
  68.             }
  69.             else
  70.                 temp->next = list->next;
  71.  
  72.  
  73.             printf("%d is deleted from list\n", value);
  74.             x = 1;
  75.             free(list);
  76.             break;
  77.         }
  78.         temp = list;
  79.         list = list->next;
  80.         list->previous=temp;
  81.     }
  82.     if(x==0)
  83.         printf("Key not found!\n");
  84. }
  85. int main()
  86. {
  87.     head=NULL;
  88.     int x,af,l;
  89.     char cf;
  90.     insert_at_end(1,'A');
  91.     insert_at_end(3,'b');
  92.     insert_at_end(5,'c');
  93.     insert_at_end(7,'d');
  94.     insert_at_end(9,'e');
  95.     printf("position:");
  96.     scanf("%d",&x);
  97.      delete_item(x);
  98.     display();
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement