4.1 初始化top = -1
#define MaxSize 50
typedef struct SeqStack {
int data[MaxSize];
int top;
} SeqStack;
void InitStack(SeqStack &seqStack) {
seqStack.top = -1;
};
bool IsEmptyStack(SeqStack seqStack) {
if (seqStack.top == -1) {
return true;
} else {
return false;
}
}
bool Push(SeqStack &seqStack, int e) {
//判满: top=MaxSize-1
if (seqStack.top == MaxSize - 1) {
return false;
}
seqStack.data[++seqStack.top] = e;
return true;
}
bool Pop(SeqStack &seqStack, int &e) {
if (seqStack.top == -1) {
return false;
}
e = seqStack.data[seqStack.top--];
return true;
}
bool GetTop(SeqStack &seqStack, int &e) {
if (seqStack.top == -1) {
return false;
}
e = seqStack.data[seqStack.top];
return true;
}
4.2 初始化top = 0
#define MaxSize 50
typedef struct SeqStack {
int data[MaxSize];
int top;
} SeqStack;
void InitStack(SeqStack &seqStack) {
seqStack.top = 0;
};
bool IsEmpty(SeqStack seqStack) {
if (seqStack.top == 0) {
return true;
} else {
return false;
}
}
bool Push(SeqStack &seqStack, int e) {
//判满: top=MaxSize
if (seqStack.top == MaxSize) {
return false;
}
seqStack.data[seqStack.top++] = e;
return true;
}
bool Pop(SeqStack &seqStack, int &e) {
if (seqStack.top == 0) {
return false;
}
e = seqStack.data[--seqStack.top];
return true;
}
bool GetTop(SeqStack seqStack, int &e) {
if (seqStack.top == 0) {
return false;
}
int index = seqStack.top - 1;
e = seqStack.data[index];
return true;
}