Advertisement
afrinahoque

Insert at first singly

Dec 8th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 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_first()
  12.  {
  13.      node *N=(node*)malloc(sizeof(node));
  14.      scanf("%d", &N->a);
  15.      N->next=NULL;
  16.  
  17.      if (head==NULL)
  18.      {
  19.          head=N;
  20.          return;
  21.      }
  22.      else
  23.      {
  24.         N->next=head;
  25.         head=N;
  26.         return;
  27.      }
  28.  }
  29.  
  30.  void display()
  31. {
  32.     node *list=head;
  33.     while(list!=NULL)
  34.     {
  35.         printf("%d\n", list->a);
  36.         list=list->next;
  37.     }
  38. }
  39.  
  40.  int main()
  41.  {
  42.      int i,n;
  43.      scanf("%d", &n);
  44.  
  45.      for(i=0; i<n; i++)
  46.      {
  47.          insert_at_first();
  48.      }
  49.      display();
  50.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement