#include<iostream>
#include<stdlib.h>
using namespace std;
#define Stack_size 20
typedef struct Stack{
int elem[Stack_size];
int top;
}Stack;
Stack * init(){//初始化一个空栈
Stack *S=(Stack *)malloc(sizeof(Stack));
S->top=-1;
return S;
}
void print(Stack *S){//打印栈元素
for(int i=0;i<=S->top;i++){
cout<<S->elem[i]<<" ";
}
cout<<endl;
}
int push(Stack *S,int num){//入栈
if(S->top==Stack_size-1)return -1;
S->top++;
S->elem[S->top]=num;
return 1;
}
int pop(Stack *S){//出栈
if(S->top!=-1){
int num=S->elem[S->top--];
return num;
}
return -99999;
}
int main(int argc, char const *argv[])
{
Stack *S;
S=init();
push(S,1);
print(S);
push(S,2);
push(S,3);
push(S,4);
print(S);
cout<<"pop "<<pop(S)<<endl;
cout<<"pop "<<pop(S)<<endl;
cout<<"pop "<<pop(S)<<endl;
return 0;
}
数据结构 顺序栈初始化及入出栈
最新推荐文章于 2023-11-24 19:09:54 发布