当前位置:网站首页>循环队列的基本操作(实验)
循环队列的基本操作(实验)
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
边栏推荐
猜你喜欢

线程组ThreadGroup使用介绍+自定义线程工厂类实现ThreadFactory接口

Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34

man man随记和crontab的@reboot用法

Logback logger and root

Processing MKDIR: unable to create directory 'AAA': read only file system

krpano全景之vtour文件夹和tour

Visio画拓扑图随记

After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items

Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)

利用json-server在本地创建服务器请求
随机推荐
kprobe 的 3 种使用
01-nio basic ByteBuffer and filechannel
man man随记和crontab的@reboot用法
线程间控制之Semaphore使用介绍
mysql 5.1升级到5.69
js 进度条,显示加载进度
krpano全景之vtour文件夹和tour
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
Algorithem_ReverseLinkedList
Preview CSV file
yml引用其他变量
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
Operation instructions of star boundary text automatic translator
Mysql的安装过程(已经安装成功的步骤说明)
MySQL数据库讲解(八)
Visio画拓扑图随记
xx项目架构随记
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
dp-能量项链
Redis数据库讲解(一)