fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. struct rocker {
  5. int data; //保存するデータ
  6. struct rocker *next; //次のボックスのアドレス(鍵)
  7. };
  8.  
  9. struct rocker *head;
  10.  
  11. struct rocker *new_rocker(struct rocker *last_rocker)
  12. {
  13. struct rocker *pt;
  14.  
  15. pt = malloc(sizeof (struct rocker));
  16. if (last_rocker) {
  17. last_rocker->next = pt;
  18. }
  19. return pt;
  20. }
  21.  
  22. void display_rockers(struct rocker *pt)
  23. {
  24. for ( ; pt; pt = pt->next) {
  25. if (pt->next) {
  26. printf("[ data = %d ]->", pt->data);
  27. } else {
  28. printf("[ data = %d ]\n", pt->data);
  29. }
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. struct rocker *last_rocker;
  36. struct rocker *next;
  37. int i, n;
  38.  
  39. scanf("%d", &n);
  40. last_rocker = NULL;
  41. for (i = 0; i < n; i++) {
  42. last_rocker = new_rocker(last_rocker);
  43. if (i == 0) {
  44. head = last_rocker;
  45. }
  46. scanf("%d", &last_rocker->data);
  47. }
  48. last_rocker->next = NULL;
  49. display_rockers(head);
  50. for (last_rocker = head; last_rocker; last_rocker = next) {
  51. next = last_rocker->next;
  52. free(last_rocker);
  53. }
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 1856KB
stdin
3
1 3 4
stdout
[ data = 1 ]->[ data = 3 ]->[ data = 4 ]