顺序栈-初始化栈顶指针为-1
1.头文件及类型定义
#include<stdio.h>
#define MaxSize 10 //定义顺序栈中元素的最大个数
#define ElemType int
2.顺序栈类型定义
typedef struct {
ElemType data[MaxSize]; //静态数组存放栈中元素
int top; //栈顶指针,一般来说存放数组的下标
}SeqStack;
3.函数声明
/*函数声明*/
void InitStack(SeqStack& S); //1.初始化顺序栈
bool StackEmpty(SeqStack S); //2.判空
bool Push(SeqStack& S, ElemType x); //3.入栈
bool Pop(SeqStack& S, ElemType& x); //4.出栈
bool GetTop(SeqStack S, ElemType& x); //5.读取栈顶元素
4.基本操作
4.1 初始化顺序栈
//1.初始化栈
void InitStack(SeqStack& S) {
S.top = -1; //初始化栈顶指针为-1