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

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

定义

队列的定义

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

队列的抽象数据类型

在这里插入图片描述

链表队列代码

数组双端队列实现

_19deque.h

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

_20arrayDeque.h

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			数组存储的无序的双端队列类的头文件
*/
#pragma once
#ifndef _ARRAYDEQUE_H_
#define _ARRAYDEQUE_H_
#include<sstream>
#include<iostream>
#include "_1myExceptions.h"
#include "_19deque.h"
/*测试函数*/
void arrayDequeTest();
template<class T>
class arrayDeque : public deque<T>
{
public:/*成员函数*//*构造函数*/arrayDeque(int initialCapacity = 10);/*析构函数*/~arrayDeque() {delete[]  deque;}bool empty() const { return theFront == theBack; };//返回true,当且仅当队列为空int size() const { return (dequeLength - theFront + theBack)%dequeLength; };//返回队列中元素个数int capacity() const {return dequeLength - 1;}void clear() { theFront = theBack = 0; }T& front();//返回头元素的引用T& back();//返回尾元素的引用void pop_front();//删除首元素void pop_back();//删除尾元素void push_front(const T& theElement);//把元素theELment加入队首void push_back(const T& theElement);//把元素theELment加入队尾/*调整队列容量大小*/void resizeDeque(int newLength);void meld(arrayDeque<T>& a, arrayDeque<T>& b);void split(arrayDeque<T>& a, arrayDeque<T>& b);/*重载操作符*//*重载[]操作符*/T operator[](int i) { return deque[(theFront + i + 1) % dequeLength]; }/*友元函数*/friend istream& operator>> <T>(istream& in, arrayDeque<T>& m);//输出但是不pop()元素friend ostream& operator<< <T>(ostream& out, arrayDeque<T>& m);
private:int theFront;       // 第一个元素的前一个位置int theBack;        // 最后一个元素的位置int dequeLength;    // 队列的容量,实质上比队列容量(不包含queueFront指向的那一个位置)大1T* deque;
};/*友元函数*/
/*>>操作符*/
template<class T>
istream& operator>>(istream& in, arrayDeque<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_back(cinElement);}return in;
}
/*<<操作符*/
template<class T>
ostream& operator<<(ostream& out, arrayDeque<T>& m)
{int size = m.size();for (int i = 0; i < size; i++)out << m.deque[(m.theFront + i + 1) % m.dequeLength] << "  ";out << endl;return out;
}/*构造函数*/
template<class T>
arrayDeque<T>::arrayDeque(int initialCapacity)
{if (initialCapacity < 1){ostringstream s("");s << "Initial capacity = " << initialCapacity << "Must be > 0";throw illegalParameterValue(s.str());}deque = new T[initialCapacity + 1];dequeLength = initialCapacity + 1;theFront = theBack = 0;
}
/*返回头元素的引用*/
template<class T>
T& arrayDeque<T>::front()
{if(theFront == theBack)throw queueEmpty();return deque[(theFront + 1) % dequeLength];
}
/*返回尾元素的引用*/
template<class T>
T& arrayDeque<T>::back()
{if (theFront == theBack)throw queueEmpty();return deque[theBack];
}
/*删除首元素*/
template<class T>
void arrayDeque<T>::pop_front()
{/*检查是否为空,为空就抛出异常*/if (theFront == theBack)throw queueEmpty();/*不为空就删除首元素*/theFront = (theFront + 1) % dequeLength;deque[theFront].~T();
}
/*删除尾元素*/
template<class T>
void arrayDeque<T>::pop_back()
{/*检查是否为空,为空就抛出异常*/if (theFront == theBack)throw queueEmpty();/*不为空就删除尾元素*/deque[theBack].~T();if (theBack == 0)theBack = dequeLength - 1;elsetheBack--;
}
/*把元素theELment加入队首*/
template<class T>
void arrayDeque<T>::push_front(const T& theElement)
{/*判断队列是否满了,如果满了,就调整容量为原来的两倍*/if ((theFront + 1) % dequeLength == theBack)resizeDeque(2 * (dequeLength-1));deque[theFront] = theElement;if (theFront == 0)theFront = dequeLength - 1;elsetheFront = theFront - 1;
}
/*把元素theELment加入队尾*/
template<class T>
void arrayDeque<T>::push_back(const T& theElement)
{/*判断队列是否满了,如果满了,就调整容量为原来的两倍*/if ((theBack + 1) % dequeLength == theFront)resizeDeque(2 * (dequeLength - 1));theBack = (theBack + 1) % dequeLength;deque[theBack] = theElement;
}/*调整队列容量大小*/
template<class T>
void arrayDeque<T>::resizeDeque(int newLength)
{T* temp = new T[newLength + 1];int size = min((*this).size(), newLength);for (int i = 0; i < size; i++)temp[i] = deque[(theFront + i + 1) % dequeLength];dequeLength = newLength + 1;theFront = newLength;theBack = size - 1;delete[] deque;deque = temp;
}/*
创建一个新的队列,该表包含了a和b中的所有元素,其中a和b的元素轮流出现,表中的首
元素为a中的第一个元素。在轮流排列元素时,如果某个队列的元素用完了,则把另一个队列的其
余元素依次添加在新队列的后部。代码的复杂性应与两个输入队列的长度呈线性比例关系。
归并后的线性队列是调用对象*this
*/
template <class T>
void arrayDeque<T>::meld(arrayDeque<T>& a, arrayDeque<T>& b)
{(*this).clear();int i = 0;while (i < a.size() && i < b.size()){push_back(a[i]);push_back(b[i]);i++;}while (i < a.size()){push_back(a[i]);i++;}while (i < b.size()){push_back(b[i]);i++;}
}/*生成两个线性队列a和b,a包含*this中索引为奇数的元素,b包含其余的元素*/
template<class T>
void arrayDeque<T>::split(arrayDeque<T>& a, arrayDeque<T>& b)
{a.clear();b.clear();int size = (*this).size();for (int i = 0; i < size; i++){if (i % 2 == 0)a.push_back(deque[(theFront + i + 1) % dequeLength]);elseb.push_back(deque[(theFront + i + 1) % dequeLength]);}
}#endif

