C++11 数据结构5 队列的概念,队列的顺序存储,实现,测试

一,队列的概念

队列是一种特殊的受限制的线性表。  

队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。

队列是一种先进先出的t(First In First Out)的线性表,简称FIFO。允许插入的一端为队尾,允许删除的一端为队头。队列不允许在中间部位进行操作!假设队列是q=(a1,a2,……,an),那么a1就是队头元素,而an是队尾元素。这样我们就可以删除时,总是从a1开始,而插入时,总是在队列最后。这也比较符合我们通常生活中的习惯,排在第一个的优先出列,最后来的当然排在队伍最后。如下图:

二,队列的顺序存储

那么如果用数组来模拟队列,情况说明如下,我们这里采用 尾部插入,头部删除的case进行处理

三,  队列顺序存储的代码实现

#ifndef __005SEQQUEUE_H__#define __005SEQQUEUE_H__//这两个是要给 所有人公开的
typedef void SeqQueueNode;
typedef void SeqQueue;//对外提供的方法//初始化队列,要动态的创建SeqQueue,根据user设定的大小创建
//int stacksize ,表示user 要创建队列的大小
//创建失败返回NULL
SeqQueue* createSeqQueue(int queuesize);//入队列 ,给队列的头部插入一个元素,插入点是在数组的尾部
//参数queue 表示要插入的栈
//参数 seqQueueNode 表示要插入的 节点
//成功 返回 1
//失败 返回<0
int push_SeqQueue(SeqQueue* queue, SeqQueueNode * seqQueueNode);//出队列 将队列的头部的第一个元素删除,删除点是在数组的头部
//参数stack 表示要删除第一个元素的栈
//成功 返回 1
//失败 返回<0
//说明,最开始的时候,让删除栈顶元素,返回int,但是这个设计是不合理的。
//因为假设上层是Teacher,这个Teacher里的元素有 char *,char**,且这两个空间也是malloc的,那么上层需要释放这两个malloc出来的元素。
//这意味着我们需要将 pop_SeqStack中的元素返回上去,上层才有机会释放,底层怎么知道上层搞的是个啥存的?因此一定要给上层,让上层去释放。//出队列 将队列的头部的第一个元素删除,删除点是在数组的头部
//参数seqqueue 表示要删除第一个元素的队列
//成功 返回 删除的第一个元素
//失败 返回 NULL
SeqQueueNode* pop_SeqQueue(SeqQueue* seqqueue);//返回队列元素,将队列头部的第一个元素返回
//失败返回NULL
//成功返回节点
SeqQueueNode* top_SeqQueue(SeqQueue* seqqueue);//返回队列大小
//成功 返回 队列的大小
//失败 返回<0
int size_SeqQueue(SeqQueue* seqqueue);//返回user对队列分配的大小
//成功 返回 队列的大小
//失败 返回<0 
int capacity_SeqQueue(SeqQueue* seqqueue);//判断队列是否为空
//成功 返回1 表示队列为空 
//成功 返回0 表示队列不为空
//失败 返回-1 表示该函数执行的时候有问题
int isEmpty_SeqQueue(SeqQueue* seqqueue);//销毁队列
//成功 返回1 表示成功销毁队列 
//失败 返回-1 表示该函数执行的时候有问题
int Destory_SeqQueue(SeqQueue* seqqueue);#endif

#include "005seqqueue.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"//栈的内部结构,不用写在.h文件中,一会要移动位置
typedef struct SeqQueue {//队列中的数组,存储的是数据的指针,假设我们的数据是Teacher,那么arr[0] 中的数据就应该是&Tea1,//也就是说,这是一个数组,数组的每一个成员里面都存放的是指针。//本质上是一个  void * arr[], 也可以看成是 int * arr[],因为实际上放的就是 Teacher的指针 SeqQueueNode **data;int m_QueueSize;//队列的实际大小int m_QueueCapacity;//队列的总大小,也就是user给定的大小
}TSeqQueue;//对外提供的方法//初始化队列,要动态的创建SeqQueue,根据user设定的大小创建
//int stacksize ,表示user 要创建队列的大小
//创建失败返回NULL
SeqQueue* createSeqQueue(int queuesize) {TSeqQueue* ss = NULL;ss = (TSeqQueue*)malloc(sizeof(TSeqQueue));if (ss == NULL) {printf("createSeqQueue func error\n");return ss;}memset(ss, 0, sizeof(TSeqQueue));ss->m_QueueSize = 0;ss->m_QueueCapacity = queuesize;ss->data = (SeqQueueNode**)malloc(sizeof(SeqQueueNode *) * queuesize);if (ss->data == NULL) {printf("createSeqQueue func error ss->data == NULL \n");return NULL;}memset(ss->data, 0, sizeof(ss->data));return ss;
}//入队列 ,给队列的头部插入一个元素,插入点是在数组的尾部
//参数queue 表示要插入的栈
//参数 seqQueueNode 表示要插入的 节点
//成功 返回 1
//失败 返回<0
int push_SeqQueue(SeqQueue* queue, SeqQueueNode * seqQueueNode) {int ret = 1;if (queue == NULL) {ret = -1;printf("push_SeqQueue func error because stack==NULL ret=-1 and return\n");return ret;}if (seqQueueNode == NULL) {ret = -2;printf("push_SeqQueue func error because seqQueueNode==NULL ret=-2 and return\n");return ret;}TSeqQueue* st = (TSeqQueue*)queue;if (st->m_QueueCapacity == st->m_QueueSize) {ret = -3;printf("push_SeqQueue func error because st->m_StackCapccity == st->m_StackSize ret = -3 and return st->m_StackSize = %d, st->m_StackCapccity = %d\n",st->m_QueueSize,st->m_QueueCapacity);return ret;}//前面检查都完毕了,到这里就真的可以插入了,实行的是尾插法st->data[st->m_QueueSize] = seqQueueNode;st->m_QueueSize++;return ret;
}//出队列 将队列的头部的第一个元素删除,删除点是在数组的头部
//参数stack 表示要删除第一个元素的栈
//成功 返回 1
//失败 返回<0
//说明,最开始的时候,让删除栈顶元素,返回int,但是这个设计是不合理的。
//因为假设上层是Teacher,这个Teacher里的元素有 char *,char**,且这两个空间也是malloc的,那么上层需要释放这两个malloc出来的元素。
//这意味着我们需要将 pop_SeqStack中的元素返回上去,上层才有机会释放,底层怎么知道上层搞的是个啥存的?因此一定要给上层,让上层去释放。//出队列 将队列的头部的第一个元素删除,删除点是在数组的头部
//参数seqqueue 表示要删除第一个元素的队列
//成功 返回 删除的第一个元素
//失败 返回 NULL
SeqQueueNode* pop_SeqQueue(SeqQueue* seqqueue) {SeqQueueNode* ret = NULL;if (seqqueue == NULL) {ret = NULL;printf("pop_SeqQueue func error because stack==NULL and return\n");return ret;}TSeqQueue * tq = (TSeqQueue *)seqqueue;if (tq->m_QueueSize == 0) {ret = NULL;printf("pop_SeqQueue func error because tq->m_QueueSize == 0 and return\n");return ret;}//执行到这里,说明可以删除ret = tq->data[0];//这里有移动的操作。因为删除的是 数组的第一个元素for (int i = 0; i < tq->m_QueueSize; ++i) {tq->data[i] = tq->data[i + 1];}tq->m_QueueSize--;return ret;
}//返回队列元素,将队列头部的第一个元素返回
//失败返回NULL
//成功返回节点
SeqQueueNode* top_SeqQueue(SeqQueue* seqqueue) {SeqQueueNode* ret = NULL;if (seqqueue == NULL) {ret = NULL;printf("top_SeqQueue func error because stack==NULL and return\n");return ret;}TSeqQueue * tq = (TSeqQueue *)seqqueue;if (tq->m_QueueSize == 0) {ret = NULL;printf("top_SeqQueue func error because tq->m_QueueSize == 0 and return\n");return ret;}//执行到这里,说明队列是有元素的ret = tq->data[0];return ret;
}//返回队列大小
//成功 返回 队列的大小
//失败 返回<0
int size_SeqQueue(SeqQueue* seqqueue){int ret = 0;if (seqqueue == NULL) {ret = -1;printf("size_SeqQueue func error because stack==NULL and return\n");return ret;}TSeqQueue * tq = (TSeqQueue *)seqqueue;return tq->m_QueueSize;}//返回user对队列分配的大小
//成功 返回 队列的大小
//失败 返回<0 
int capacity_SeqQueue(SeqQueue* seqqueue) {int ret = 0;if (seqqueue == NULL) {ret = -1;printf("capacity_SeqQueue func error because stack==NULL and return\n");return ret;}TSeqQueue * tq = (TSeqQueue *)seqqueue;return tq->m_QueueCapacity;
}//判断队列是否为空
//成功 返回1 表示队列为空 
//成功 返回0 表示队列不为空
//失败 返回-1 表示该函数执行的时候有问题
int isEmpty_SeqQueue(SeqQueue* seqqueue) {int ret = 0;if (seqqueue == NULL) {ret = -1;printf("isEmpty_SeqQueue func error because stack==NULL and return\n");return ret;}TSeqQueue * tq = (TSeqQueue *)seqqueue;if (tq->m_QueueSize ==0) {ret = 1;}else {ret = 0;}return ret;
}//销毁队列
//成功 返回1 表示成功销毁队列 
//失败 返回-1 表示该函数执行的时候有问题
int Destory_SeqQueue(SeqQueue* seqqueue) {int ret = 1;if (seqqueue == NULL) {ret = -1;printf("isEmpty_SeqQueue func error because stack==NULL and return\n");return ret;}TSeqQueue * tq = (TSeqQueue *)seqqueue;if (tq->data!=NULL) {free(tq->data);}tq->data = NULL;free(tq);seqqueue = NULL;return ret;}

