fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct data {
  5. char key;
  6. struct data *next;
  7. };
  8.  
  9. void push(struct data **top, char key);
  10. char pop(struct data **top);
  11. void print_stack_list(struct data *top);
  12.  
  13. int main() {
  14. struct data *top;
  15. top = NULL;
  16.  
  17. push(&top, 'a');
  18.  
  19. print_stack_list(top);
  20. printf("\n");
  21.  
  22. push(&top, 'b');
  23.  
  24. print_stack_list(top);
  25. printf("\n");
  26.  
  27. pop(&top);
  28.  
  29. print_stack_list(top);
  30. printf("\n");
  31.  
  32. pop(&top);
  33.  
  34. return 0;
  35. }
  36.  
  37. void push(struct data **top, char key) {
  38. struct data *d;
  39. d = (struct data *) malloc(sizeof(struct data));
  40. d->key = key;
  41. d->next = *top;
  42. *top = d;
  43. }
  44.  
  45. char pop(struct data **top) {
  46. int key;
  47. struct data *d;
  48. key = (*top)->key;
  49. d = *top;
  50. *top = (*top)->next;
  51. free(d);
  52. return key;
  53. }
  54.  
  55. void print_stack_list(struct data *top) {
  56. while (top != NULL) {
  57. printf("%c\n",top->key);
  58. top = top->next;
  59. }
  60. }
Success #stdin #stdout 0.01s 1852KB
stdin
Standard input is empty
stdout
a

b
a

a