队列的链表实现 题目(难度1/10)
C++数据结构与算法 目录
队列介绍
队列这种容器,就像大家排队上公交车一样。
第一个来到的人排在最前面;
最后来的排在最后面;
第一个先上车(离开队列);
队列的接口
队列是有如下接口的容器:
class Queue
{
public:const int& first(void) const;//队列的对头(第一个元素),最先进队列的元素inline bool empty(void) const;//判断队列里是否没有元素(空队列)inline size_t size(void) const;//返回队列元素数量void enqueue(const int& item);//将元素item进队列void dequeue(void);//将对头(队列的第一个元素)出队列(从队列中删除)void clear(void);//清空队列中的所有元素
实现思路
由于双链表的接口覆盖了队列的接口,所以可以使用双链表来实现一个队列。
这样就可以在双链表外面封装出一个队列的类。
在实现的时候,队列的接口只需要调用双链表的接口即可。
题目如下
#include <iostream>
#include <iomanip>
#include <list>
using namespace std;//------下面的代码是用来测试你的代码有没有问题的辅助代码,你无需关注------
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
struct Record { Record(void* ptr1, size_t count1, const char* location1, int line1, bool is) :ptr(ptr1), count(count1), line(line1), is_array(is) { int i = 0; while ((location[i] = location1[i]) && i < 100) { ++i; } }void* ptr; size_t count; char location[100] = { 0 }; int line; bool is_array = false; bool not_use_right_delete = false; }; bool operator==(const Record& lhs, const Record& rhs) { return lhs.ptr == rhs.ptr; }std::vector<Record> myAllocStatistic; void* newFunctionImpl(std::size_t sz, char const* file, int line, bool is) { void* ptr = std::malloc(sz); myAllocStatistic.push_back({ ptr,sz, file, line , is }); return ptr; }void* operator new(std::size_t sz, char const* file, int line) { return newFunctionImpl(sz, file, line, false); }void* operator new [](std::size_t sz, char const* file, int line)
{return newFunctionImpl(sz, file, line, true);
}void operator delete(void* ptr) noexcept { Record item{ ptr, 0, "", 0, false }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); } }void operator delete[](void* ptr) noexcept { Record item{ ptr, 0, "", 0, true }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (!itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); } }
#define new new(__FILE__, __LINE__)
struct MyStruct { void ReportMemoryLeak() { std::cout << "Memory leak report: " << std::endl; bool leak = false; for (auto& i : myAllocStatistic) { if (i.count != 0) { leak = true; std::cout << "leak count " << i.count << " Byte" << ", file " << i.location << ", line " << i.line; if (i.not_use_right_delete) { cout << ", not use right delete. "; } cout << std::endl; } }if (!leak) { cout << "No memory leak." << endl; } }~MyStruct() { ReportMemoryLeak(); } }; static MyStruct my; void check_do(bool b, int line = __LINE__) { if (b) { cout << "line:" << line << " Pass" << endl; } else { cout << "line:" << line << " Ohh! not passed!!!!!!!!!!!!!!!!!!!!!!!!!!!" << " " << endl; exit(0); } }
#define check(msg) check_do(msg, __LINE__);
//------上面的代码是用来测试你的代码有没有问题的辅助代码,你无需关注------class Queue
{
public:inline const int& first(void) const;inline bool empty(void) const;inline size_t size(void) const;void enqueue(const int& _item);void dequeue(void);void clear(void);
private://使用双链表实现队列std::list<int> m_queue;
};inline const int& Queue::first(void) const
{int a = 0;return a;
}bool Queue::empty(void) const
{return false;
}void Queue::clear(void)
{
}size_t Queue::size(void) const
{return -1;
}void Queue::enqueue(const int& _item)
{
}void Queue::dequeue(void)
{
}int main(int argc, char** argv)
{//test clear{Queue s;check(s.size() == 0);check(s.empty());s.enqueue(1);check(s.size() == 1);check(s.empty() == false);s.clear();check(s.size() == 0);check(s.empty());}//test first{Queue q;check(q.size() == 0);q.enqueue(1);check(q.size() == 1);auto q2 = q;check(q2.size() == 1);q = q2;q.enqueue(2);auto first = q2.first();check(first == 1);check(q.size() == 2);check(q.first() == 1);q.clear();check(q.size() == 0 && q.empty());}//test enqueue dequeue{Queue q;for (size_t i = 0; i < 10; i++){q.enqueue(i);}int i = 0;while (!q.empty()){check(q.first() == i++)q.dequeue();}check(q.size() == 0 && q.empty());}
}
预期输出如下
line:71 Pass
line:72 Pass
line:74 Pass
line:75 Pass
line:77 Pass
line:78 Pass
line:83 Pass
line:85 Pass
line:87 Pass
line:91 Pass
line:92 Pass
line:93 Pass
line:95 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:107 Pass
line:110 Pass
Memory leak report:
No memory leak.
相关文章:
队列的链表实现 题目(难度1/10)
C数据结构与算法 目录 队列介绍 队列这种容器,就像大家排队上公交车一样。 第一个来到的人排在最前面; 最后来的排在最后面; 第一个先上车(离开队列); 队列的接口 队列是有如下接口的容器࿱…...
SpringMVC常用的三种获取请求参数的方式
在Spring MVC中,可以使用多种方式来获取请求参数。下面我将介绍常用的几种方式,并提供相关的示例代码。 1. 使用RequestParam注解获取请求参数 RequestParam注解用于从请求中获取指定名称的参数值,并将其绑定到方法参数上。如果请求中没有找…...