_20arrayDeque.cpp

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			测试_20arrayDeque.h头文件中的所有函数
*/
#include <iostream>
#include "_20arrayDeque.h"
using namespace std;
/*测试函数*/
void arrayDequeTest()
{cout << endl << "*********************************arrayDequeTest()函数开始*************************************" << endl;arrayDeque<int> a;//测试输入和输出cout << endl << "测试友元函数*******************************************" << endl;cout << "输入输出************************" << endl;cin >> a;cout << "arrayDeque 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_front()********************" << endl;cout << "before push_front() arrayDeque a is:" << a;a.push_front(99);a.push_front(22);cout << "after push_front() arrayDeque a is:" << a;cout << "push_back()*********************" << endl;cout << "before push_back() arrayDeque a is:" << a;a.push_back(99);a.push_back(22);cout << "after push_back() arrayDeque a is:" << a;cout << "front()*************************" << endl;cout << "a.front() = " << a.front() << endl;cout << "back()**************************" << endl;cout << "a.back() = " << a.back() << endl;cout << "pop_front()*********************" << endl;cout << "before pop_front arrayDeque a is:" << a;a.pop_front();a.pop_front();cout << "after pop_front arrayDeque a is:" << a;cout << "pop_back()**********************" << endl;cout << "before pop_back arrayDeque a is:" << a;a.pop_back();a.pop_back();cout << "after pop_back arrayDeque a is:" << a;cout << "resizeDeque()*******************" << endl;cout << "before resizeDeque a.capacity() = " << a.capacity() << endl;a.resizeDeque(4);cout << "after resizeDeque a.capacity() = " << a.capacity() << endl;cout << "resizeDeque a is:" << a;cout << "a.front() = " << a.front() << endl;cout << "a.back() = " << a.back() << endl;a.push_back(88);cout << "after resizeDeque a.capacity() = " << a.capacity() << endl;cout << "meld()**************************" << endl;arrayDeque<int> b;cin >> b;cout << "arrayDeque a is:" << a;cout << "arrayDeque b is:" << b;arrayDeque<int> c;c.meld(a, b);cout << "arrayDeque c is:" << c;cout << "split()*************************" << endl;arrayDeque<int> d;arrayDeque<int> e;c.split(d, e);cout << "arrayDeque c is:" << c;cout << "arrayDeque d is:" << d;cout << "arrayDeque e is:" << e;cout << "*********************************arrayDequeTest()函数结束*************************************" << endl;}

_1main.cpp

/*
Project name :			allAlgorithmsTest
Last modified Date:		2022年8月13日17点38分
Last Version:			V1.0
Descriptions:			main()函数,控制运行所有的测试函数
*/
#include <iostream>
#include "_20arrayDeque.h"int main()
{arrayDequeTest();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/111618.shtml

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

相关文章

【vue】el-carousel实现视频自动播放与自动切换到下一个视频功能:

文章目录 一、原因:二、实现代码:三、遇到的问题&#xff1a;【1】问题&#xff1a;el-carousel页面的视频不更新【2】问题&#xff1a;多按几次左按钮&#xff0c;其中跳过没有播放的视频没有销毁&#xff0c;造成再次自动播放时会跳页 一、原因: 由于后端无法实现将多条视频拼…

手机爬虫用Scrapy详细教程:构建高效的网络爬虫

如果你正在进行手机爬虫的工作&#xff0c;并且希望通过一个高效而灵活的框架来进行数据抓取&#xff0c;那么Scrapy将会是你的理想选择。Scrapy是一个强大的Python框架&#xff0c;专门用于构建网络爬虫。今天&#xff0c;我将与大家分享一份关于使用Scrapy进行手机爬虫的详细…

照片后期编辑工具Lightroom Classic 2024 mac中文新增功能

Lightroom Classic 2024&#xff08;lrC2024&#xff09;是专为摄影爱好者和专业摄影师设计的软件&#xff0c;它提供了全面的照片编辑工具&#xff0c;可以精准调整照片的色彩、对比度和曝光等参数&#xff0c;以便定制后期处理效果。 在lrC2024中&#xff0c;用户体验得到了提…

文件的逻辑结构(顺序文件,索引文件)

所谓的“逻辑结构”&#xff0c;就是指在用户看来&#xff0c;文件内部的数据应该是如何组织起来的。 而“物理结构”指的是在操作系统看来&#xff0c;文件的数据是如何存放在外存中的。 1.无结构文件 无结构文件:文件内部的数据就是一系列二进制流或字符流组成。无明显的逻…

SortedSet 和 List 异同点

SortedSet 在 Java 的整个集合体系中&#xff0c;集合可以分成两个体系&#xff0c;一个是 Collection 存储单个对象的集合&#xff0c;另一个是 k-v 结构的 Map 集合。 SortedSet 是 Collection 体系下 Set 接口下的派生类&#xff0c;而 Set 集合的特征是不包含重 复的元素的…

(论文翻译)UFO: Unified Feature Optimization——UFO:统一特性优化

作者&#xff1a; Teng Xi 论文总结&#xff1a;总结 Code: https://github.com/PaddlePaddle/VIMER/tree/main/UFO 摘要&#xff1a; 本文提出了一种新的统一特征优化(Unified Feature Optimization, UFO)范式&#xff0c;用于在现实世界和大规模场景下训练和部署深度模型…

asp.net特色商品购物网站系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio

一、源码特点 asp.net特色商品购物网站系统 是一套完善的web设计管理系统&#xff0c;系统采用mvc模式&#xff08;BLLDALENTITY&#xff09;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为 vs2010&#xff0c;数据库为sqlserver2008&a…

安装Apache2.4

二、安装配置Apache&#xff1a; 中文官网&#xff1a;Apache 中文网 官网 (p2hp.com) 我下的是图中那个版本&#xff0c;最新的64位 下载下后解压缩。如解压到D:\tool\Apache24 PS&#xff1a;特别要注意使用的场景和64位还是32位版本 2、修改Apcahe配置文件 具体步骤: 打…

Required MultipartFile parameter ‘file‘ is not present

出现这个原因我们首先想到的是加一个RequestParam("file")&#xff0c;但是还有可能的原因是因为我们的名字有错误 <span class"input-group-addon must">模板上传 </span> <input id"uploadFileUpdate" name"importFileU…

内衣专用洗衣机怎么样?选购内衣裤洗衣机的方法

有的小伙伴在问内衣洗衣机有没有必要入手&#xff0c;答案是有必要的&#xff0c;贴身衣物一定要和普通衣服分开来洗&#xff0c;而且用手来清洗衣物真的很耗时间而且还清洗不干净&#xff0c;有了内衣洗衣机&#xff0c;我们不仅可以解放双手&#xff0c;在清洗过程中还能更加…

实现日期间的运算——C++

&#x1f636;‍&#x1f32b;️Take your time ! &#x1f636;‍&#x1f32b;️ &#x1f4a5;个人主页&#xff1a;&#x1f525;&#x1f525;&#x1f525;大魔王&#x1f525;&#x1f525;&#x1f525; &#x1f4a5;代码仓库&#xff1a;&#x1f525;&#x1f525;魔…

SLAM 14 notes

4.23 推导 f ( x ) f(x) f(x)在点a处的泰勒展开 f ( x ) ∑ n 0 ∞ f ( n ) a n ! ( x − a ) n f(x) \sum_{n0}^\infty \frac{f^{(n)}a}{n!}(x-a)^n f(x)∑n0∞​n!f(n)a​(x−a)n l n x lnx lnx的n阶导数 l n ( n ) x ( − 1 ) n − 1 ( n − 1 ) ! x n ln^{(n)}x \fr…

react 中获取多个input输入框中的值的 俩种写法

目录 1. 使用受控组件 2. 使用非受控组件 1. 使用受控组件 这是React中最常见的方法&#xff0c;每个输入框都与React组件的state相关联&#xff0c;并通过onChange事件来更新state。 代码示例&#xff1a; import React, { Component } from react;class MultipleInputExam…

在thonny软件里安装python包 比如 numpy pygame

有一些程序使用了第三方库。如果本地没有安装相应的Python包&#xff0c;这个程序就不能正常运行了。 Python包管理工具提供了对Python 包的查找、下载、安装、卸载的功能。 网络上有很多第三方库&#xff0c;不管要下载哪一个&#xff0c;都需要通过正确的名称来下载安装。 …

websocket+node+vite(vue)实现一个简单的聊天

1.前端逻辑 本项目基于之前搭建的vite环境&#xff1a;https://blog.csdn.net/beekim/article/details/128083106?spm1001.2014.3001.5501 新增一个登录页和聊天室页面 <template><div>登录页</div><div>用户名:<input type"text" pl…

无人机电力巡检:国网安徽实际案例解析

在科技快速发展的今天&#xff0c;传统行业正在经历前所未有的转型。电力巡检&#xff0c;这一看似传统且乏味的任务&#xff0c;却因为无人机技术的介入而焕发新生。今天&#xff0c;让我们深入了解一个具体的案例&#xff0c;探索无人机如何革新电力巡检。 案例背景&#xff…

【Linux】:权限

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家解读一下有关Linux的基础知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门到精通 数…

mac电脑安装雷蛇管理软件,实现调整鼠标dpi,移动速度,灯光等

雷蛇官网只给了win版本驱动 mac版本驱动到这里下载: GitHub - 1kc/razer-macos: Color effects manager for Razer devices for macOS. Supports High Sierra (10.13) to Monterey (12.0). Made by the community, based on openrazer. 安装后会显示开发者不明,请丢弃到垃圾桶.…

ORACLE内存结构

内存体系结构 ​​​​​​​ 目录 内存体系结构 2.1自动内存管理 2.2自动SGA内存管理 2.3手动SGA内存管理 2.3.1数据库缓冲区 2.3.1.1保留池 2.3.1.2回收池 2.3.2共享池 2.3.2.1SQL查询结果和函数查询结果 2.3.2.2库缓存 2.3.2.3数据字典缓存 2.3.3大池 2.3.4 …

Redux详解(二)

1. 认识Redux Toolkit Redux Toolkit 是官方推荐的编写 Redux 逻辑的方法。 通过传统的redux编写逻辑方式&#xff0c;会造成文件分离过多&#xff0c;逻辑抽离过于繁琐&#xff08;具体可看上篇文章 Redux详解一&#xff09;&#xff0c;React官方为解决这一问题&#xff0c;推…