#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef enum{OK,ERROR,TURE,FLASE}Status;
typedef struct
{
int *base;
int *top;
//栈容量
int stacksize;
}SqStack;
//顺序栈的初始化
Status InitStack(SqStack& S)
{
S.base = new int[MAXSIZE];
if(!S.base)exit(1);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
//push
Status Push(SqStack& S,int e)
{
if(S.top - S.base == S.stacksize)
return ERROR;
*S.top++ = e;
return OK;
}
//POP
Status Pop(SqStack& S,int& e)
{
if(S.base == S.top)return ERROR;
e = *--S.top;
return OK;
}
//遍历
Status StackTraverse(SqStack& S)
{
if(S.base == S.top)exit(1);
int *p = S.base;
while(p != S.top)
{
cout<<*p++<<" ";
}
return OK;
}
//Gettop
int GetTop(SqStack& S)
{
if(S.base == S.top)exit(1);
return *(S.top-1);
}
//判断站是否为空
Status StackEmpty(SqStack& S)
{
if(S.top == S.base)return FLASE;
else
{
return TURE;
}
}
//取栈长度
Status StackLength(SqStack& S,int& length)
{
if(S.base == S.top)exit(1);
length = S.top - S.base;
return OK;
}
//清空栈
Status ClearStack(SqStack& S)
{
if(S.base == S.top)exit(1);
int * p = S.base;
while(p != S.top)
{
*p++ = 0;
}
return OK;
}
//销毁
Status DestroyStack(SqStack& S)
{
if(S.top == S.base)exit(1);
delete [] S.base;
S.base = NULL;
S.top = NULL;
return OK;
}
int main()
{
Status Sta;
SqStack S;
Sta = InitStack(S);
if(Sta == OK)
{
cout<<"栈的初始化成功!"<<endl;
}
int N,length;
cout<<"十进制: ";
cin>>N;
while(N)
{
Push(S,N%8);
N = N / 8;
}
StackLength(S,length);
cout<<"栈的长度: "<<length<<endl;
cout<<"栈的遍历: ";
Sta = StackTraverse(S);
if(Sta == OK)cout<<endl<<"遍历成功!"<<endl;
int e = 0;
cout<<"八进制: ";
while(StackEmpty(S) == TURE)
{
Pop(S,e);
cout<<e;
}
cout<<endl;
Sta = DestroyStack(S);
if(Sta == OK)cout<<"成功销毁栈!"<<endl;
return 0;
}