Advertisement
SAADQUAMER

Untitled

Oct 11th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 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.     struct node*previous;
  10.  
  11. } node;
  12.  
  13. node *head;
  14. void display()
  15. {
  16.  
  17.     node *list=head;
  18.     while(list !=NULL)
  19.     {
  20.         printf("\n\nA= %d ,C= %c\n",list->a,list->ch);
  21.  
  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.  
  56. void delete_pos(int pos)
  57. {
  58.     node *list=head,*temp=NULL;
  59.     int x=0;
  60.     if(pos==1)
  61.     {
  62.         head=list->next;
  63.         head->previous=NULL;
  64.         free(list);
  65.         return;
  66.     }
  67.     else
  68.     {
  69.         pos=pos-2;
  70.         while(pos!=0)
  71.         {
  72.             list=list->next;
  73.             pos--;
  74.             if(list==NULL)
  75.             {return;}
  76.             else{
  77.                 temp=list;
  78.  
  79.                 list->previous->next=NULL;
  80.                 list=list->previous;
  81.                 free(temp);
  82.             }
  83.  
  84.         }
  85.         temp=list->next;
  86.         list->next=temp->next;
  87.         temp->next->previous=list;
  88.  
  89.         free(temp);
  90.         x=1;
  91.     }
  92.  
  93.     if(x==1)
  94.     {
  95.         printf("KEY IS DELETED \n\n");
  96.     }
  97. }
  98. int main()
  99. {
  100.     head=NULL;
  101.     insert_at_end(5,'p');
  102.     insert_at_end(4,'r');
  103.     insert_at_end(2,'u');
  104.     delete_pos(3);
  105.  
  106.     display();
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement