当前位置:网站首页>顺序栈的基本操作
顺序栈的基本操作
2022-04-23 14:11:00 【白衣折扇y】
新人小白的博客
️希望大家多多关注
以后会经常更新哒~
️个人主页: 收藏加关注,永远不迷路~ ️
前言
编程实现栈的以下基本操作:建栈,取栈顶元素,入栈,出栈。
️个人主页: 收藏加关注,永远不迷路~ ️
下面就让我们一起来看看怎么实现吧

一、代码实现
顺序栈是栈的顺序实现。顺序栈是指利用顺序存储结构实现的栈。采用地址连续的存储空间(数组)依次存储栈中数据元素,由于入栈和出栈运算都是在栈顶进行,而栈底位置是固定不变的,可以将栈底位置设置在数组空间的起始处;栈顶位置是随入栈和出栈操作而变化的,故需用一个整型变量top来记录当前栈顶元素在数组中的位置。采用顺序存储结构的栈称为顺序栈(sequence stack)。设数组data[MAXSIZE]为栈的存储空间,其中MAXSIZE是一个预先设定的常数,为允许进栈结点的最大可能数目,即栈的容量。
下面是实现顺序栈的基本操作的代码:
#include <iostream>
using namespace std;
//栈的顺序存储表示
#define MAXSIZE 100
#define ERROR -1
#define OK 1
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//初始化
void InitStack( SqStack &S )
{
//构造一个空栈
S.base =new SElemType[MAXSIZE];
S.top = S.base;
S.stacksize = MAXSIZE;
}
//判空
bool StackEmpty( SqStack S )
{
if(S.top == S.base) return true;
else return false;
}
//长度
int StackLength( SqStack S )
{
return S.top - S.base;
}
//清空
void ClearStack( SqStack &S )
{
S.top = S.base;
}
//销毁
void DestroyStack( SqStack &S )
{
if( S.base )
{
delete [] S.base ;
S.stacksize = 0;
S.base = S.top = NULL;
}
}
//入栈
Status Push ( SqStack &S, SElemType e)
{
//插入元素e为新的栈顶元素
if ( S.top-S.base==S.stacksize )
return ERROR;
*S.top++ = e;
return OK;
}
//出栈
Status Pop ( SqStack &S, SElemType & e)
{
//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
if ( S.top == S.base ) return ERROR;
e=*--S.top;
return OK;
}
//取栈顶元素
Status GetTop( SqStack S, SElemType &e)
{
// 若栈不空,则用e返回S的栈顶元素
if( S.top == S.base ) return ERROR;
e = *( S.top - 1 );
return OK;
}
//显示栈
DisplayStack(SqStack &S)
{
SElemType *p;
if( S.top == S.base ) return ERROR;
p=S.top;
while(p>S.base){
p--;
cout<<*p<<endl;
}
return OK;
}
void show_help()
{
cout<<"******* Data Structure ******"<<endl;
cout<<"1----清空栈"<<endl;
cout<<"2----判断栈是否为空"<<endl;
cout<<"3----求栈长度"<<endl;
cout<<"4----取栈顶元素"<<endl;
cout<<"5----入栈"<<endl;
cout<<"6----出栈"<<endl;
cout<<"7----显示栈"<<endl;
cout<<" 退出,输入0"<<endl;
}
int main()
{
char operate_code;
show_help();
SqStack S;
InitStack(S);
SElemType e;
int i;
while(1)
{
cout<<"请输入操作代码:";
cin>>operate_code;
if(operate_code=='1')
{
cout<<"The stack has been cleared."<<endl;
ClearStack(S);
}
else if (operate_code=='2')
{
if(StackEmpty(S))
cout<<"The stack is empty."<<endl;
else
cout<<"The stack is not empty."<<endl;
}
else if (operate_code=='3')
{
cout<<"The length of stack is:"<<StackLength(S)<<endl;
}
else if (operate_code=='4')
{
cout<<"栈顶元素为:"<<endl;
if(GetTop(S,e) == 1) cout<<e<<endl;
else cout <<"error"<<endl;
}
else if (operate_code=='5')
{
cout<<"请输入你想要插入的数:"<<endl;
cin>>e;
Push(S,e);
}
else if (operate_code=='6')
{
cout<<"出栈元素为:"<<endl;
if(Pop(S,e)) cout<<e<<endl;
}
else if (operate_code=='7')
{
cout<<"The contents of the stack are:"<<endl;
DisplayStack(S);
}
else if (operate_code=='0')
{
break;
}
else
{
cout<<"\n操作码错误!!!"<<endl;
show_help();
}
}
//调用销毁栈函数
DestroyStack(S);
return 0;
}
二、运行结果


总结
本文用来介绍数据结构中顺序栈的代码实现过程及运行结果示例。用菜单样式显示顺序栈的初始化、清空、销毁、判空、求长度、取栈顶元素、入栈、出栈以及遍历栈使其显示等操作。

版权声明
本文为[白衣折扇y]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_54439023/article/details/124241314
边栏推荐
猜你喜欢
随机推荐
sar命令详解
MySQL数据库讲解(七)
OpenStack命令操作
线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口
MySQL-InnoDB-事务
OpenSSH的升级、版本号的修改
js 进度条,显示加载进度
Mysql的安装过程(已经安装成功的步骤说明)
Arrays类的使用案例
On the problem of cliff growth of loss function in the process of training
redis 模块编程中 key value的生命周期
获取线程返回值Future接口与FutureTask类使用介绍
金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务
Nacos作为配置中心(四) 使用Demo
浅谈skiplist在LevelDB的应用
某政务云项目业务系统迁移调研实践
JS progress bar, displaying the loading progress
Wechat applet rotation map swiper
elk安装
redis数据库讲解(四)主从复制、哨兵、Cluster群集









