Advertisement
pkbagchi

insert_data(not reverse)

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