#include <iostream>
#include "com_h.H"
#include<stdio.h>
int main()
{
LinkStack S;
int e;
InitStack(S);
// cout<<"请输入一个要入栈的元素,0表示停止"<<endl;
// cin>>e;
int n;
menu1();
cout<<"默认初始化完成!" <<endl;
do{
cout<<"请输入操作数字:"<<endl;
cin>>n;
switch(n)
{
case 1:
cout<<"请输入一个要入栈的元素:"<<endl;
cin>>e;
Push(S,e);
break;
case 2:
if(IsEmpty(S)==1)
{
cout<<"该链表为空!"<<endl;
}
else
{
Pop(S,e);
cout<<"出栈元素为:"<<e<<endl;
if(IsEmpty(S)==1)
{
cout<<"出栈后该链表为空!"<<endl;
}
else
{
cout<<"剩余的链栈:"<<endl;
printStack(S);
}
}
break;
case 3:
if(IsEmpty(S)==1)
{
cout<<"该链表为空!"<<endl;
}
else{
// cout<<"取栈顶元素测试:";
e=GetTop(S);
cout<<"取出的栈顶元素为:"<<e<<endl;
}
break;
case 4:
if(IsEmpty(S)==1)
{
cout<<"该链表为空!"<<endl;
}
else
{
int m;
m=ListLength(S);
cout<<"栈的长度为:"<<m<<endl;
}
break;
case 5:
if(IsEmpty(S)==1)
{
cout<<"该链表为空!"<<endl;
}
else
{
cout<<"该链表不为空!"<<endl;
}
break;
case 6:
if(Clear(S)==1)
{
cout<<"清空成功!"<<endl;
}
else
{
cout<<"该链栈为空!"<<endl;
}
break;
case 7:
Destroy(S);
cout<<"摧毁一个链栈成功!"<<endl;
break;
case 0:
cout<<"结束操作!退出该程序!"<<endl;
exit(0);
default:
break;
}
} while(n!=0);
}
#include<iostream>
using namespace std;
#include"com_h.H"
//初始化一个链表
Status InitStack(LinkStack &S)//构造一个空栈S,栈顶指针置空
{
S=NULL;//没有必要设置头结点
return OK;
}
//入栈操作
Status Push(LinkStack &S,SElemType e)
{//链栈在入栈前不需要判断栈是否满,只需要为入栈元素分配一个结点空间
StackNode *p;
p=new StackNode;//生成新结点
p->data=e;//将新结点数据域置为e
p->next=S;//将新结点插入栈顶
S=p;//修改栈顶指针为p;
return OK;
}
//出栈操作
Status Pop(LinkStack &S,SElemType &e)
{//链栈在出栈前需要判断栈是否为空,且需要释放栈顶空间
//删除S的栈顶元素,用e值返回
if(S==NULL)
{
return ERROR;//空栈
}
else
{
StackNode *p=S;
e=S->data;
//用p临时保存栈顶元素空间,以备释放
S=S->next;//修改栈顶指针
delete p;//释放原栈顶元素的空间
return OK;
}
}
//取栈顶元素
SElemType GetTop(LinkStack S)
{
//返回栈顶元素,不修改栈顶指针
if(S!=NULL)//栈非空
{
return S->data;//返回栈顶元素的值,栈顶元素不变
}
}
void printStack(LinkStack S)
{
cout<<"栈顶->";
StackNode *p=S;
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
int ListLength(LinkStack S)
{ //返回L中数据元素个数
LinkStack p=S->next; //p指向第一个结点
int count=1;
while(p)
{
//遍历单链表,统计结点数
++count;
p=p->next;
}
return count;
}
//判断是否为空
Status IsEmpty(LinkStack S)
{
if(S==NULL)
{
return OK;
}
else
{
return ERROR;
}
}
//摧毁链栈
Status Destroy(LinkStack &S)
{
LinkStack p;
while(S!=NULL)
{
p=S->next;
delete S;
S=p;
}
return OK;
}
//清空栈
Status Clear(LinkStack &S)
{
S=NULL;
return OK;
}
void menu1()
{
cout<<"\t\t\t》》》》请输入链表栈的操作《《《《"<<endl;
cout<<"\t\t\t>>>1.入栈"<<endl;
cout<<"\t\t\t>>>2.出栈"<<endl;
cout<<"\t\t\t>>>3.取栈顶元素"<<endl;
cout<<"\t\t\t>>>4.输出链栈的长度"<<endl;
cout<<"\t\t\t>>>5.判断链表是否为空"<<endl;
cout<<"\t\t\t>>>6.清空一个链表"<<endl;
cout<<"\t\t\t>>>7.摧毁一个链表"<<endl;
cout<<"\t\t\t>>>0.结束程序!"<<endl;
}
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define MaxSize 100
typedef int Status;
typedef int SElemType;
//链栈的存储结构 ,通常用单链表来实现
typedef struct StackNode
{
SElemType data;
struct StackNode *next;
} StackNode,*LinkStack;//栈顶插入和删除,链表头部作为栈顶
//链栈
extern Status InitStack(LinkStack &S);
extern Status Push(LinkStack &S,SElemType e);
extern Status Pop(LinkStack &S,SElemType &e);
extern SElemType GetTop(LinkStack S);
extern void printStack(LinkStack S);
extern int ListLength(LinkStack S);
extern Status IsEmpty(LinkStack S);
extern Status Clear(LinkStack &S);
extern Status Destroy(LinkStack &S);
extern void menu1();
链栈的基本操作,包含初始化一个链表 ,入栈操作,出栈操作,取栈顶元素,判断是否为空 ,摧毁链栈 ,清空栈 。