/*
* @Description:
* @版本: 2.0
* @作者: jianwen
* @Date: 2019-11-01 15:44:37
* @LastEditors: jianwen
* @LastEditTime: 2019-11-03 10:08:29
*/
#include <iostream>
using namespace std;
#define MAXSIZE 10
typedef int datatype;
class Stack
{
public:
datatype data[MAXSIZE];
int top; //用于栈顶指针,栈顶元素在数组中的位置0-n
};
class SqStack{
public:
bool Empty_Stack(Stack &S);
bool IsNull(Stack &S);
bool PushStack(Stack &S,datatype a);
bool PopStack(Stack &S,datatype e);
bool show(Stack S);
};
bool SqStack::Empty_Stack(Stack &S){
S.top = -1;
return true;
}
bool SqStack::IsNull(Stack &S){
if (S.top >= 0)
return true;
return false;
}
bool SqStack::PushStack(Stack &S,datatype a){
if(S.top == 10-1){
cout << "栈满" << endl;
return false;
}
S.top += 1;
S.data[S.top] = a;
cout <<S.data[S.top];
return true;
}
bool SqStack::PopStack(Stack &S,datatype e){
if (S.top == -1)
return false;
e = S.data[S.top];
S.top -=1;
return true;
}
bool SqStack::show(Stack S)
{
if (IsNull(S))
{
for (int i = 0;i <= S.top;++i)
{
cout<<S.data[i]<<" ";
}
return true;
}
else
{
cout<<"Empty Stack~";
return false;
}
}
int main()
{
SqStack s;
Stack S;
datatype e, d;
s.Empty_Stack(S); //置空栈
cout<<"This Stack is Null ?"<<bool(s.IsNull(S))<<endl; //判断是否为空栈
s.PushStack(S,2); //进栈
s.PushStack(S,3);
s.PushStack(S,4);
s.PushStack(S,2);
s.PushStack(S,8);
cout<<"The elements are : ";
s.show(S); //显示整个栈的内容
cout << endl;
s.PopStack(S, e); //出栈
s.PopStack(S, d);
s.show(S);
return 0;
}
栈的顺序存储结构C++实现
最新推荐文章于 2023-02-10 15:19:35 发布