当前位置:网站首页>循环队列的基本操作(实验)
循环队列的基本操作(实验)
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
边栏推荐
- Mysql的安装过程(已经安装成功的步骤说明)
- Redis数据库讲解(一)
- MySQL数据库讲解(八)
- 快速搞懂线程实现的三种方式
- js 进度条,显示加载进度
- 正则表达式
- 百度笔试2022.4.12+编程题目:简单整数问题
- 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
- 编译Openssl
- dp-[NOIP2000]方格取数
猜你喜欢
处理 mkdir:无法创建目录“aaa“:只读文件系统
MySQL数据库讲解(十)
Operation instructions of star boundary text automatic translator
flannel 原理 之 TUN模式
A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
Recyclerview advanced use (I) - simple implementation of sideslip deletion
xx项目架构随记
Installation and use of postman pit
Nacos作为配置中心(四) 使用Demo
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
随机推荐
JumpServer
JS progress bar, displaying the loading progress
js 进度条,显示加载进度
ie8 浏览器提示是否 阻止访问js脚本
krpano全景之vtour文件夹和tour
Nacos作为配置中心(四) 使用Demo
dp-能量项链
JS key value judgment
统信UOS PHP7.2.3升级至PHP7.2.24
Notes on Visio drawing topology
Redis源码分析之HSET流程与ziplist
如何轻松做好一个项目
Recyclerview advanced use (I) - simple implementation of sideslip deletion
mysql 5.1升级到5.67
openstack理论知识
Pass in external parameters to the main function in clion
std::map 和 std::vector 内存释放
云迁移的六大场景
json date时间日期格式化
STD:: map and STD:: vector memory free