#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include "iostream"
#include <stdio.h>
#include <stdlib.h>extern "C" {
#include "005seqqueue.h"
}typedef struct Teacher {int age;char name[128];char *othername;char **stuname; //一个老师下面有5个学生
}Teacher;int main() {_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);//程序退出时检测内存泄漏并显示到“输出”窗口int ret = 0;//初始化队列,要动态的创建SeqStack,根据user设定的大小创建
//int stacksize ,表示user 要创建栈的大小
//创建失败返回NULLSeqQueue* seqqueue = createSeqQueue(1024);if (seqqueue == NULL) {ret = -1;printf("func createSeqQueue error because seqstack = NULL return ret =-1\n");return ret;}ret = isEmpty_SeqQueue(seqqueue);printf("isEmpty_SeqQueue = ret %d\n", ret);ret = size_SeqQueue(seqqueue);printf("size_SeqQueue = ret %d\n", ret);ret = capacity_SeqQueue(seqqueue);printf("capacity_SeqQueue = ret %d\n", ret);Teacher tea1;tea1.age = 20;strcpy(tea1.name, (const char*)"tea1");tea1.othername = (char *)malloc(sizeof(char) * 128);memset(tea1.othername, 0, sizeof(char) * 128);strcpy(tea1.othername, (const char*)"tea1othername");tea1.stuname = (char **)malloc(sizeof(char *) * 5);memset(tea1.stuname, 0, sizeof(char *) * 5);for (size_t i = 0; i < 5; i++){tea1.stuname[i] = (char *)malloc(sizeof(char) * 128);//每个学生名字也有128个字符memset(tea1.stuname[i], 0, sizeof(char) * 128);sprintf(tea1.stuname[i], "tea1stuname%d", i + 1);}Teacher tea2;tea2.age = 22;strcpy(tea2.name, (const char*)"tea2");tea2.othername = (char *)malloc(sizeof(char) * 128);memset(tea2.othername, 0, sizeof(char) * 128);strcpy(tea2.othername, (const char*)"tea2othername");tea2.stuname = (char **)malloc(sizeof(char *) * 5);memset(tea2.stuname, 0, sizeof(char *) * 5);for (size_t i = 0; i < 5; i++){tea2.stuname[i] = (char *)malloc(sizeof(char) * 128);//每个学生名字也有128个字符memset(tea2.stuname[i], 0, sizeof(char) * 128);sprintf(tea2.stuname[i], "tea2stuname%d", i + 1);}Teacher tea3;tea3.age = 33;strcpy(tea3.name, (const char*)"tea3");tea3.othername = (char *)malloc(sizeof(char) * 128);memset(tea3.othername, 0, sizeof(char) * 128);strcpy(tea3.othername, (const char*)"tea3othername");tea3.stuname = (char **)malloc(sizeof(char *) * 5);memset(tea3.stuname, 0, sizeof(char *) * 5);for (size_t i = 0; i < 5; i++){tea3.stuname[i] = (char *)malloc(sizeof(char) * 128);//每个学生名字也有128个字符memset(tea3.stuname[i], 0, sizeof(char) * 128);sprintf(tea3.stuname[i], "tea3stuname%d", i + 1);}ret = push_SeqQueue(seqqueue, &tea1);if (ret < 0) {printf("push_SeqQueue(seqqueue, (SeqStackNode * )&tea1) func error ret =%d \n", ret);return ret;}push_SeqQueue(seqqueue, &tea2);if (ret < 0) {printf("push_SeqQueue(seqqueue, (SeqStackNode * )&tea2) func error ret =%d \n", ret);return ret;}push_SeqQueue(seqqueue, &tea3);if (ret < 0) {printf("push_SeqQueue(seqqueue, (SeqStackNode * )&tea3) func error ret =%d \n", ret);return ret;}printf("-after-\n");ret = isEmpty_SeqQueue(seqqueue);printf("isEmpty_SeqQueue = ret %d\n", ret);ret = size_SeqQueue(seqqueue);printf("size_SeqQueue = ret %d\n", ret);ret = capacity_SeqQueue(seqqueue);printf("capacity_SeqQueue = ret %d\n", ret);while (size_SeqQueue(seqqueue) > 0) {Teacher * temptea = (Teacher *)top_SeqQueue(seqqueue);if (temptea == NULL) {printf("can not get find teacher\n");}printf("temptea->age = %d,temptea->name = %s,temptea->othername=%s\n",temptea->age,temptea->name,temptea->othername);for (size_t j = 0; j < 5; j++){printf("temptea->stuname[%d] = %s,  ",j, temptea->stuname[j]);}Teacher * deltea = (Teacher *)pop_SeqQueue(seqqueue);if (deltea == NULL) {printf("pop_SeqStack seqstack error\n");}if (deltea->othername != NULL) {free(deltea->othername);}if (deltea->stuname != NULL) {for (size_t i = 0; i < 5; i++){if (deltea->stuname[i] != NULL) {free(deltea->stuname[i]);}}free(deltea->stuname);deltea->stuname = NULL;}printf("\n");}printf("sss\n");//销毁栈//成功 返回1 表示成功销毁栈 //失败 返回-1 表示该函数执行的时候有问题ret = Destory_SeqQueue(seqqueue);return 0;}

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

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

