Advertisement
afrinahoque

insert at last singly

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