当前位置:网站首页>顺序栈的基本操作
顺序栈的基本操作
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
边栏推荐
猜你喜欢
Operation instructions of star boundary automatic text translator (advanced version)
API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
关于UDP接收icmp端口不可达(port unreachable)
常见存储类型和FTP主被动模式解析
KVM学习资源
Use the executors class to quickly create a thread pool
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
随机推荐
About the configuration and use of json5 in nodejs
MySQL lock database lock
DP energy Necklace
JS progress bar, displaying the loading progress
GFS分布式文件系统(理论)
Use the executors class to quickly create a thread pool
01-nio basic ByteBuffer and filechannel
js 格式化时间
Introduction to loan market quotation interest rate (LPR) and loan benchmark interest rate
进入新公司,运维工程师从下面这几项了解系统的部署
dp-能量项链
Redis数据库讲解(一)
source insight via samba
KVM learning resources
ssh限制登录的四种手段
操作系统常见面试题目:
std::map 和 std::vector 内存释放
Docker篇 (五) MySQL的安装
微信小程序轮播图swiper
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能