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