您的位置:首页 > 其它

栈的基本操作(顺序表)

2015-10-25 10:20 369 查看
菜鸟制作,不要笑话......

这是栈的基本操作,直接上代码:

head.h

#define OK 1
#define ERROR 0
#define STACKINITSIZE 100
#define STACKADD 10
typedef int Status;
typedef int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
void show();
Status InitStack(SqStack &S);
Status DestroyStack(SqStack &S);
Status ClearStack(SqStack &S);
Status StackEmpty(SqStack &S);
int StackLength(SqStack S);
Status GetTop(SqStack &S,SElemType &e);
Status Push(SqStack &S,SElemType e);
Status Pop(SqStack &S,SElemType &e);
Status StackTraverse(SqStack &S);

function.cpp
#include"head.h"
#include<iostream>
using namespace std;
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;
}
Status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(STACKINITSIZE*sizeof(SElemType));
S.top=S.base;
S.stacksize=STACKINITSIZE;
return OK;
}
Status DestroyStack(SqStack &S)
{
if(S.base==NULL)
return ERROR;
free(S.base);
S.base=S.top=NULL;
S.stacksize=0;
return OK;
}
Status ClearStack(SqStack &S)
{
if(S.base==NULL)
return ERROR;
S.top=S.base;
S.stacksize=0;
return OK;
}
Status StackEmpty(SqStack &S)
{
if(S.base==S.top)
return OK;
else
return ERROR;
}
int StackLength(SqStack S)
{
if(S.base==NULL)
return ERROR;
return S.top-S.base;
}
Status GetTop(SqStack &S,SElemType &e)
{
if(S.base==NULL)
return ERROR;
if(S.top==S.base)
return ERROR;
else
e=*(S.top-1);
return OK;
}
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>= STACKINITSIZE)
{
S.base=(SElemType*)realloc(S.base,(STACKINITSIZE+ STACKADD)*sizeof(SElemType));
S.top=S.base+STACKINITSIZE;
S.stacksize+=STACKADD;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}
Status StackTraverse(SqStack &S)
{
if(S.base==NULL)
return ERROR;
if(S.base==S.top)
return ERROR;
SElemType *p=S.base;
for(;p<S.top;p++)
cout<<*p<<" ";
cout<<endl;
return OK;
}


main.cpp
#include"head.h"
#include<iostream>
using namespace std;
void main()
{
SqStack S;
int n;
SElemType e;
while(1)
{
show();
cin>>n;
switch(n)
{
case 1:{
if(!InitStack(S))
cout<<"初始化失败!"<<endl;
else
cout<<"初始化成功!"<<endl;
break;
}
case 2:{
if(!DestroyStack(S))
cout<<"销毁失败!"<<endl;
else
cout<<"销毁成功!"<<endl;
break;
}
case 3:{
if(!ClearStack(S))
cout<<"清空失败!"<<endl;
else
cout<<"清空成功!"<<endl;
break;
}
case 4:
{
if(S.base==NULL)
cout<<"栈不存在!"<<endl;
if(StackEmpty(S))
cout<<"栈为空!"<<endl;
else
cout<<"栈不为空!"<<endl;
break;
}
case 5:{
if(!StackLength(S))
cout<<"栈不存在!"<<endl;
else
cout<<"栈长为:"<< StackLength(S)<<endl;
break;
}
case 6:
{
if(!GetTop(S,e))
cout<<"获取失败!"<<endl;
else
cout<<"栈顶为:"<<e<<endl;
break;
}
case 7:{
cout<<"请输入要压入的数据:"<<endl;
cin>>e;
if(!Push(S,e))
cout<<"压入失败!"<<endl;
else
cout<<"压入成功!"<<endl;
break;
}
case 8:{
if(!Pop(S,e))
cout<<"出栈失败!"<<endl;
else
cout<<"出栈成功!出栈的数据为:"<<e<<endl;
break;
}
case 9:
{
if(!StackTraverse(S))
cout<<"遍历失败!"<<endl;
else
cout<<"遍历成功!"<<endl;
break;
}
default:{cout<<"输入有误!请重新输入:"<<endl;break;}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: