当前位置:网站首页>循环队列的基本操作(实验)
循环队列的基本操作(实验)
2022-04-23 14:11:00 【白衣折扇y】
前言
编程实现循环队列的以下基本操作:建队列,取队头元素,入队,出队。
想要了解循环队列的详细讲解请点我哦
️个人主页: 收藏加关注,永远不迷路~ ️
下面就让我们一起来看看怎么实现吧
一、代码实现
#include <iostream>
using namespace std;
#define ERROR -1
#define OK 1
typedef int QElemType;
typedef int Status;
//循环队列的存储结构
#define MAXQSIZE 100 //最大队列长度
typedef struct
{
QElemType *base; //用于动态分配存储空间
int front; //队头索引
int rear; //队尾索引
} SqQueue;
//初始化
void InitQueue (SqQueue &Q)
{
//构造一个空队列
Q.base =new QElemType[MAXQSIZE];
Q.front=Q.rear=0;
}
//销毁队列
void DestroyQueue(SqQueue &Q)
{
if(Q.base)
free(Q.base);
Q.base = NULL;
Q.front = Q.rear = 0;
}
//清空队列
void ClearQueue(SqQueue &Q)
{
Q.front=Q.rear=0;
}
//求长度
int QueueLength (SqQueue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
//判空
bool QueueEmpty (SqQueue Q)
{
return (Q.front==Q.rear);
}
//求队头元素
Status GetHead (SqQueue Q, QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
return OK;
}
//循环队列入队
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear = (Q.rear+1) % MAXQSIZE;
return OK;
}
//循环队列出队
Status DeQueue (SqQueue &Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1) % MAXQSIZE;
return OK;
}
//遍历使队列显示
void DisplayQueue(SqQueue Q)
{
int i=Q.front;
while(Q.front!=Q.rear && (i+MAXQSIZE) % MAXQSIZE != Q.rear)
{
cout<<Q.base[i]<<endl;
i++;
}
}
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();
SqQueue Q;
InitQueue(Q);
QElemType e;
int i;
while(1)
{
cout<<"请输入操作代码:";
cin>>operate_code;
if(operate_code=='1')
{
cout<<"The queue has been cleared."<<endl;
ClearQueue(Q);
}
else if (operate_code=='2')
{
if(QueueEmpty(Q))
cout<<"The queue is empty."<<endl;
else
cout<<"The queue is not empty."<<endl;
}
else if (operate_code=='3')
{
cout<<"The length of queue is:"<<QueueLength(Q)<<endl;
}
else if (operate_code=='4')
{
cout<<"队头元素为:"<<endl;
if(GetHead(Q,e) == 1) cout<<e<<endl;
else cout <<"error"<<endl;
}
else if (operate_code=='5')
{
cout<<"请输入你想要入队的数:"<<endl;
cin>>e;
EnQueue(Q,e);
}
else if (operate_code=='6')
{
cout<<"出队元素为:"<<endl;
if(DeQueue(Q,e)) cout<<e<<endl;
}
else if (operate_code=='7')
{
cout<<"The contents of the queue are:"<<endl;
DisplayQueue(Q);
}
else if (operate_code=='0')
{
break;
}
else
{
cout<<"\n操作码错误!!!"<<endl;
show_help();
}
}
//调用销毁栈函数
DestroyQueue(Q);
return 0;
}
二、运行结果
总结
本文用来介绍数据结构中循环队列的代码实现过程及运行结果示例。用菜单样式显示循环队列的初始化、清空、销毁、求长度、求队头元素、判空、入队、出队以及遍历队列使其显示等操作。
版权声明
本文为[白衣折扇y]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_54439023/article/details/124240760
边栏推荐
- Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
- MYSQL一种分表实现方案及InnoDB、MyISAM、MRG_MYISAM等各种引擎应用场景介绍
- MySQL数据库讲解(九)
- STD:: map and STD:: vector memory free
- 微信小程序轮播图swiper
- 微信小程序将原生请求通过es6的promise来进行优化
- 剑指offer刷题(1)--面向华为
- 在Clion中给主函数传入外部参数
- 政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
- TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
猜你喜欢
随机推荐
Five ways of using synchronized to remove clouds and fog are introduced
如何轻松做好一个项目
第四届“传智杯”全国大学生IT技能大赛(决赛B组) 题解
在电视屏幕上进行debug调试
翻牌效果
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
GFS分布式文件系统(理论)
DP energy Necklace
KVM learning resources
redis数据库讲解(三)redis数据类型
解决ssh配置文件优化以及连接慢的问题
Uni app message push
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
微信小程序轮播图swiper
MySQL数据库讲解(九)
在Clion中给主函数传入外部参数
Returns the subscript after array sorting
flannel 原理 之 子网划分
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
js 递归(1)