fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. struct data{
  6. char key;
  7. struct data *next;
  8. };
  9.  
  10. void print_stack_list(struct data *top);
  11.  
  12. int main() {
  13. struct data *top, *cur;
  14. char values[4] = {'a', 'b', 'c', 'd'};
  15. int i;
  16. top = NULL;
  17. for (i = 0; i < 4; i++) {
  18. cur = (struct data*)malloc(sizeof(struct data));
  19. if (cur == NULL) {
  20. printf("メモりが確保できませんでした。\n");
  21. return 1;
  22. }
  23. cur->key = values[i];
  24. cur->next = top;
  25. top = cur;
  26. }
  27. print_stack_list(top);
  28. return 0;
  29. }
  30.  
  31. void print_stack_list(struct data *top) {
  32. while (top != NULL) {
  33. printf("%c\n",top->key);
  34. top = top->next;
  35. }
  36. }
Success #stdin #stdout 0.02s 1852KB
stdin
Standard input is empty
stdout
d
c
b
a