arrStack.h
个人笔记
#include <malloc.h>
#define Error(module) fprintf(stderr,"error:"#module"\n")
typedef struct astack *Stack;
typedef struct astack {
int top;
int maxtop;
StackItem *data; //数组实现
}Astack;
Stack StackInit(int size) {
Stack S = malloc(size*sizeof(*S));
S->data = malloc(sizeof(StackItem));
S->maxtop = size;
S->top = 1;
return S;
}
int StackIsEmpty(Stack S) {
return S->top < 0;
}
int StackFull(Stack S) {
return S->top >= S->maxtop;
}
StackItem StackTop(Stack S) { //取栈顶元素
if( StackIsEmpty(S) ) {
Error("Stack is Empty.");
}
else
return S->data[S->top];
}
void StackPush(StackItem x, Stack S) { //入栈
if( StackFull(S) ) {
Error("Stack is full.");
} else {
S->data[++S->top] = x;
}
}
StackItem StackPop(Stack S) { //出栈
if( StackIsEmpty(S) ) {
Error("Stack is empty.");
}
else
return S->data[S->top--];
}