#include<stdio.h>#include<stdbool.h>#include<stdlib.h>//设置栈的最大长度为10#defineMaxSize10/*
栈:
1.英文名为stack,是线性的数据结构,可以用数组存储
2.栈只允许从【栈顶】加入数据和删除数据,就像堆起来的盘子,只能从顶拿走和放入
3.栈是LIFO(Last In First Out),后入先出
*/typedefstruct{int data[MaxSize];//顺序栈使用静态数组存储的int top;//栈顶指针,这个指针存储栈所在位置的数组下标}SqStack;voidInitStack(SqStack *S){
S->top=-1;//由于数组下标从0开始,所以当没有元素时,我们设定栈顶位置为-1;所以栈的判空条件为S->top==-1?}
bool IsEmpty(SqStack S){if(S.top==-1){printf("Stack is Empty!\n");return true;}else{printf("Stack is NOT Empty!\n");return false;}}
bool IsFull(SqStack S){if(S.top==MaxSize-1){printf("Stack is full now\n");return true;}else{printf("Stack is NOT full now\n");return false;}}//入栈操作
bool Push(SqStack *S,int x){if( S->top==MaxSize-1)//栈满{printf("Error:Stack is full ,failed to Push\n");return false;}
S->top+=1;
S->data[S->top]=x;//这两行也可以写成S->data[++S->top]=x; !!return true;}//弹栈操作
bool Pop(SqStack *S,int*x){if(S->top==-1){printf("Error:Stack is empty, failed to Pop\n");return false;}*x=S->data[S->top];
S->top=S->top-1;//这两行也可以写成:*x=S->data[S->top--];return true;}//读栈顶元素操作
bool readTop(SqStack S,int*x){if(S.top==-1){printf("Error: Stack is empty, failed to read the top\n");return false;}else{*x=S.data[S.top];return true;}}intmain(){/*操作代码*/system("pause");return0;}