#include<iostream>
#include<malloc.h>
#define SElemType int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
using namespace std;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack( SqStack &S )
{
S.base=new SElemType[STACK_INIT_SIZE];
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return true;
}
Status DestroyStack(SqStack &S){
if(S.base){
delete[]S.base;
S.stacksize=0;
S.top=S.base=NULL;
}
return true;
}
Status ClearStack( SqStack S )
{
if(S.base) S.top=S.base;
return true;
}
bool StackEmpty( SqStack S )
{
if(S.base==S.top)
return true;
return false;
}
int StackLength( SqStack S )
{
return S.top-S.base;
}
Status Pop ( SqStack &S, SElemType & e)
{
if ( S.top == S.base ) return false;
e=*--S.top;
return true;
}//Pop
Status Circle( SqStack S){
int e,n;
while(!StackEmpty(S)){
e=*(S.top-1);
cout<<e<<" ";
Pop(S,n);
}
cout<<endl;
return 0;
}
Status GetTop( SqStack S, SElemType &e)
{ // 若栈不空,则用e返回S的栈顶元素
if( S.top == S.base )
return false;
e=*(S.top - 1 );
return true;
}
Status Push ( SqStack &S, SElemType e)
{ //插入元素e为新的栈顶元素
if ( S.top-S.base==S.stacksize ) {
S.base=(SElemType*) realloc ( S.base,
(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if (!S.base)exit(10);
S.top=S.base + S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++ = e;
return true;
}//Pus
void show(){
cout<<" 1.构造空栈"<<endl;
cout<<" 2.销毁栈结构"<<endl;
cout<<" 3.栈清空"<<endl;
cout<<" 4.栈判空"<<endl;
cout<<" 5.求栈长"<<endl;
cout<<" 6.遍历栈"<<endl;
cout<<" 7.求栈顶元素"<<endl;
cout<<" 8.入栈"<<endl;
cout<<" 9.出栈"<<endl;
}
int main()
{
SqStack S;
InitStack(S);
show();
int n;
while(1)
{
cin>>n;
switch(n){
case 1: {
InitStack(S);
break;
}
case 2: {
DestroyStack(S);
break;
} case 3: {
ClearStack(S);
break;
}
case 4: {
if(StackEmpty(S)){
cout<<"此栈是空栈"<<endl;
}else{
cout<<"此栈不是空栈"<<endl;
}
break;
}
case 5: {
cout<<StackLength(S)<<endl;
break;
}
case 6: {
Circle(S);
break;
}
case 7: {
int e;
if(GetTop(S,e)==false){
cout<<"此栈为空栈"<<endl;
}
else
cout<<GetTop(S,e)<<endl;
break;
}
case 8: {
int e;
cin>>e;
Push(S,e);
break;
}
case 9: {
int e;
cout<<Pop(S,e)<<endl;
break;
}
default:{
cout<<"操作错误"<<endl;
break;
}
}
}
return 0;
}
/*#include<iostream>
#include<malloc.h>
#define SElemType int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Status;
using namespace std;
typedef struct StackNode {
SElemType data;
struct StackNode *next;
} StackNode, *LinkStack;
void InitStack(LinkStack &S )
{
S=NULL;
}
bool StackEmpty(LinkStack &S){
if(S==NULL) return true;
return false;
}
Status Push(LinkStack &S,SElemType e){
LinkStack p;
p=new StackNode;
p->data=e;
p->next=S;
S=p;
return 0;
}
Status Pop (LinkStack &S,SElemType &e)
{
LinkStack p;
if (S==NULL) return false;
e = S-> data; p = S; S = S-> next;
delete p;
return true;
}
void show(){
cout<<" 1.构造空栈"<<endl;
cout<<" 2.栈判空"<<endl;
cout<<" 3.入栈"<<endl;
cout<<" 4.出栈"<<endl;
}
int main()
{
LinkStack S;
InitStack(S);
show();
int n;
while(1)
{
cin>>n;
switch(n){
case 1: {
InitStack(S);
break;
}
case 2: {
if(StackEmpty(S)){
cout<<"此栈是空栈"<<endl;
}else{
cout<<"此栈不是空栈"<<endl;
}
break;
}
case 3: {
int e;
cin>>e;
Push(S,e);
break;
}
case 4: {
int e;
cout<<Pop(S,e)<<endl;
break;
}
default:{
cout<<"操作错误"<<endl;
break;
}
}
}
return 0;
}*/