顺序栈 初始化,入栈,出栈,获取栈顶元素
#include <stdio.h>
#include <stdlib.h>
#define Max 5
typedef struct {
int data[Max];
int top;
}SqStack;
void InitStack(SqStack& S) {
S.top = -1;
}
bool push(SqStack& S, int e) {
if (S.top == Max - 1) {
return false;
}
S.data[++S.top] = e;
return true;
}
bool pop(SqStack& S, int& e) {
if (S.top == -1) {
return false;
}
e = S.data[S.top--];
return true;
}
bool GetTop(SqStack S, int& e) {
if (S.top == -1)
return false;
e = S.data[S.top];
return true;
}
int main() {
SqStack S;
InitStack(S);
push(S, 3);
push(S, 4);
int e;
GetTop(S,e);
printf("The top is %d\n", e);
pop(S, e);
printf("The top is %d\n", e);
GetTop(S, e);
printf("The top is %d\n", e);
}