/* The stack pointer always points to the first free position on the stack. This is the common way to implement the stack. */ #include struct Node { int element; Node* next; }; Node* stackPointer = NULL; void push(int element) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->element = element; newNode->next = stackPointer; stackPointer = newNode; } bool isEmpty() { return stackPointer == NULL; } int pop() { if (!isEmpty()) { Node* tmp = stackPointer; int element = stackPointer->element; stackPointer = stackPointer->next; free(tmp); return element; } }