#include <stdio.h> #include <malloc.h> typedef bool Status; #define STACK_INIT_SIZE 200 #define STACKINCREMENT 10 typedef struct { int x; int y; int a; }Step,SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack; Status InitStack (SqStack &S); Status Push(SqStack &S, SElemType e); Status Pop(SqStack &S, SElemType &e); void StackEmpty(SqStack &S); void DestroyStack(SqStack &S); //初始化栈,要点在于s.top=s.base Status InitStack (SqStack &S) { S.base = (SElemType *) malloc (STACK_INIT_SIZE * sizeof(SElemType)); if(!S.base) return 0; S.top = S.base; S.stacksize = STACK_INIT_SIZE; return 1; } //弹栈,是先表头减1然后输出 Status Pop(SqStack &S, SElemType &e) { if(S.top == S.base) return 0; e = *(--S.top); return 1; } Status Push(SqStack &S, SElemType e) { if(S.top - S.base >= S.stacksize) { S.base = (SElemType *) realloc (S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType)); if(!S.base) return 0; S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top = e;//压栈,压入元素后要注意栈顶指针加1 S.top++; return 1; } //把栈置空 void StackEmpty(SqStack &S) { S.base = S.top; } void DestroyStack(SqStack &S){ free(&S); }