链栈

#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("              ++++++++++++++++++++++++++++++\n");

  printf(" 请输入数字进行操作:");
  scanf("%d",&num);
  if(num==0||num==1||num==2||num==3||num==4||num==5||num==6||num==7)
  {
   switch( num )
   {
    case 0:
     door = 0;
     break;
    case 1:
     InitStack( S );
     init=1;
     break;  
    case 2:
     if(init)
      TraverseStack(S);
     else printf("请先对顺序表进行初始化!\n"); 
     break;
    case 3:
     if(init) Push(S);
     else  printf("请先对顺序表进行初始化!\n");
     break;
    case 4:
     if(init) Pop(S);
     else  printf("请先对顺序表进行初始化!\n");
     break;
    case 5:
     if(init) StackLength(S);
     else  printf("请先对顺序表进行初始化!\n");
     break;
    case 6:
     if(init) GetTop(S);
     else  printf("请先对顺序表进行初始化!\n");
     break;
   }
  }
 }

 return 0;
}