栈的顺序存储结构
顺序栈的实现
#include "iostream"
#define MaxSize 50
//栈的结构体
struct Stack{
int data[MaxSize];
int top;
}stack;
//初始化栈
void init(){
stack.top = -1;
}
//判断是否栈空
bool isEmpty(){
if (stack.top == -1){
printf("栈空\n");
return true;
}
return false;
};
//判断是否栈满
bool isFull(){
if (stack.top == MaxSize - 1){
printf("栈满\n");
return true;
}
return false;
};
//入栈
int push(int num){
if (!isFull()){
stack.top = stack.top + 1;
stack.data[stack.top] = num;
}
return -1; //当栈满的时候返回-1
}
//出栈
int pop(){
if (!isEmpty()){
stack.top = stack.top - 1;
return stack.data[stack.top + 1];
}
return -1; //当栈空的时候返回-1
}
int main(){
init();
//入栈
for (int i = 1; i <=5; i++){
push(i);
}
//出栈,调用一次出栈一次
pop();
printf("%d\n", pop());
}
共享栈
标签:return,int,top,MaxSize,基本操作,data,stack
From: https://www.cnblogs.com/miyol/p/16734094.html