#include <stdio.h>

typedef struct node {
	struct node *parent;
	int value;
} Node;

typedef struct stack {
	Node *top;
} Stack;

Node *push(int _val, Stack *_stack) {
	Node *newNode = (Node *)malloc(sizeof(Node));		// allocate memory for the node and save reference
	newNode->value = _val;										// save the value into the node
	newNode->parent = _stack->top;							// point the parent of the new node to the old top
	_stack->top = newNode;										// have the top of the stack point to the new node
	return newNode;
}

int pop(Stack *_stack) {
	// make sure stack isnt empty
	if(_stack->top == NULL)
		return -1;
	
	// grab the top entry and save the value in it
	Node *topNode = _stack->top;
	int toReturn = topNode->value;
	
	// have the top of the stack down one level (effectively removing the value)
	_stack->top = topNode->parent;
	
	// free the memory
	free(topNode);
	
	// return the value
	return toReturn;
}

// returns 1 if stack is empty, 0 else
int isEmpty(Stack *_stack) {
	if(_stack->top == NULL)
		return 1;
	return 0;
}

int top(Stack *_stack) {
	return _stack->top->value;
}

int main() {
	// create out stack to fool around with
	Stack *myStack = (Stack *)malloc(sizeof(Stack));
	
	// add some stuff to the stack
	push(5, myStack);
	push(4, myStack);
	push(3, myStack);
	push(2, myStack);
	push(1, myStack);
	push(0, myStack);
	
	// Look at the top element
	printf("Top: %d\n", top(myStack));
	
	// Display the stuff in the stack and remove it all
	while(!isEmpty(myStack))
		printf("Pop: %d\n", pop(myStack));
	
	return 0;
}