2023开学礼新疆理工学院图书馆藏八一新书《乡村振兴战略下传统村落文化旅游设计》许少辉新财经理工
2023开学礼新疆理工学院图书馆藏八一新书《乡村振兴战略下传统村落文化旅游设计》许少辉新财经理工...
数据结构----结构--线性结构--字符串
数据结构----结构–线性结构–字符串 一.字符串的定义方式 第一种: char* str1"Hello"第二种: char str2[]"Hello";区别 1.所在区域不同 //str1在常量区//str2在这里的写法是在栈区2.元素是否可改 //str1中的元素不可改//st…...

数据工厂-生成接口通用用例
章节目录: 一、背景介绍二、前置准备三、设计思路四、代码具体实现五、执行效果六、其他说明七、结束语 一、背景介绍 有哪些用例是可以通用且固定的? 针对之前提到的接口用例设计思路,拆分为三个切入点: 举个例子: {…...
N 字形变换
N 字形变换 题目: 将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:P A H N A P L S I I G Y I R 之后,你的输…...

STM32+RTThread配置以太网无法ping通,无法获取动态ip的问题
记录一个非常蠢的问题,今天在移植rtthread的以太网驱动的时候出现无法获取动态ip的问题,问题如下: 设置为动态ip时不管是连接路由器还是电脑主机都无法ping通,也无法获取dns地址。 设置为静态ip时无法ping通主机。 使用wireshark…...

python编写MQTT订阅程序
Download | Eclipse Mosquitto 1、下载: https://mosquitto.org/files/binary/win64/mosquitto-2.0.17-install-windows-x64.exe 2、安装: 3、conf配置 1)使用notepad打开“C:\Program Files\mosquitto\mosquitto.conf”另存为c:\myapp\msquitto\mo…...
mysql 中 cast 函数用法
在 MySQL 中,CAST() 函数用于将一个表达式转换为指定的数据类型。它可以用于多种场景,例如将字符串转换为数字,或者将日期时间转换为特定格式。 以下是 CAST() 函数的基本语法: CAST(expression AS datatype) 其中,…...

MongoDB 的简介
MongoDB 趋势 对于 MongoDB 的认识 Q&A QA什么是 MongoDB? 一个以 JSON 为数据模型的文档数据库一个以 JSON 为数据模型的文档数据库文档来自于“JSON Document”,并非我们一般理解的 PDF,WORD谁开发 MongDB? 上市公司 MongoD…...

是否在业务中使用大语言模型?
ChatGPT取得了巨大的成功,在短短一个月内就获得了1亿用户,并激发了企业和专业人士对如何在他们的组织中利用这一工具的兴趣和好奇心。 但LLM究竟是什么,它们如何使你的企业受益?它只是一种炒作,还是会长期存在? 在这篇文章中我…...

37. 交换字符(第三期模拟笔试)
题目: 给定一个01串(仅由字符0和字符1构成的字符串)。每次操作可以交换两个相邻的字符。 例如:对于字符串"001110"来说, 可以交换第二个字符0和第三个字符1,交换之后的字符串变成了"0101…...

git 查看当前分支最近一次提交的commit SHA
获取当前分支最近一次commit SHA (长度为40个16进制数字的字符)命令如下: git rev-parse HEAD 获取简写(短) commit SHA git rev-parse --short HEAD...

LuatOS 开发指南
NDK 开发 官方教程 官方例程 API 下载软件 下载官方NDK例程压缩包到本地,并解压。可以看到目录如下: doc: 文档教程 env: 编译环境 example: NDK示例 platform: 需要编译的平台(air72x/air8xx) tools: 其他辅助软件 VSCode 使…...

maven推包The environment variable JAVA_HOME is not correctly set
解决办法: 打开idea查看jdk安装位置 1.在/etc下面创建(如果存在就是更新)launchd.conf。里面添加一行: setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_351.jdk/Contents/Home #JAVA_HOME后面是我的java安装路径…...

Python VScode 配置
在上一章节中我们已经安装了 Python 的环境,本章节我们将介绍 Python VScode 的配置。 准备工作: 安装 VS Code安装 VS Code Python 扩展安装 Python 3 安装 VS Code VSCode(全称:Visual Studio Code)是一款由微软…...

【vue2第九章】组件化开发和根组件以及style上的scoped作用
组件化开发和根组件 什么是组件化开发? 一个页面可以拆分为多个组件,每个组件有自己的样式,结构,行为,组件化开发的好处就是,便于维护,利于重复利用,提升开发的效率。 便于维护&…...

从零开始的Hadoop学习(五)| HDFS概述、shell操作、API操作
1. HDFS 概述 1.1 HDFS 产出背景及定义 1)HDFS 产生背景 随着数据量越来越大,在一个操作系统存不下所有的数据,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,迫切 需要一种系统来管理多台机器上的…...
【spark】序列化和反序列化,transient关键字的使用
序列化 Spark是基于JVM运行的进行,其序列化必然遵守Java的序列化规则。 序列化就是指将一个对象转化为二进制的byte流(注意,不是bit流),然后以文件的方式进行保存或通过网络传输,等待被反序列化读取出来。…...
2.4 Vector<T> 动态数组(随机访问迭代器)
C自学精简教程 目录(必读) 该 Vector 版本特点 这里的版本主要是使用模板实现、支持随机访问迭代器,支持std::sort等所有STL算法。(本文对随机迭代器的支持参考了 复旦大学 大一公共基础课C语言的一次作业) 随机访问迭代器的实现主要是继承std::iterator<std:…...

