#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5

typedef struct Stack
{
	int top;
	int data[MAX_SIZE];
}Stack;

Stack* initStack()
{
	Stack *s = (Stack *)malloc(sizeof(Stack));
	s->top = 0;
	
	for(int i = 0; i < MAX_SIZE; i++)
	{
		s->data[i] = 0;
	}
	return s;
}

int push(Stack *s, int value)
{
	if(s->top == MAX_SIZE - 1)
	{
		return -1;
	}

	s->top++;
	s->data[s->top] = value;
	return 0;
}

int pop(Stack *s)
{
	if(s->top == -1)
	{
		return -1;
	}

	int v = s->data[s->top];
	s->top--;
	return v;
}

void main()
{
	Stack *s = initStack();
	push(s, 10);
	push(s, 20);
	push(s, 30);
	push(s, 40);

	printf("%d\n", pop(s));
}

posted on 2013-04-26 20:44  纯洁的坏蛋  阅读(584)  评论(0编辑  收藏  举报