#include #include typedef int Item; typedef struct node { Item data; struct node *next; } StackNode; // by putting top inside struct we get single pointer to hodl that does not change when top changes typedef struct { StackNode* top; } StackType; StackType* createStack() { StackType* s = malloc(sizeof(StackType)); return s; } void push(StackType* s, Item i) { StackNode* newNode = malloc(sizeof(StackNode)); newNode->data = i; newNode->next = s->top; s->top = newNode; } Item pop(StackType* s) { StackNode* oldTop; if (s->top==NULL) { Item i; return i; } oldTop = s->top; s->top = oldTop->next; Item d = oldTop->data; free(oldTop); return d; } int main(void) { StackType * s1; s1 = createStack(); push(s1, 5); push(s1, 10); printf("%d\n", pop(s1)); printf("%d\n", pop(s1)); }