#include <stdio.h>
#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;
#define MAXSIZE 50
typedef struct { //定义顺序栈
int data[MAXSIZE];
int top;
}SqStack;
void InitStack(SqStack &S)
{
S.top=-1;
}
bool StackEmpty(SqStack S)
{
if(S.top==-1)
return true;
else
return false;
}
bool Push(SqStack &S,int x)
{
if(S.top==MAXSIZE-1)
{
cout<<"栈满!"<<endl;
return false;
}
S.data[++S.top]=x;
return true;
}
bool Pop(SqStack &S,int &x)
{
if(S.top==-1)
{
cout<<"栈空!"<<endl;
return false;
}
x=S.data[S.top--];
return true;
}
bool GetTop(SqStack S,int &x)
{
if(S.top==-1)
{
cout<<"栈空!"<<endl;
return false;
}
x=S.data[S.top];
return true;
}
main()
{
SqStack S;
InitStack(S);
Push(S,1);
Push(S,2);
Push(S,3);
Push(S,4);
Push(S,5);
int x;
Pop(S,x);
cout<<x<<endl;
Pop(S,x);
cout<<x<<endl;
Pop(S,x);
cout<<x<<endl;
Pop(S,x);
cout<<x<<endl;
Pop(S,x);
cout<<x<<endl;
}
数据结构3.1-顺序栈的定义
最新推荐文章于 2024-07-03 11:26:52 发布