当前位置:网站首页>循环队列的基本操作(实验)
循环队列的基本操作(实验)
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
边栏推荐
猜你喜欢
微信小程序客服接入,实现发送和接收消息
setcontext getcontext makecontext swapcontext
KVM learning resources
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
01-nio basic ByteBuffer and filechannel
x509证书cer格式转pem格式
倒计时1天~2022云容灾产品线上发布会即将开始
Nacos作为配置中心(四) 使用Demo
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
利用json-server在本地创建服务器请求
随机推荐
js 递归(1)
微信小程序客服接入,实现发送和接收消息
redis 模块编程中 key value的生命周期
DP - [noip2000] grid access
进入新公司,运维工程师从下面这几项了解系统的部署
openstack理论知识
Algorithem_ReverseLinkedList
Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
错误:无法远程查找到密钥 “428F7ECC7117F726“
MySQL数据库讲解(七)
redis数据库讲解(四)主从复制、哨兵、Cluster群集
编译Openssl
JS key value judgment
浅谈skiplist在LevelDB的应用
拨开云雾synchronized使用五种方式介绍
STD:: map and STD:: vector memory free
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
字节面试编程题:最小的K个数
js 格式化时间
获取线程返回值Future接口与FutureTask类使用介绍