栈的基本操作(顺序表)
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;}
}
}
}
这是栈的基本操作,直接上代码:
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;}
}
}
}
相关文章推荐
- HIHO Drinking Game
- UML之用例图
- Noip2013火柴排队题解
- 浅谈计算机中的存储模型-(虚拟存储)
- Makefile
- 一看就会Android之SQLite中事务的使用
- LeetCode 089 Gray Code
- 程序员理想
- 为经典版eclipse增加web and JavaEE插件
- iOS JSON的选项NSJSONReadingMutableLeaves
- HDU3584 Cube
- Android开发手记一 NDK编程实例
- 生命是一场旅程,人生是一个过程。走得越远,去的地方越多,越清楚自己要什么生活。
- bzoj1072: [SCOI2007]排列perm
- pch文件的创建与设置
- [24]Swap Nodes in Pairs
- Spring MVC 如何上传多个文件到指定位置
- iOS中触摸事件响应者链条
- pga_aggregate_target和_pga_max_size都不能绝对限制实际PGA的使用
- 如何识别网页类型(wap页面还是wise页面)