Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 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 *insert(struct Node *head, int data) {
  10.     struct Node *tmp;
  11.     tmp = (struct Node *) malloc(sizeof(struct Node));
  12.  
  13.     tmp->data = data;
  14.     tmp->next = head;
  15.     head = tmp;
  16.  
  17.     return head;
  18. }
  19.  
  20. void display(struct Node *head) {
  21.     struct Node *start = head;
  22.  
  23.     while (start) {
  24.         printf("%d ", start->data);
  25.         start = start->next;
  26.     }
  27.  
  28.     printf("\n");
  29. }
  30.  
  31. int main() {
  32.     int T, data;
  33.     scanf("%d", &T);
  34.  
  35.     struct Node *head = NULL;  
  36.     while (T-- > 0) {
  37.         scanf("%d", &data);
  38.         head = insert(head, data);
  39.     }
  40.  
  41.     display(head);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement