fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node{
  5. int data;
  6. struct Node *next;
  7. } Node;
  8.  
  9. Node* read_item(){
  10. int in = 0;
  11. if (scanf("%d", &in) != 1)
  12. return NULL;
  13. Node * new_item = (Node *)calloc(1,sizeof(Node));
  14. new_item->data = in;
  15. return new_item;
  16. }
  17.  
  18. void read_list(Node **head){
  19. if(*head == NULL)
  20. *head = read_item();
  21.  
  22. Node * last_item = *head;
  23. Node * item = NULL;
  24. while((item = read_item())){
  25. last_item->next = item;
  26. last_item = item;
  27. }
  28. }
  29.  
  30. void print(Node *head){
  31. Node *p;
  32. p = head;
  33. if (!p) printf("The list is empty\n");
  34. while(p){
  35. printf("out: %d\n", p->data);
  36. p = p->next;
  37. }
  38. }
  39.  
  40. int main(){
  41. Node *head = NULL;
  42. read_list(&head);
  43. print(head);
  44.  
  45. int pause;
  46. scanf("%d", &pause);
  47. }
Success #stdin #stdout 0s 2296KB
stdin
1 2 3
stdout
out: 1
out: 2
out: 3