#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -2
#define OVERFLOW -1
#include
#include
using namespace std;
typedef int Status; //Status
相当于 int
typedef struct Node
{
int
elem;
struct Node
*
next;
}Node;//结点
typedef struct Stack
{
Node * base;
Node * top;
}LinkStack; //栈
//***********************函数的声明*****************************
void InitStack(LinkStack
&S) //创建一个空栈
{
S.base = (Node *)malloc(sizeof(Node) );
if(!S.base )
exit(OVERFLOW); //存储分配失败
S.top = S.base;
cout<<"初始化成功"<<endl;
return ;
}
Status StackEmpty(LinkStack S)//判断是否为空
{
if(S.top == S.base)
{
cout<<"栈为空!"<<endl;
return TRUE;
}
else return FALSE;
}
void TraverseStack(LinkStack S)//输出当前顺序表
{
if(StackEmpty(S)) return ;
Node * p = S.top ;
cout<<"栈中的元素为:";
while( p != S.base )
{
cout<<p->elem<<"
";
p = p->next;
}
cout<<endl;
}
void Push(LinkStack &S )//插入元素e为新的栈顶元素
{
int e;
cout<<"请输入要入栈的元素:";
cin>>e;
Node * pNew = (Node *)malloc(sizeof(Node));
if(pNew == NULL ) exit(OVERFLOW);
pNew->elem = e;
pNew->next = S.top ;
S.top = pNew;
}
void Pop(LinkStack &S)//出栈,出栈元素为e
{
if(StackEmpty(S)) return ;
Node * p = S.top;
p->elem = S.top->elem ;
S.top = S.top->next;
cout<<"出栈成功!出栈元素为"<<p->elem
<<endl;
free(p);
}
void StackLength(LinkStack S)//返回栈的长度
{
int len = 0;
if(StackEmpty(S)) return ;
Node * p = S.top;
while(p != S.base )
{
len++;
p = p->next;
}
cout<<"栈的长度为:"
<<len<<endl;
}
void GetTop(LinkStack S)
{
if(StackEmpty(S)) return;
cout<<"栈顶元素为:"<< S.top->elem
<<endl;
}
//***********************主函数*****************************
int main(void)
{
LinkStack S;//定义一个栈
int num,door = 1,init =0;
while( door )
{
printf("\n-------------------------------------------------------------\n");
printf("
如要对链栈进行操作,请输入各项前面相应的数字\n");
printf("
++++++++++++++++++++++++++++++\n");
printf("
| 1、初始化链栈
|\n");
printf("
| 2、查看当前链栈
|\n");
printf("
| 3、入栈
|\n");
printf("
| 4、出栈
|\n");
printf("
| 5、查看链栈长度
|\n");
printf("
| 6、查看链栈栈顶元素
|\n");
printf("
| 0、退出
|\n");
printf("