《数据结构、算法与应用C++语言描述》使用C++语言实现数组循环队列

《数据结构、算法与应用C++语言描述》使用C++语言实现数组循环队列

定义

队列的定义

队列(queue)是一个线性表,其插入和删除操作分别在表的不同端进行。插入元素的那一端称为队尾(back或rear),删除元素的那一端称为队首(front)。

队列的抽象数据类型

在这里插入图片描述

数组循环队列实现代码

_17queue.h

抽象类栈。

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			队列的抽象类
*/
#pragma once
#ifndef _QUEUE_H_
#define _QUEUE_H_
template<class T>
class queue
{
public:virtual ~queue() {}virtual bool empty() const = 0;//返回true,当且仅当队列为空virtual int size() const = 0;//返回队列中元素个数virtual T& front() = 0;//返回头元素的引用virtual T& back() = 0;//返回尾元素的引用virtual void pop() = 0;//删除首元素virtual void push(const T& theElement) = 0;//把元素theELment加入队尾
};
#endif

_18arrayQueue.h

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			数组存储的队列的头文件
*/
#pragma once
#ifndef _ARRAYQUEUE_H_
#define _ARRAYQUEUE_H_
#include<sstream>
#include<iostream>
#include "_1myExceptions.h"
#include "_17queue.h"
#include <cmath>
/*测试函数*/
void arrayQueueTest();using namespace std;
template<class T>
class arrayQueue : public queue<T>
{
public:/*成员函数*/arrayQueue(int initialCapacity = 10);~arrayQueue() { delete[] queue; }bool empty() const { return theFront == theBack; }int size() const //返回队列的元素个数{return (queueLength - theFront + theBack) % queueLength;}void clear() { theFront = theBack = 0; }/*清空队列中的元素*/int capacity() const { return queueLength-1; }//返回第一个元素T& front(){if (theFront == theBack)throw queueEmpty();return queue[(theFront + 1) % queueLength];}//返回最后一个元素T& back(){if (theFront == theBack)throw queueEmpty();return queue[theBack];}//删除队首元素void pop(){if (theFront == theBack)throw queueEmpty();theFront = (theFront + 1) % queueLength;queue[theFront].~T();}//向队尾插入元素theElementvoid push(const T& theElement);/*调整队列容量大小*/void resizeQueue(int newLength);void meld(arrayQueue<T>& a, arrayQueue<T>& b);//合并队列a,b到当前队列void split(arrayQueue<T>& a, arrayQueue<T>& b);//将当前队列分成两个队列a,b/*重载操作符*//*重载[]操作符*/T operator[](int i){ return queue[(theFront + i + 1) % queueLength]; }/*友元函数*/friend istream& operator>> <T>(istream& in, arrayQueue<T>& m);//输出但是不pop()元素friend ostream& operator<< <T>(ostream& out, arrayQueue<T>& m);
private:int theFront;       // 第一个元素的前一个位置int theBack;        // 最后一个元素的位置int queueLength;    // 队列的容量,实质上比队列容量(不包含queueFront指向的那一个位置)大1T* queue;           // 指向队列首地址的指针
};
/*友元函数*/
/*>>操作符*/
template<class T>
istream& operator>>(istream& in, arrayQueue<T>& m)
{int numberOfElement = 0;cout << "Please enter the number of element:";while (!(in >> numberOfElement)){in.clear();//清空标志位while (in.get() != '\n')//删除无效的输入continue;cout << "Please enter the number of element:";}T cinElement;for (int i = 0; i < numberOfElement; i++){cout << "Please enter the element " << i + 1 << ":";while (!(in >> cinElement)){in.clear();//清空标志位while (in.get() != '\n')//删除无效的输入continue;cout << "Please enter the element " << i + 1 << ":";}m.push(cinElement);}return in;
}
/*<<操作符*/
template<class T>
ostream& operator<<(ostream& out, arrayQueue<T>& m)
{int size = m.size();for (int i = 0; i < size; i++)out << m.queue[(m.theFront + i + 1) % m.queueLength] << "  ";out << endl;return out;
}
/*成员函数*/
/*构造函数*/
template<class T>
arrayQueue<T>::arrayQueue(int initialCapacity)
{if (initialCapacity < 1){ostringstream s("");s << "Initial capacity = " << initialCapacity << "Must be > 0";throw illegalParameterValue(s.str());}queue = new T[initialCapacity+1];queueLength = initialCapacity+1;theFront = theBack = 0;
}/*向队尾插入元素theElement*/
template<class T>
void arrayQueue<T>::push(const T& theElement)
{//首先检查队列是否已满,如已满,则将队列容量加倍if ((theBack + 1) % queueLength == theFront)resizeQueue(2 * (queueLength-1));    theBack = (theBack + 1) % queueLength;queue[theBack] = theElement;
}
/*调整队列容量大小*/
template<class T>
void arrayQueue<T>::resizeQueue(int newLength)
{T* temp = new T[newLength + 1];int size = min((*this).size(), newLength);for (int i = 0; i < size; i++)temp[i] = queue[(theFront + i + 1) % queueLength]; queueLength = newLength+1;theFront = newLength;theBack = size - 1;delete[] queue;queue = temp;
}/*
创建一个新的队列,该表包含了a和b中的所有元素,其中a和b的元素轮流出现,表中的首
元素为a中的第一个元素。在轮流排列元素时,如果某个队列的元素用完了,则把另一个队列的其
余元素依次添加在新队列的后部。代码的复杂性应与两个输入队列的长度呈线性比例关系。
归并后的线性队列是调用对象*this
*/
template <class T>
void arrayQueue<T>::meld(arrayQueue<T>& a, arrayQueue<T>& b)
{(*this).clear();int i = 0;while (i < a.size() && i < b.size()){push(a[i]);push(b[i]);i++;}while (i < a.size()){push(a[i]);i++;}while (i < b.size()){push(b[i]);i++;}
}/*生成两个线性队列a和b,a包含*this中索引为奇数的元素,b包含其余的元素*/
template<class T>
void arrayQueue<T>::split(arrayQueue<T>& a, arrayQueue<T>& b)
{a.clear();b.clear();int size = (*this).size();for (int i = 0; i < size; i++){if (i % 2 == 0)a.push(queue[(theFront + i + 1) % queueLength]);elseb.push(queue[(theFront + i + 1) % queueLength]);}
}
#endif

_18arrayQueue.cpp

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			测试_18arrayQueue.h头文件中的所有函数
*/
#include <iostream>
#include <time.h>
#include "_18arrayQueue.h"
using namespace std;/*测试函数*/
void arrayQueueTest()
{cout << endl << "*********************************arrayQueueTest()函数开始*************************************" << endl;arrayQueue<int> a;//测试输入和输出cout << endl << "测试友元函数*******************************************" << endl;cout << "输入输出************************" << endl;cin >> a;cout << "arrayQueue a is:" << a;cout << endl << "测试成员函数*******************************************" << endl;cout << "empty()*************************" << endl;cout << "a.empty() = " << a.empty() << endl;cout << "size()**************************" << endl;cout << "a.size() = " << a.size() << endl;cout << "capacity()**********************" << endl;cout << "a.capacity() = " << a.capacity() << endl;cout << "push()**************************" << endl;cout << "arrayQueue a is:" << a;a.push(99);a.push(22);cout << "arrayQueue a is:" << a;cout << "front()*************************" << endl;cout << "a.front() = " << a.front() << endl;cout << "back()**************************" << endl;cout << "a.back() = " << a.back() << endl;cout << "pop()***************************" << endl;cout << "before pop arrayQueue a is:" << a;a.pop();a.pop();cout << "after pop arrayQueue a is:" << a;cout << "resizeQueue()*******************" << endl;cout << "before resizeQueue a.capacity() = " << a.capacity()<<endl;a.resizeQueue(4);cout << "after resizeQueue a.capacity() = " << a.capacity() << endl;cout << "arrayQueue a is:" << a;cout << "a.front() = " << a.front() << endl;cout << "a.back() = " << a.back() << endl;a.push(88);cout << "after resizeQueue a.capacity() = " << a.capacity() << endl;cout << "meld()**************************" << endl;arrayQueue<int> b;cin >> b;cout << "arrayQueue a is:" << a;cout << "arrayQueue b is:" << b;arrayQueue<int> c;c.meld(a, b);cout << "arrayQueue c is:" << c;cout << "split()*************************" << endl;arrayQueue<int> d;arrayQueue<int> e;c.split(d, e);cout << "arrayQueue c is:" << c;cout << "arrayQueue d is:" << d;	cout << "arrayQueue e is:" << e;cout << endl << "测试成员函数性能***************************************" << endl;cout << "push()**************************" << endl;arrayQueue<int> f;double clocksPerMillis = double(CLOCKS_PER_SEC) / 1000;clock_t startTime = clock();for (int i = 0; i < 100000000; i++)f.push(i);double pushTime = (clock() - startTime) / clocksPerMillis;cout << 10000 << " push took " << pushTime << " ms" << endl;cout << "*********************************arrayQueueTest()函数结束*************************************" << endl;}

main.cpp

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			main()函数,控制运行所有的测试函数
*/
#include <iostream>
#include "_18arrayQueue.h"int main()
{arrayQueueTest();return 0;
}

_1myExceptions.h

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			综合各种异常
*/
#pragma once
#ifndef _MYEXCEPTIONS_H_
#define _MYEXCEPTIONS_H_
#include <string>
#include<iostream>using namespace std;// illegal parameter value
class illegalParameterValue 
{public:illegalParameterValue(string theMessage = "Illegal parameter value"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// illegal input data
class illegalInputData 
{public:illegalInputData(string theMessage = "Illegal data input"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// illegal index
class illegalIndex 
{public:illegalIndex(string theMessage = "Illegal index"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// matrix index out of bounds
class matrixIndexOutOfBounds 
{public:matrixIndexOutOfBounds(string theMessage = "Matrix index out of bounds"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// matrix size mismatch
class matrixSizeMismatch 
{public:matrixSizeMismatch(string theMessage = "The size of the two matrics doesn't match"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// stack is empty
class stackEmpty
{public:stackEmpty(string theMessage = "Invalid operation on empty stack"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// queue is empty
class queueEmpty
{public:queueEmpty(string theMessage = "Invalid operation on empty queue"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// hash table is full
class hashTableFull
{public:hashTableFull(string theMessage = "The hash table is full"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// edge weight undefined
class undefinedEdgeWeight
{public:undefinedEdgeWeight(string theMessage = "No edge weights defined"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};// method undefined
class undefinedMethod
{public:undefinedMethod(string theMessage = "This method is undefined"){message = theMessage;}void outputMessage() {cout << message << endl;}private:string message;
};
#endif

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/110541.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue 鼠标划入划出多传一个参数

// item可以传递弹窗显示数据&#xff0c; $event相关参数可以用来做弹窗定位用 mouseover"handleMouseOver($event, item)" mouseleave"handleMouseLeave($event, item)"举个栗子&#xff1a; 做一个hover提示弹窗组件(用的vue3框架 less插件) 可以将组件…

rabbitmq发送json格式 utf8编码数据

参考文章&#xff1a;Spring-Cloud RabbitMQ 用法 - 发送json对象 - 简书 生产者&#xff1a; 消费者&#xff1a;

哪家堡垒机支持国密算法?有哪些功能?

国密算法即国家密码局认定的国产密码算法&#xff0c;即商用密码。最近看到有不少小伙伴在问&#xff0c;哪家堡垒机支持国密算法&#xff1f;有哪些功能&#xff1f; 哪家堡垒机支持国密算法&#xff1f; 行云堡垒支持SM2、SM3、SM4等国产密码算法&#xff0c;同时支持国密…

2023年下半年NPDP考试今天开始报名!

2023年第二次NPDP考试将于2023年12月2日&#xff08;周六&#xff09;举行&#xff0c;考试报名相关事项安排如下&#xff1a; 一、考试时间&#xff1a; 12月2日09:00-12:30。 二、报名时间&#xff1a; 10月18日08:00-11月10日23:59。 三、缴费及退考截止时间&#xff1…

C++使用openssl对AES-256-ECB PKCS7 加解密

/** AES-256-ECB PKCS7 加密 函数* input:经过PKCS7填充后的明文数据* outhex:加密后的命名数据16进制数,可以使用base64_encode转换为base64格式字符串密文* key:密钥* len:经过PKCS7填充后的明文数据长度*/ void AesEcb256Pkcs7Encrypt(u8 *input, u8 *outhex, u8 *key, int …

基于Java的旅游网站系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09; 代码参考数据库参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&am…

2023年中国GPS导航设备产量、销量及市场规模分析[图]

GPS导航设备行业是指生产和销售用于导航、定位和监控目的的GPS设备的行业&#xff0c;可以用于汽车、船只、飞机、人员和其他物体的定位和导航&#xff0c;以及用于地理信息系统&#xff08;GIS&#xff09;、测绘、海洋抢险、森林监测、地质勘探、气象预报、交通管理、物流跟踪…

Java项目-网页聊天程序

目录 项目介绍 项目功能简介 项目创建 用户管理模块 1.数据库设计及代码实现 2.前后端交互接口的设计 3.服务器代码开发 好友管理模块 数据库设计 好友表设计的两个重要问题 设计前后端交互接口 服务器代码 会话管理模块 会话的数据库设计 获取会话信息 约定前后…

Linux杀掉僵尸进程方法

说明&#xff1a; 一般在使用pytorch训练网络模型时&#xff0c;可能会不正确的终端训练&#xff0c;导致进程僵尸&#xff0c;GPU依然被占用的情况。 解决办法&#xff1a; 查看进程的ID及其父进程ID指令&#xff1a; ps -ef | grep defunct | more假设输出如下&#xff1a…

滴滴弹性云基于 K8S 的调度实践

上篇文章详细介绍了弹性云混部的落地历程&#xff0c;弹性云是滴滴内部提供给网约车等核心服务的容器平台&#xff0c;其基于 k8s 实现了对海量 node 的管理和 pod 的调度。本文重点介绍弹性云的调度能力&#xff0c;分为以下部分&#xff1a; 调度链路图&#xff1a;介绍当前弹…

服务器数据恢复-RAID信息破坏导致服务器操作系统无法启动的数据恢复案例

服务器数据恢复环境&#xff1a; 一台服务器&#xff0c;8块硬盘组建了一组raid5磁盘阵列&#xff0c;服务器安装的是windows server操作系统&#xff0c;上层部署ORACLE数据库。 服务器故障&#xff1a; 在服务器运行过程中&#xff0c;2块硬盘报警&#xff0c;服务器操作系统…

【75. 颜色分类】

目录 一、题目描述二、算法思想三、代码实现 一、题目描述 二、算法思想 三、代码实现 class Solution { public:void sortColors(vector<int>& nums) {int nnums.size();for(int left-1,rightn,i0;i<right;){if(nums[i]0)swap(nums[i],nums[left]);else if(nums…

Android SurfaceControlViewHost介绍及使用

概要介绍 SurfaceControlViewHost是一个工具类&#xff0c; 用于帮助在其他进程中显示本进程的view。 SurfaceControlViewHost 为绘制进程持有&#xff0c;其中的SurfacePackage 交给另外的显示进程&#xff0c;在显示进程中的SurfaceView中通过SurfaceView.setChildSurface…

华测监测预警系统 2.2---任意文件读取漏洞

目录 1. 资产搜集 2. 漏洞复现 3. 实战总结 1. 资产搜集 直接上fofa 和 hunter 个人推荐hunter可以看到icp备案公司直接提交盒子就行了 FOFA语法 app”华测监测预警系统2.2” Hunter语法 web.body”华测监测预警系统2.2” 2. 漏洞复现 这里手动复现的&#xff0c;目录是/…

Godot2D角色导航-自动寻路教程(Godot获取导航路径)

文章目录 开始准备获取路径全局点坐标 开始准备 首先创建一个导航场景&#xff0c;具体内容参考下列文章&#xff1a; Godot实现角色随鼠标移动 然后我们需要设置它的导航目标位置&#xff0c;具体关于位置的讲解在下面这个文章&#xff1a; Godot设置导航代理的目标位置 获取…

【自动化测试】基于Selenium + Python的web自动化框架

一、什么是Selenium&#xff1f; Selenium是一个基于浏览器的自动化工具&#xff0c;她提供了一种跨平台、跨浏览器的端到端的web自动化解决方案。Selenium主要包括三部分&#xff1a;Selenium IDE、Selenium WebDriver 和Selenium Grid&#xff1a;  1、Selenium IDE&…

吐血整理,服务端性能测试中间件-项目集成redis实战,一篇打通...

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 Linux下Redis安装…

Jmeter性能测试:高并发分布式性能测试

一、为什么要进行分布式性能测试 当进行高并发性能测试的时候&#xff0c;受限于Jmeter工具本身和电脑硬件的原因&#xff0c;无法满足我们对大并发性能测试的要求。 基于这种场景下&#xff0c;我们就需要采用分布式的方式来实现我们高并发的性能测试要求。 二、分布式性能…

深入探求:中国精益生产与管理实践的崭新视角

经过多方位的了解&#xff0c;比之制造行业上的精益管理上的表现情况&#xff0c;认为目前国内的精益生产精益管理实践仍处于自我认知的水平。目前很多的企业前进的步伐还是主要依据市场经济发展所衍生出来的较为先进的工具运用&#xff0c;其战略性的管理处于局部优化再而达到…

线上软件的故障排查方法

线上软件的故障排查是确保软件系统正常运行和快速解决问题的重要任务。以下是一些通用的线上软件故障排查方法&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 监控系统&#xff1a; 使用监控工具来追…