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