数组栈C语言

#include 
#include 
#define MAX 10
typedef int DataType;
struct Stack {
	int top;
	DataType* stack;
};
struct Stack* initStack()
{
	struct Stack* newStack = (struct Stack *)malloc(sizeof(struct Stack));
	newStack->top = -1;
	newStack->stack = (DataType *)malloc(sizeof(DataType) * MAX);
	return newStack;
}
DataType Top(struct Stack* newStack)
{
	return newStack->stack[newStack->top];
}
int empty(struct Stack* newStack)
{
	if(newStack->top == -1)
		return -1;
	return 0;
}
void push(struct Stack* newStack, DataType data)
{
	if(newStack->top == MAX - 1)
		return;
	newStack->stack[++newStack->top] = data;
}
void pop(struct Stack* newStack)
{
	--newStack->top;
	if(newStack->top == -1)
		return;
}
int main()
{
	struct Stack* newStack = initStack();
	push(newStack, 1);
	push(newStack, 2);
	push(newStack, 3);
	while(!empty(newStack))
	{
		printf("%d", Top(newStack));
		pop(newStack);
	}
	printf("\n");
	return 0;
}

你可能感兴趣的:(数组栈C语言)