#include <stdio.h>
#include <malloc.h>
/**************************************
struct Stack * s = newLinkedStack();
s->push(s,5);
s->push(s,6);
printf("%d\n",s->pop(s));
printf("%d\n",s->pop(s));
***************************************/
struct StackNode
{
	int data;
	struct StackNode* preNode;
};
typedef struct StackType
{
	struct StackNode* top;
	void (*push) (struct StackType* self, int data);
	int (*pop) (struct StackType* self);
	void (*clear) (struct StackType* self);
}Stack;

void pushStack(struct StackType* self, int data);
int popStack(struct StackType* self);
struct StackType* newLinkedStack();
struct StackNode* newStackNode();
void clearStack(struct StackType* self);

struct StackType* newLinkedStack()
{
	struct StackType* stack = (struct StackType*)malloc(sizeof(struct StackType));

	stack->push = pushStack;
	stack->pop = popStack;
	stack->clear = clearStack;
	stack->top = NULL;
	
	return stack; 
}
struct StackNode* newStackNode()
{
	struct StackNode* stackNode = (struct StackNode*) malloc( sizeof(struct StackNode) );
	stackNode->preNode = NULL;
	return stackNode;
}
void  pushStack(struct StackType* self, int data)
{
	struct StackNode* stackNode = newStackNode();
	stackNode->data = data;
	if(self->top == NULL)
		self->top = stackNode;
	else
	{
		stackNode->preNode = self->top;
		self->top = stackNode;
	}
} 
int popStack(struct StackType* self)
{
	int retn = self->top->data;
	void* temp = self->top;

	self->top = self->top->preNode;
	
	free(temp);
	return retn;
}
int isEmptyStack(struct StackType* self)
{
	if(self->top == NULL)
	{
		return 1;
	}
	else return 0;
	
}
void clearStack(struct StackType* self)
{
	while(self->top != NULL)
	{
		self->pop(self);
	}
}
void deleteLinkedStack(struct StackType* s)
{
	s->clear(s);
	free(s);
}
int main(void)
{
	Stack* s = newLinkedStack();
	s->push(s,5);
	s->push(s,6);
	printf("%d\n",s->pop(s));
	printf("%d\n",s->pop(s));
}