Advertisement
rony-Rony_05

del pos

Oct 12th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 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_pos(int pos)
  56. {
  57.     node *list=head,*temp=NULL,*tail;
  58.  
  59.  
  60.     if(pos==1)
  61.     {
  62.         if(list->next==NULL)
  63.         {
  64.             printf("DATA DELETED\n\n");
  65.             free(list);
  66.         }
  67.         else
  68.         {
  69.             head=list->next;
  70.             head->previous=NULL;
  71.             printf("DATA DELETED\n\n");
  72.             free(list);
  73.         }
  74.     }
  75.     else
  76.     {
  77.         pos=pos-2;
  78.         while(pos!=0 && list->next!=NULL)
  79.         {
  80.             list=list->next;
  81.             pos--;
  82.             if(list==NULL)
  83.             {
  84.                 printf("NO DATA FOUND!!\n\n");
  85.                 break;
  86.             }
  87.         }
  88.         if(list->next==NULL)
  89.         {
  90.             printf("You Choose Wrong Position!!\n\n");
  91.         }
  92.         else if(list==NULL && pos==0)
  93.         {
  94.             temp=tail;
  95.             tail=temp->previous;
  96.             tail->next=NULL;
  97.             printf("DATA DELETED\n\n");
  98.             free(temp);
  99.         }
  100.         else if(list!=NULL && pos==0)
  101.         {
  102.             temp=list->next;
  103.             temp->next->previous=list;
  104.             list->next=temp->next;
  105.             printf("DATA DELETED\n\n");
  106.             free(temp);
  107.         }
  108.    }
  109. }
  110.  
  111. int main()
  112. {
  113.     head=NULL;
  114.     int x,af,l;
  115.     char cf;
  116.     insert_at_end(1,'A');
  117.     insert_at_end(3,'b');
  118.     insert_at_end(5,'c');
  119.     insert_at_end(7,'d');
  120.     insert_at_end(9,'e');
  121.     printf("position:");
  122.     scanf("%d",&x);
  123.      delete_pos(x);
  124.     display();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement