mhrabbi

Create and Display Doubly Linked List

Oct 25th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *prev,*next;
  8. }*head;
  9.  
  10. int main()
  11. {
  12.     int n;
  13.     printf("Enter Total Number Of Nodes: ");
  14.     scanf("%d",&n);
  15.     createList(n);
  16.  
  17.     printf("\Doubly Linked List is: ");
  18.     displayList();
  19.  
  20.     return 0;
  21.  
  22. }
  23. void createList(int n)
  24. {
  25.     int i,data;
  26.     struct node *temp,*p;
  27.  
  28.     temp=(struct node *)malloc(sizeof(struct node));
  29.  
  30.     printf("Enter data for node 1: ");
  31.     scanf("%d",&data);
  32.  
  33.     temp->data=data;
  34.     temp->prev=NULL;
  35.     temp->next=NULL;
  36.  
  37.     head=temp;
  38.     p=temp;
  39.  
  40.     for(i=2;i<=n;i++)
  41.     {
  42.         temp=(struct node *)malloc(sizeof(struct node));
  43.         printf("Enter data for node %d: ",i);
  44.         scanf("%d",&data);
  45.  
  46.         temp->data=data;
  47.         temp->next=NULL;
  48.         temp->prev=p;
  49.         p->next=temp;
  50.  
  51.         p=p->next;
  52.  
  53.     }
  54. }
  55. void displayList()
  56. {
  57.     struct node *temp;
  58.  
  59.     temp=head;
  60.  
  61.     while(temp != NULL)
  62.     {
  63.         printf("\n%d",temp->data);
  64.         temp=temp->next;
  65.     }
  66. }
Add Comment
Please, Sign In to add comment