//栈 面向对象 +++++++++顺序存储结构 数组 浪费资源
#include
#include
#include
using namespace std;
typedef int ElemType;
const int MAXSIZE = 100;
class SqStack
{
public:
SqStack();
~ SqStack();
void SetEmpty();
void push( ElemType e);
ElemType pop();
void PrintOut();
private:
ElemType elem[MAXSIZE];
int top;
};
SqStack:: SqStack()
{
top = 0;
}
SqStack::~ SqStack()
{
}
void SqStack::SetEmpty() {
top = 0;
}
void SqStack::push( ElemType e) {
if (top == MAXSIZE - 1) {
cout << "栈满溢出"<< endl;
}
else
{
top++;
elem[top] = e;
}
}
ElemType SqStack::pop() {
ElemType x;
if (top == 0) {
cout << "栈为空!!" << endl;
x = 0;
}
else
{
x = elem[top];
top--;
}
return x;
}
void SqStack::PrintOut() {
cout << " PrintOut Data:" << endl;
if (top == 0) {
cout << " 已是空栈!"<= 1; k--) {
cout << setw(6) << elem[k] << endl;
}
}
}
int main() {
ElemType e, x;
int a;
SqStack s;
cout << "栈的顺序存储结构:" << endl;
cout <<"输入1进行入栈,输入2进行出栈,输入-999结束" << endl;
cin >> a;
while (a!=-999)
{
if (a == 1) {
cout << "输入压栈数据:" << endl;
cin >> e;
s.push(e);
s.PrintOut();
cout << "输入指令:" << endl;
cin >> a;
}
if (a == 2) {
x = s.pop();
cout << "出栈数据为:" <> a;
}
else {
cout << "错误指令!" << endl;
cout << "输入指令:" << endl;
cin >> a;
}
}
_getch();
return 0;
}
数据结构=多种结构比较(1)——栈
最新推荐文章于 2020-10-29 22:46:48 发布