SHARE
TWEET

delete by position

a guest Oct 13th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct Node {
  5.     int data;
  6.     struct Node *next, *prev;
  7.  
  8. }node;
  9.  
  10. node* head=NULL, *tail;
  11.  
  12. void insert_end(int data)
  13. {
  14.     node *newnode;
  15.     newnode=(node*)malloc(sizeof(node));
  16.     newnode->data=data;
  17.     newnode->next=NULL;
  18.     newnode->prev=NULL;
  19.  
  20.     if(head==NULL) {
  21.         head=newnode;
  22.         tail=newnode;
  23.     }
  24.     else {
  25.         node* current=head;
  26.         while(current->next!=NULL) {
  27.             current=current->next;
  28.         }
  29.         current->next=newnode;
  30.         newnode->prev=current;
  31.         tail=newnode;
  32.     }
  33. }
  34.  
  35. void delele_by_position()
  36. {
  37.     node*current=head;
  38.     node*previous;
  39.     int cnt=1,n;
  40.     printf("Enter the position: ");
  41.     scanf("%d", &n);
  42.     if(n==1) {
  43.         head=head->next;
  44.         head->prev=NULL;
  45.         free(current);
  46.     }
  47.     else {
  48.         node*current=head;
  49.         while(cnt<n) {
  50.             previous=current;
  51.             current=current->next;
  52.             cnt++;
  53.         }
  54.         if(current->next == NULL) {
  55.             previous->next = NULL;
  56.             tail=previous;
  57.             free(current);
  58.         }
  59.         else {
  60.             previous->next=current->next;
  61.             current->next->prev=previous;
  62.             free(current);
  63.         }
  64.     }
  65. }
  66.  
  67. void printData()
  68. {
  69.     node*current=head;
  70.     printf("New list is: ");
  71.     while(current!=NULL) {
  72.         printf("%d ", current->data);
  73.         current=current->next;
  74.     }
  75.     printf("\n");
  76. }
  77.  
  78. void printData2()
  79. {
  80.     node*current=tail;
  81.     printf("New list is: ");
  82.     while(current!=NULL) {
  83.         printf("%d ", current->data);
  84.         current=current->prev;
  85.     }
  86.     printf("\n");
  87. }
  88.  
  89. int main()
  90. {
  91.     insert_end(5);
  92.     insert_end(10);
  93.     insert_end(15);
  94.     insert_end(20);
  95.     insert_end(25);
  96.     int i;
  97.     for(i=1; i<=5; i++) {
  98.         delele_by_position();
  99.         printData();
  100.         printData2();
  101.     }
  102.     return 0;
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top