Linux应用开发之网络套接字编程(实例篇)
服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

【配置 YOLOX 用于按目录分类的图片数据集】
现在的图标点选越来越多,如何一步解决,采用 YOLOX 目标检测模式则可以轻松解决 要在 YOLOX 中使用按目录分类的图片数据集(每个目录代表一个类别,目录下是该类别的所有图片),你需要进行以下配置步骤&#x…...

12.找到字符串中所有字母异位词
🧠 题目解析 题目描述: 给定两个字符串 s 和 p,找出 s 中所有 p 的字母异位词的起始索引。 返回的答案以数组形式表示。 字母异位词定义: 若两个字符串包含的字符种类和出现次数完全相同,顺序无所谓,则互为…...
Rapidio门铃消息FIFO溢出机制
关于RapidIO门铃消息FIFO的溢出机制及其与中断抖动的关系,以下是深入解析: 门铃FIFO溢出的本质 在RapidIO系统中,门铃消息FIFO是硬件控制器内部的缓冲区,用于临时存储接收到的门铃消息(Doorbell Message)。…...

ABAP设计模式之---“简单设计原则(Simple Design)”
“Simple Design”(简单设计)是软件开发中的一个重要理念,倡导以最简单的方式实现软件功能,以确保代码清晰易懂、易维护,并在项目需求变化时能够快速适应。 其核心目标是避免复杂和过度设计,遵循“让事情保…...
Java 二维码
Java 二维码 **技术:**谷歌 ZXing 实现 首先添加依赖 <!-- 二维码依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.5.1</version></dependency><de…...

10-Oracle 23 ai Vector Search 概述和参数
一、Oracle AI Vector Search 概述 企业和个人都在尝试各种AI,使用客户端或是内部自己搭建集成大模型的终端,加速与大型语言模型(LLM)的结合,同时使用检索增强生成(Retrieval Augmented Generation &#…...

Linux 中如何提取压缩文件 ?
Linux 是一种流行的开源操作系统,它提供了许多工具来管理、压缩和解压缩文件。压缩文件有助于节省存储空间,使数据传输更快。本指南将向您展示如何在 Linux 中提取不同类型的压缩文件。 1. Unpacking ZIP Files ZIP 文件是非常常见的,要在 …...
jmeter聚合报告中参数详解
sample、average、min、max、90%line、95%line,99%line、Error错误率、吞吐量Thoughput、KB/sec每秒传输的数据量 sample(样本数) 表示测试中发送的请求数量,即测试执行了多少次请求。 单位,以个或者次数表示。 示例:…...
解决:Android studio 编译后报错\app\src\main\cpp\CMakeLists.txt‘ to exist
现象: android studio报错: [CXX1409] D:\GitLab\xxxxx\app.cxx\Debug\3f3w4y1i\arm64-v8a\android_gradle_build.json : expected buildFiles file ‘D:\GitLab\xxxxx\app\src\main\cpp\CMakeLists.txt’ to exist 解决: 不要动CMakeLists.…...