顺序栈

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -2
#define OVERFLOW -1
#define STACK_INIT_SIZE 15
#define STACK_INCREMENT 10

#include
#include

using namespace std;

typedef int Status;  //Status 相当于 int

typedef struct Stack
{
 int * base;
 int * top;
 int stacksize;
}SqStack; 

//***********************函数的声明*****************************

void  InitStack(SqStack &S) //创建一个空栈
{

 S.base = (int *)malloc(sizeof(int) * STACK_INIT_SIZE);
 if(!S.base ) exit(OVERFLOW); //存储分配失败
 S.top = S.base;
 S.stacksize = STACK_INIT_SIZE;

 int len;
 int * p = S.base;
 cout<<"请输入初始栈的元素个数:";
 cin>>len;

 cout<<"请输入"<<len<<"个元素,以空格隔开:";
 while(len--) cin >> *p ++;

 S.top = p;

 return ;
}
Status StackEmpty(SqStack S)//判断是否为空
{
 if(S.top == S.base)
 {
  cout<<"栈为空!"<<endl;
  return OK;
 }
 else return ERROR;
}
void PrintStack(SqStack S)//输出当前顺序表
{
 int * p = S.base ;
 cout<<"栈中的元素为:";
 while( p != S.top )
  cout<<*p ++<<" ";
 cout<<endl;
}
void Push(SqStack &S )//插入元素e为新的栈顶元素
{
 int e;
 cout<<"请输入要入栈的元素:";
 cin>>e;

 if(S.top - S.base == S.stacksize )
 {
  S.base = (int *)realloc(S.base , sizeof(int)* (S.stacksize + STACK_INCREMENT)); 
  if(!S.base) exit(OVERFLOW);

  S.top = S.base +S.stacksize;
  S.stacksize += STACK_INCREMENT;
 }
 * S.top++ = e;
}
void Pop(SqStack &S)//出栈,出栈元素为e
{
 if(StackEmpty(S)) return ;
 cout<<"出栈成功!出栈元素为"<<* --S.top<<endl;
}
void StackLength(SqStack S)//返回栈的长度

 cout<<"栈的长度为:" <<S.top-S.base<<endl;
}
void GetTop(SqStack S)
{
 if(StackEmpty(S)) return;
 cout<<"栈顶元素为:"<< *(S.top-1)<<endl;
}

//***********************主函数*****************************
int main(void)
{
 SqStack S;//定义一个栈
 int door = 1,init = 0,num;

 while( door )
 
  printf("*****************************************************************\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");
  cout<<" 请输入数字进行操作:";
  cin>>num;
  if(num==0||num==1||num==2||num==3||num==4||num==5||num==6)
  {
   switch(num)
   {
    case 0: door = 0; break;
    case 1: InitStack(S); init = 1; break;
    case 2:
     if(init){ Push(S); break;}
     cout<<"请先对顺序表进行初始化!"<<endl;break;
    case 3: 
     if(init){Pop(S);  break;}
     cout<<" 请先对顺序表进行初始化!"<<endl;break;
    case 4: 
     if(init){ PrintStack(S); break; }
     cout<<" 请先对顺序表进行初始化!"<<endl;break;
    case 5: 
     if(init){ StackLength(S); break;}
     cout<<" 请先对顺序表进行初始化!"<<endl;break;
    case 6: 
     if(init){ GetTop(S); break;}
     cout<<" 请先对顺序表进行初始化!"<<endl;break;
   }
  }
  else
  cout<<"输入不正确请重新输入!"<<endl; continue; }
 }
 return 0;
}