Advertisement
rifat99

Link List

Feb 16th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node
  4. {
  5.     int x;
  6.     struct Node *ptr;
  7. }node;
  8.  
  9. node *head = NULL;
  10.   node *temp = NULL;
  11.  
  12. int main()
  13. {
  14.     int i,n;
  15.      printf("Input the number of nodes:",n);
  16.     scanf("%d",&n);
  17.  
  18.     for(i=1; i<=n; i++)
  19.     {
  20.         node *q = (node*)malloc(sizeof (node));
  21.  
  22.          printf("\nInput data for node %d : ",i);
  23.  
  24.         scanf("%d", &q -> x);
  25.  
  26.         q -> ptr = NULL;
  27.         if(head == NULL)
  28.         {
  29.             head = q;
  30.         }
  31.         else
  32.         {
  33.             node *temp = head;
  34.             while(temp -> ptr != NULL)
  35.             {
  36.                 temp = temp -> ptr;
  37.             }
  38.  
  39.             temp -> ptr = q;
  40.         }
  41.  
  42.     }
  43.  
  44.  
  45.       node *temp = head;
  46.      
  47.       printf("\nData entered in the list : \n");
  48.      
  49.       while(temp!= NULL)
  50.         {
  51.  
  52.             printf("\nData = %d\n", temp->x);
  53.             temp = temp->ptr;
  54.         }
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement