C++实现链栈相关操作代码
#include<iostream>
#include<cstdlib>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef int Status;
typedef int Elemtype;
typedef struct StackNode
{
Elemtype data;
struct StackNode* next;
}StackNode, * Linklist;
Status InitStack(Linklist& S);//初始化链栈
Status CreateStack(Linklist& S, int len);//创建链栈
Status PushStack(Linklist& S, Elemtype e);//入链栈
Status PopStack(Linklist& S, Elemtype& e);//出链栈
Status GetStack(Linklist S);//取出链栈的栈顶元素
Status LenStack(Linklist S);//求取链栈的长度
Status PrintStack_1(Linklist S);//逆序打印链栈
Status PrintStack_2(Linklist S);//正序打印链栈
int main(void)
{
Linklist S;
S = NULL;
int e;
int k;
int len;
do {
cout << "\n*****链栈相关操作*****";
cout << "\n1.初始化一个空链栈";
cout << "\n2.随机创建一个包含正整数值的链栈";
cout << "\n3.将一个数e插入到链栈栈顶";
cout << "\n4.将链栈栈顶元素弹出,并输出元素的值";
cout << "\n5.获取链栈栈顶元素";
cout << "\n6.求链栈的长度";
cout << "\n7.输出链栈从栈顶到栈底的元素";
cout << "\n8.输出链栈从栈底到栈顶的元素";
cout << "\n0.结束相关操作";
cout << "\n输入想要进行相关操作的序号:";
cin >> k;
switch (k)
{
case 1:
{
if (InitStack(S))
cout << "初始化成功。\n" << endl;
else
cout << "初始化失败。\n" << endl;
break;
}
case 2:
{
cout << "输入想要创建链栈的长度:";
cin >> len;
if (CreateStack(S,len))
{
cout << "创建成功。\n";
}
else
{
cout << "创建失败。\n";
}
break;
}
case 3:
{
cout << "输入想要插入元素的数值x为:";
cin >> e;
if (PushStack(S, e)) cout << "插入成功。\n";
else cout << "插入失败。\n";
break;
}
case 4:
{
if (PopStack(S,e)) cout << "删除成功。\n";
else cout << "删除失败。\n";
break;
}
case 5:
{
if (GetStack(S)) cout << "输出成功。\n";
else cout << "输出失败。\n";
break;
}
case 6:
{
if (LenStack(S)) cout << "长度获取成功。\n";
else cout << "长度获取失败。\n";
break;
}
case 7:
{
if (PrintStack_1(S)) cout << "全部输出成功。\n";
else cout << "全部输出失败。\n";
break;
}
case 8:
{
if (PrintStack_2(S)) cout << "全部输出成功。\n";
else cout << "全部输出失败。\n";
break;
}
}
} while (k != 0);
return OK;
}
Status InitStack(Linklist& S)//初始化链栈
{
S = NULL;
return OK;
}
Status CreateStack(Linklist& S, int len)//创建链栈
{
StackNode* p;
int e;
cout << "\n输入想要创建链栈的数据:";
for (int i = 0; i < len; i++)
{
p = new StackNode;
cin >> e;
p->data = e;
p->next = S;
S = p;
}
return OK;
}
Status PushStack(Linklist& S, Elemtype e)//入链栈
{
StackNode* p;
p = new StackNode;
p->data = e;
p->next = S;
S = p;
return OK;
}
Status PopStack(Linklist& S, Elemtype& e)//出链栈
{
if (S == NULL) return ERROR;
StackNode* p;
p = new StackNode;
e = S->data;
p = S;
S = S->next;
delete p;
cout << "出链栈的元素为:" << e << endl;
return OK;
}
Status GetStack(Linklist S)//取出链栈的栈顶元素
{
if (S->next == NULL) return ERROR;
cout << "取出链栈的栈顶元素为:" << S->data << endl;
return OK;
}
Status LenStack(Linklist S)
{
StackNode* p;
p = new StackNode;
p = S;
int len = 0;
while (p)
{
p = p->next;
len++;
}
cout << "链栈的长度为:" << len << endl;
return OK;
}
Status PrintStack_1(Linklist S)//打印链栈
{
if (S == NULL)
{
cout << "链栈为空。" << endl;
}
cout << "链栈中的元素有:";
StackNode* p;
p = S;
while (p!=NULL)
{
cout << p->data << " ";
p = p->next;
}
return OK;
}
Status PrintStack_2(Linklist S)//递归实现正序打印链栈
{
if (S == NULL)
{
cout << "链栈为空。" << endl;
return ERROR;
}
if (S->next == NULL)
{
cout << "链栈中的元素有:";
cout << S->data << " ";
return OK;
}
PrintStack_2(S->next);
cout << S->data << " ";
}