#include <stdio.h>
#include<stdlib.h>
#define MaxSize 10
//定义
typedef struct {
int data[MaxSize];
int top; //栈顶指针 数组下标
}SqStack;
//初始化 判空也是看 栈顶指针是不是-1
void InitStack(SqStack& S) {
S.top = -1; //也可以是S.top=0
}
//增加
bool Push(SqStack& S, int x) {
if (S.top == MaxSize - 1)
return false;
S.top = S.top + 1; //++S.top先加在使用 S.top++ 先使用再加
S.data[S.top] = x;
return true;
}
//删除
bool Delete(SqStack& S, int &x) {
if (S.top == -1)
return false;
x = S.data[S.top];
S.top = S.top - 1;
return true;
}
//出栈
bool Pop(SqStack& S, int &x) {
if (S.top == -1)
return false;
x = S.data[S.top--];
return true;
}
//读出栈顶元素
bool GetTop(SqStack& S, int x) {
if (S.top == -1)
return false;
x = S.data[S.top];
return true;
}
SqStack
最新推荐文章于 2022-10-26 22:33:37 发布