fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int Elem;
  5.  
  6. typedef struct stack {
  7. Elem e;
  8. struct stack *next;
  9. } Stack;
  10.  
  11. Elem pop(Stack **s) {
  12. if (*s==NULL) exit(EXIT_FAILURE);
  13. Stack* help = *s;
  14. Elem e = help->e;
  15. *s = (*s)->next;
  16. free(help);
  17. return e;
  18. }
  19.  
  20. void push(Stack **ss, Elem elem) {
  21. Stack *help;
  22.  
  23. help = malloc(sizeof(Stack));
  24. help->e = elem;
  25. help->next = *ss;
  26. *ss = help;
  27. }
  28.  
  29. int main()
  30. {
  31. Stack *s = NULL;
  32. push(&s, 10);
  33. push(&s, 12);
  34.  
  35. printf("%d\n", pop(&s));
  36. printf("%d\n", pop(&s));
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 1852KB
stdin
Standard input is empty
stdout
12
10