fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct link{
  5. int data;
  6. struct link *next;
  7. };
  8.  
  9. struct list{
  10. struct link *head;
  11. struct link *tail;
  12. };
  13.  
  14. struct link * add_tail(struct list *lst, int value){
  15. struct link *item = (struct link *)malloc(sizeof(struct link));
  16. item->data = value;
  17. item->next = NULL;
  18. if(lst->head == NULL){
  19. lst->head = item;
  20. lst->tail = lst->head;
  21. }else{
  22. lst->tail->next = item;
  23. lst->tail = item;
  24. }
  25. return item;
  26. }
  27.  
  28. void print(struct list *lst){
  29. struct link *p = lst->head;
  30. while(p){
  31. printf("%d\n", p->data);
  32. p = p->next;
  33. }
  34. }
  35.  
  36. int main(){
  37. struct list lst;
  38. lst.head = lst.tail = NULL;
  39. int in;
  40.  
  41. while(scanf("%d", &in) == 1){
  42. add_tail(&lst, in);
  43. }
  44. print(&lst);
  45. }
Success #stdin #stdout 0s 2292KB
stdin
1 2 3 -1
stdout
1
2
3
-1