#include<iostream>
using namespace std; //关于这里,且听下回分解
//定义结点类型
struct Node
{
int data;
Node *next;
};
//定义链队列
class LinkStack
{
public:
LinkStack(); //构造函数,初始化一个空的链栈
~LinkStack(); //析构函数,释放链栈中各结点的存储空间
void push(int x); //将元素x入栈
int pop(); //将栈顶元素出队
int getTop(); //取链栈的队头元素
int Empty(); //判断链栈是否为空
void print(); //输出当前栈中元素
private:
Node *top; //栈顶指针
};
//初始化链栈
LinkStack::LinkStack()
{
cout<<"出栈成功!"<<endl;
}
//释放链队列
LinkStack::~LinkStack()
{
cout<<"释放成功!"<<endl;
}
//入栈
void LinkStack::push(int x)
{
cout<<"入栈成功!"<<endl;
}
//出栈
int LinkStack::pop()
{
cout<<"出栈成功!"<<endl;
}
//获取栈顶元素
int LinkStack::getTop()
{
cout<<"获取栈顶元素成功!"<<endl;
}
//链是否为空
int LinkStack::Empty()
{
cout<<"出栈成功!"<<endl;
}
//输出链栈元素
void LinkStack::print()
{
cout<<"输出成功!"<<endl;
}
//菜单
void menu() {
cout << "1.进栈" << endl;
cout << "2.出栈" << endl;
cout << "3.获取栈顶元素" << endl;
cout << "4.栈是否为空" << endl;
cout << "5.由栈顶到栈底依次输出栈元素" << endl;
cout << "6.退出" << endl;
}
//执行菜单功能
void function(int num, LinkStack &s) {
switch (num) {
int x;
case 1:
cout << "请输入进栈元素值,多个值以空格分隔,-1为结束标志:" << endl;
break;
case 2:
try
{
cout<<"try成功!"<<endl;
}
catch (char*str) { cout << str << endl; }
break;
case 3:
try
{
cout<<"3 成功!"<<endl;
}
catch (char*str) { cout << str << endl; }
break;
case 4:
cout<<"出栈成功!"<<endl;
break;
case 5:
try
{
cout<<"出栈成功!"<<endl;
}
catch (char*str) { cout << str << endl; }
cout << endl;
break;
default:
exit(0);
}
}
int main()
{
LinkStack Q; //创建类的实例
int num;
cout << "***学号+姓名第5周链栈***" << endl;
menu();
while (true)
{
cout << "\n----请输入要执行的操作序号:" << endl;
cin >> num;
function(num, Q);
}
return 0;
}