当前位置:网站首页>循环队列的基本操作(实验)
循环队列的基本操作(实验)
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
边栏推荐
猜你喜欢
常见存储类型和FTP主被动模式解析
Installation and use of postman pit
A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
MySQL数据库讲解(七)
什么是云迁移?云迁移的四种模式分别是?
统信UOS卸载php7.2.24,安装php7.4.27 ;卸载再安装为PHP 7.2.34
云迁移的六大场景
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
setcontext getcontext makecontext swapcontext
随机推荐
JumpServer
MySQL基础知识
IE8 browser prompts whether to block access to JS script
TLS/SSL 协议详解 (28) TLS 1.0、TLS 1.1、TLS 1.2之间的区别
统信UOS PHP7.2.3升级至PHP7.2.24
Docker篇 (五) MySQL的安装
線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
STD:: map and STD:: vector memory free
Flop effect
Recyclerview advanced use (I) - simple implementation of sideslip deletion
ansible及常用模块的使用
微信小程序轮播图swiper
xx项目架构随记
一些小小小小记录~
SED 学以致用
How to do a project easily
OpenSSH的升级、版本号的修改
js 键值判断
TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
grep无法重定向到文件的问题