Advertisement
rony-Rony_05

nth doubly

Oct 12th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 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 insert_nth(int n,int aN,char chN)
  56. {
  57.     node *N=(node*)malloc(sizeof(node));
  58.     N->a=aN;
  59.     N->ch=chN;
  60.     N->next=NULL;
  61.     N->previous=NULL;
  62.     if(n==1)
  63.     {
  64.         N->previous=NULL;
  65.         N->next=head;
  66.         head=N;
  67.  
  68.         return;
  69.     }
  70.     else
  71.     {
  72.         node*list=head;
  73.         n=n-2;
  74.         while(n!=0&&list->next!=NULL)
  75.         {
  76.             list=list->next;
  77.             n--;
  78.         }
  79.         N->next=list->next;
  80.         if(list->next !=NULL)
  81.         {
  82.             list->next->previous=N;
  83.         }
  84.  
  85.         list->next=N;
  86.     }
  87. }
  88. int main()
  89. {
  90.     head=NULL;
  91.     int x,af,l;
  92.     char cf;
  93.     insert_at_end(1,'A');
  94.     insert_at_end(3,'b');
  95.     insert_at_end(5,'c');
  96.     insert_at_end(7,'d');
  97.     insert_at_end(9,'e');
  98.     printf("position:");
  99.     scanf("%d",&x);
  100.     scanf("%d",&l);
  101.     scanf("%d",&af);
  102.     insert_nth(x,l,af);
  103.     display();
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement