fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct stack
  5. {
  6. int data;
  7. struct stack *next;
  8. } stack;
  9.  
  10.  
  11. void push_s(stack** s, int data)
  12. {
  13. while (*s)
  14. s = &(*s)->next;
  15.  
  16. *s = malloc(sizeof(**s));
  17. (*s)->data = data;
  18. (*s)->next = NULL;
  19. }
  20.  
  21. void print_s(stack const *s)
  22. {
  23. for (;s;s = s->next)
  24. printf("%p: data = %d, next = %p\n", s, s->data, s->next);
  25. }
  26.  
  27. void free_s(stack **s)
  28. {
  29. while (*s)
  30. {
  31. stack *tmp = *s;
  32. *s = tmp->next;
  33. free(tmp);
  34. }
  35. }
  36.  
  37. int main (void)
  38. {
  39. stack* s = NULL;
  40.  
  41. push_s(&s,2);
  42. push_s(&s,4);
  43. push_s(&s,6);
  44. push_s(&s,8);
  45.  
  46. print_s(s);
  47. free_s(&s);
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
0x826e008: data = 2, next = 0x826e018
0x826e018: data = 4, next = 0x826e028
0x826e028: data = 6, next = 0x826e038
0x826e038: data = 8, next = (nil)