一、顺序栈代码
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int top;
}Sqstack;
Status InitStack(Sqstack &S);
Status Push(Sqstack &S,ElemType e);
Status Pop(Sqstack &S,ElemType &e);
Status GetPop(Sqstack S,ElemType &e);
Status StackEmpty(Sqstack S);
void Show(Sqstack S);
//初始化
Status InitStack(Sqstack &S)
{
S.elem=new ElemType[MAXSIZE];
if(!S.elem) return ERROR;
S.top =-1;
return OK;
}
//入栈
Status Push(Sqstack &S,ElemType e)
{
if(S.top ==MAXSIZE-1) return ERROR;
S.top++;
S.elem[S.top]=e;
return OK;
}
//出栈
Status Pop(Sqstack &S,ElemType &e)
{
if(S.top==-1) return ERROR;
e=S.elem[S.top];
S.top--;
return OK;
}
//取栈顶元素
Status GetPop(Sqstack S,ElemType &e)
{
if(S.top==-1) return ERROR;
e=S.elem [S.top];
return OK;
}
//判断栈是否为空
Status StackEmpty(Sqstack S)
{
if(S.top==-1)
return OK;
else
return ERROR;
}
//输出元素
void Show(Sqstack S)
{
if(S.top==-1)
{
cout<<"没有数据"<<endl;
return ;
}
while(S.top!=-1)
{
cout<<S.elem[S.top]<<" ";
S.top--;
}
cout<<endl;
}
int main()
{
Sqstack S;
ElemType e;
int choice;
while(1)
{
cout<<"1、初始化 2、入栈 3、出栈 4、显示"<<endl;
cout<<"5、判断栈空 6、取栈顶元素 0、退出"<<endl;
cout<<"输入选项=";
cin>>choice;
switch(choice)
{
case 1:
InitStack(S);
cout<<"初始化成功!"<<endl;
break;
case 2:
cout<<"入栈数据=";
cin>>e;
if(Push(S,e)==OK)
cout<<"输入成功!"<<endl;
else
cout<<"输入失败!"<<endl;
break;
case 3:
if(Pop(S,e)==OK)
{
cout<<"出栈成功!"<<endl;
cout<<"出栈数据="<<e<<endl;
}
else
cout<<"出栈失败!"<<endl;
break;
case 4:
Show(S);
break;
case 5:
if(StackEmpty(S)==OK)
cout<<"栈空!"<<endl;
else
cout<<"栈非空!"<<endl;
Show(S);
break;
case 6:
if(GetPop(S,e)==OK)
{
cout<<"栈顶元素="<<e<<endl;
}
else
cout<<"栈空!"<<endl;
break;
case 0:
return 0;
default:
cout<<"选项输入错误!"<<endl;
}
system("pause");
system("cls");
}
return 0;
}
二、链栈代码
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
//定义存储结构
typedef struct Node
{
ElemType data;
struct Node *next;
}StackNode,*LinkStack;
Status Initstack(LinkStack &S);
Status Push(LinkStack &S,ElemType e);
Status Pop(LinkStack &S,ElemType &e);
Status GetPop(LinkStack S,ElemType &e);
Status StackEmpty(LinkStack S);
void Show(LinkStack S);
//初始化
Status Initstack(LinkStack &S)
{
S=NULL;
return OK;
}
//入栈(头插法)
Status Push(LinkStack &S,ElemType e)
{
StackNode *p;
p=new StackNode ;
if(!p) return ERROR;
p->data=e;
p->next=S;
S=p;
return OK;
}
//出栈
Status Pop(LinkStack &S,ElemType &e)
{
StackNode *p;
if(S==NULL) return ERROR;
e=S->data;
p=S;
S=S->next;
delete p;
return OK;
}
//取栈顶元素
Status GetPop(LinkStack S,ElemType &e)
{
if(S==NULL) return ERROR;
e=S->data;
return OK;
}
//判断栈是否为空
Status StackEmpty(LinkStack S)
{
if(S==NULL)
return OK;
else
return ERROR;
}
//输出函数
void Show(LinkStack S)
{
StackNode *p;
p=S;
if(S==NULL)
{
cout<<"没有数据!"<<endl;
return ;
}
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int main()
{
LinkStack S;
ElemType e;
int choice;
while(1)
{
cout<<"1、初始化 2、入栈 3、出栈 4、显示"<<endl;
cout<<"5、判断栈空 6、取栈顶元素 0、退出"<<endl;
cout<<"输入选项=";
cin>>choice;
switch(choice)
{
case 1:
Initstack(S);
cout<<"初始化成功!"<<endl;
break;
case 2:
cout<<"入栈数据=";
cin>>e;
if(Push(S,e)==OK)
cout<<"输入成功!"<<endl;
else
cout<<"输入失败!"<<endl;
break;
case 3:
if(Pop(S,e)==OK)
{
cout<<"出栈成功!"<<endl;
cout<<"出栈数据="<<e<<endl;
}
else
cout<<"出栈失败!"<<endl;
break;
case 4:
Show(S);
break;
case 5:
if(StackEmpty(S)==OK)
cout<<"栈空!"<<endl;
else
cout<<"栈非空!"<<endl;
Show(S);
break;
case 6:
if(GetPop(S,e)==OK)
{
cout<<"栈顶元素="<<e<<endl;
}
else
cout<<"栈空!"<<endl;
break;
case 0:
return 0;
default:
cout<<"选项输入错误!"<<endl;
}
system("pause");
system("cls");
}
return 0;
}