相关文章

这8款3DMAX建筑室内插件一个都不能少!

3DMax是一款经典的建筑室内场景设计和渲染软件&#xff0c;它能够帮助3D设计师、建筑师和艺术家实现他们的创意概念。本文推荐的8款建筑室内插件&#xff0c;将使3DMax如虎添翼&#xff0c;大大节约设计师们的工作时间&#xff0c;提高工作效率。 1.3DMAX楼层平面图生成器&…

idea使用plantuml插件报错(类图):Dot Executable: /opt/local/bin/dot

报错提示&#xff1a; 解决方式&#xff1a; 方式一: 直接设置Remote Rendering即可 &#xff08;使用服务器地址&#xff09; 无特殊要求可直接使用默认提供的服务地址&#xff0c;也可自行搭建服务替换地址。 自行搭建服务可参考&#xff1a; 在本地Windows 11 系统的桌面…

C++ 程序的内存分配

C 程序的内存分配 C 程序的内存分配栈堆数据区程序代码区参考 C 程序的内存分配 一个 C 编译的程序占用内存分为以下几个部分&#xff08;从高地址到低地址&#xff09;&#xff1a; 内核空间&#xff1a;由操作系统创建并控制&#xff0c;用户代码不能读写。栈&#xff1a;由…

【ds】替换空格

用‘%20’替换空格 var replaceBlank (charArr)> {if (!charArr || charArr.length 0) return var len charArr.lengthlet spaceLen 0for (let i 0; i < len; i) {if (charArr[i] ) {spaceLen}}var extraLen spaceLen * 2 // -> 20% 每一个空格需要增加2个ch…

Pytest精通指南(23)钩子函数-执行顺序(pytest-ordering)

文章目录 前言应用场景插件安装参数分析装饰方法装饰类装饰模块 前言 pytest-ordering 是一个pytest插件&#xff0c;它允许用户自定义测试用例的执行顺序。 默认情况下&#xff0c;pytest会按照模块、类、函数定义的顺序以及它们的名称的字母顺序来执行测试用例。 但通过使用 …

Kafka集群搭建可视化指南

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 Kafka集群搭建可视化指南 前言准备工作硬件要求环境准备 kafka集群的部署与配置3.1 单节点部署与多节点集群搭建单节点部署&#xff1a;多节点集群搭建&#xff1a; 3.2 Broker配置与优化3.3 Topic的创…

如何30天快速掌握键盘盲打

失业后在家备考公务员&#xff0c;发现了自己不正确的打字方式&#xff0c;决定每天抽出一点时间练习打字。在抖音上看到一些高手的飞速盲打键盘后&#xff0c;觉得使用正确的指法打字是很必要的。 练习打字&#xff0c;掌握正确的键盘指法十分关键。 练习打字的第一步是找到…

RIP最短路实验(思科)

华为设备参考&#xff1a;RIP最短路实验&#xff08;华为&#xff09; 一&#xff0c;技术简介 RIP&#xff08;Routing Information Protocol&#xff0c;路由信息协议&#xff09;是一种基于距离矢量的内部网关协议&#xff0c;工作原理是每个路由器周期性地向邻居路由器发…

分享一款嵌入式开源按键框架代码工程MultiButton

一、工程简介 MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块。 Github地址&#xff1a;https://github.com/0x1abin/MultiButton 这个项目非常精简&#xff0c;只有两个文件&#xff1a; &#xff08;1&#xff09;可无限扩展按键&#xff1b; &#xff08;2&#x…

sublime text的json快捷键

系统 macos 配置 sublime Text->Settings->Key Bindings 效果 可以看到&#xff0c;按&#xff1a;shiftcommandp&#xff0c;会出现快捷键窗口&#xff0c;打pretty&#xff0c;会出现Format JSON&#xff0c;最右侧显示⌘J&#xff0c;说明只需要macos的⌘和J同时按…

垃圾回收知识整理

1.为什么要有垃圾回收 提高开发效率:程序员无需显式地分配和释放内存&#xff0c;这是由Java虚拟机&#xff08;JVM&#xff09;自动处理的。这种自动内存管理大大简化了程序员的工作。 减少程序错误: 手动管理内存致各种内存管理错误&#xff08;如内存泄漏、野指针等&#xf…

linux 设备树-of_address_to_resource

实例分析-reg 属性解析(基于ranges属性) /{#address-cells <0x01>;#size-cells <0x01>;soc {compatible "simple-bus";#address-cells <0x01>;#size-cells <0x01>;ranges <0x7e000000 0x3f000000 0x1000000 0x40000000 0x40000000…