Files
Data_Structure/OrigFiles/3-特殊线性表/3-SqStack(顺序栈)/SqStack.cpp

97 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include<iostream>//cout,cin
using namespace std;
#include "SqStack.h"
char pause;
void dispmenu()
{ //显示主菜单
cout<<endl;
cout<<"1-初始化顺序栈\n";
cout<<"2-元素入栈\n";
cout<<"3-元素出栈\n";
cout<<"4-取栈顶元素\n";
cout<<"5-测栈空\n";
cout<<"6-显示栈元素\n";
cout<<"0-退出\n";
}
//主函数
int main()
{
int i;
int e;
SqStack<int> S;//建立容量为20、元素类型为整型的空顺序栈
system("cls"); // 清屏
int choice;
do
{
dispmenu(); // 显示主菜单
cout<<"Enter choice(1~60 退出):";
cin>>choice;
switch(choice)
{
case 1: // 初始化顺序栈
cout<<"请输入要创建的顺序栈的长度";
cin>>i;
cout<<endl;
InitStack (S,i);
cout<<endl<<"创建成功!"<<endl;
break;
case 2: // 入栈
cout<<"输入要入栈的元素值:"<<endl;
cin>>e;
if(Push(S,e))
{
cout<<"入栈成功!入栈后,栈元素为:"<<endl;
DispStack(S);
}
else
cout<<endl<<"入栈不成功!"<<endl;
break;
case 3: // 出栈
if(Pop(S,e))
{
cout<<"出栈成功!出栈元素为:"<<e<<endl;
cout<<"出栈后,栈元素为:"<<endl;
DispStack(S);
}
else
cout<<endl<<"栈空,出栈失败!"<<endl;
break;
case 4: // 取栈顶元素
if(GetTop(S,e))
{
cout<<endl<<"栈顶元素为:"<<e<<endl;
}
else
cout<<endl<<"栈空!"<<endl;
break;
case 5: // 测栈空
if(StackEmpty(S))
cout<<endl<<"空栈!"<<endl;
else
cout<<endl<<"不是空栈!"<<endl;
break;
case 6: // 显示栈元素
DispStack(S);
cout<<endl;
break;
case 0: // 退出
DestroyStack(S);
cout<<"结束运行 Bye-bye"<<endl;
break;
default: // 无效选择
cout<<"Invalid choice\n";
break;
}
}while(choice!=0);
return 0;
}//end of main function