Advertisement
dsdeep

Tazbid Doubly

Oct 7th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct linkList
  5. {
  6.     int data;
  7.     struct linkList *next;
  8.     struct linkList *prev;
  9. } node;
  10. node *last;
  11. void createlinklist(node *t,int n)
  12. {
  13.     int i, data;
  14.     if(n >= 1)
  15.     {
  16.         printf("Input data for node-1 : ");
  17.         scanf("%d", &data);
  18.  
  19.         t->data = data;
  20.         t->next = NULL;
  21.         t->prev = NULL;
  22.  
  23.         for(i=2; i<=n; i++)
  24.         {
  25.             printf("Input data for node-%d : ", i);
  26.             scanf("%d", &data);
  27.             node *newnode = (node*)malloc(sizeof(node));
  28.  
  29.             newnode->data = data;
  30.             newnode->next = NULL;
  31.             newnode->prev = t;
  32.             t->next = newnode;
  33.             t = t->next;
  34.             if(i==n){
  35.                 last=t;
  36.                 printf("D = %p T = %p\n",last,t);
  37.             }
  38.         }
  39.     }
  40. }
  41.  
  42. void display(node *t)
  43. {
  44.     printf("\nPrint the link list form first to last: ");
  45.             printf("%d ", t->data);
  46.     while (t->next!= NULL)
  47.     {
  48.         t = t->next;
  49.         printf("%d ", t->data);
  50.     }
  51.  
  52.     printf("\nPrint the link list form last to first: ");
  53.      printf("%d ", last->data);
  54.     while (last->prev != NULL)
  55.     {
  56.         last = last->prev;
  57.         printf("%d ", last->data);
  58.  
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     int n;
  65.     node *first = (node*)malloc(sizeof(node));
  66.  
  67.     printf("How many node do you want to create? -  ");
  68.     scanf("%d", &n);
  69.  
  70.     createlinklist(first,n);
  71.     printf("Another First D = %d\n",first->data);
  72.     printf("Another Last D = %d",last->data);
  73.     display(first);
  74.  
  75.     return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement