队列的概念及结构(内有成型代码可供CV工程师参考)

简介: 队列的概念及结构(内有成型代码可供CV工程师参考)

前言以及队列全部代码(CV工程师点这里)


       前言:前面我们学习了链表以及栈的知识,他们都是数据结构中的重要知识点,接下来我们来学习一下队列有关的知识。还是老套路二话不说,先上代码


#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
  struct QueueNode* next;
  QDataType data;
}QNode;
typedef struct Queue
{
  QNode* phead;
  QNode* ptail;
  int size;
}Queue;
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
bool QueueEmpty(Queue* pq);
void QueueInit(Queue* pq)
{
  assert(pq);
  pq->phead = NULL;
  pq->ptail = NULL;
  pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
  assert(pq);
  QNode* cur = pq->phead;
  while (cur)
  {
  QNode* next = cur->next;
  free(cur);
  cur = next;
  }
  pq->phead = pq->ptail = NULL;
  pq->size = 0;
}
void QueuePush(Queue* pq, QDataType x)
{
  assert(pq);
  QNode* newnode = (QNode*)malloc(sizeof(QNode));
  if (newnode == NULL)
  {
  perror("malloc fail\n");
  return;
  }
  newnode->data = x;
  newnode->next = NULL;
  if (pq->ptail == NULL)
  {
  assert(pq->phead == NULL);
  pq->phead = pq->ptail = newnode;
  }
  else
  {
  pq->ptail->next = newnode;
  pq->ptail = newnode;
  }
  pq->size++;
}
void QueuePop(Queue* pq)
{
  assert(pq);
  assert(!QueueEmpty(pq));
  if (pq->phead->next ==NULL)
  {
  QNode* head = pq->phead;
  free(head);
  pq->phead = pq->ptail = NULL;
  pq->size = 0;
  }
  else
  {
  QNode* head = pq->phead;
  pq->phead = pq->phead->next;
  free(head);
  pq->size--;
  }
}
QDataType QueueFront(Queue* pq)
{
  assert(pq);
  assert(!QueueEmpty(pq));
  return(pq->phead->data);
}
QDataType QueueBack(Queue* pq)
{
  assert(pq);
  assert(!QueueEmpty(pq));
  return pq->ptail->data;
}
int QueueSize(Queue* pq)
{
  assert(pq);
  return pq->size;
}
bool QueueEmpty(Queue* pq)
{
  assert(pq);
  return pq->size == 0;
}


一、队列的概念


       队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)


       入队列:进行插入操作的一端称为队尾


       出队列:进行删除操作的一端称为队头


ef15adf2909b1359946f582373db2734_0160d52a940b406abd7d7805f0264c03.png


二、队列的实现


       队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

7bd72952df38c8f199ea03140a95f02b_6878cb6c4bfe48798d1091bd22aa1184.png

3fa91b3fe757eb653ce38062397c1ded_051df0b8baef1f790dbfde1a4b6aa4e4.jpeg


4f25d28ce2deec8e25500b2964cb58c4_defd26e59e604734a6f5d377bfee9272.png


三、代码实现以及详细解释


       1. 初步介绍


1. 初始化队列                   void QueueInit(Queue* pq);

2. 队列的销毁                   void QueueDestroy(Queue* pq);

3. 队列插入元素(尾插) void QueuePush(Queue* pq, QDataType x);

4. 删除队头元素               void QueuePop(Queue* pq);

5. 返回队头元素               QDataType QueueFront(Queue* pq);

6. 返回队尾元素               QDataType QueueBack(Queue* pq);

7. 求队列的长度               int QueueSize(Queue* pq);

8. 判断是否为空               bool QueueEmpty(Queue* pq);


       2.  定义结构体,以及栈内数据类型


代码:


#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{
  struct QueueNode* next;
  QDataType data;
}QNode;
typedef struct Queue
{
  QNode* phead;
  QNode* ptail;
  int size;
}Queue;


       3. 初始化队列


代码:


void QueueInit(Queue* pq)
{
  assert(pq);
  pq->phead = NULL;
  pq->ptail = NULL;
  pq->size = 0;
}


       4.队列的销毁


代码:


void QueueDestroy(Queue* pq)
{
  assert(pq);
  QNode* cur = pq->phead;
  while (cur)
  {
  QNode* next = cur->next;
  free(cur);
  cur = next;
  }
  pq->phead = pq->ptail = NULL;
  pq->size = 0;
}


       5. 队列插入元素(尾插)


代码:


void QueuePush(Queue* pq, QDataType x)
{
  assert(pq);
  QNode* newnode = (QNode*)malloc(sizeof(QNode));
  if (newnode == NULL)
  {
  perror("malloc fail\n");
  return;
  }
  newnode->data = x;
  newnode->next = NULL;
  if (pq->ptail == NULL)
  {
  assert(pq->phead == NULL);
  pq->phead = pq->ptail = newnode;
  }
  else
  {
  pq->ptail->next = newnode;
  pq->ptail = newnode;
  }
  pq->size++;
}


       6.删除队头元素


代码:


void QueuePop(Queue* pq)
{
  assert(pq);
  assert(!QueueEmpty(pq));
  if (pq->phead->next ==NULL)
  {
  QNode* head = pq->phead;
  free(head);
  pq->phead = pq->ptail = NULL;
  pq->size = 0;
  }
  else
  {
  QNode* head = pq->phead;
  pq->phead = pq->phead->next;
  free(head);
  pq->size--;
  }
}


       7.返回队头元素


代码:


QDataType QueueFront(Queue* pq)
{
  assert(pq);
  assert(!QueueEmpty(pq));
  return(pq->phead->data);
}


       8. 返回队尾元素


代码:


QDataType QueueBack(Queue* pq)
{
  assert(pq);
  assert(!QueueEmpty(pq));
  return pq->ptail->data;
}


       9.求队列的长度  


代码:


int QueueSize(Queue* pq)
{
  assert(pq);
  return pq->size;
}


       10. 判断是否为空


代码:


bool QueueEmpty(Queue* pq)
{
  assert(pq);
  return pq->size == 0;
}

目录
相关文章
|
存储 NoSQL Java
使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)
该博客文章展示了如何在Linux虚拟机上使用Redis和Jedis客户端实现手机验证码的验证功能,包括验证码的生成、存储、验证以及限制每天发送次数的逻辑,并提供了测试结果截图。
使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)
|
数据采集 机器学习/深度学习 数据挖掘
从混乱到有序,Python数据清洗术,让你的数据分析之路畅通无阻!
【7月更文挑战第20天】数据清洗在数据分析中至关重要,它确保数据质量,影响分析准确性和效率。Python的Pandas库是数据预处理的得力工具。基本步骤包括:导入数据(如`pd.read_csv()`)、检查概况(`head()`, `info()`, `describe()`)、处理缺失值(`fillna()`或`dropna()`)、转换数据类型(`pd.to_numeric()`)、去除重复项(`drop_duplicates()`)、排序和筛选数据,以及对分类变量编码(如使用`LabelEncoder`)。
296 3
|
iOS开发 MacOS Python
Python 虚拟环境及pip环境管理
`venv`是Python的虚拟环境管理工具,提供独立的环境避免包冲突,便于管理与删除。创建虚拟环境使用`python3 -m venv test`,激活环境在Windows上运行`. Scripts\activate`,macOS上运行`. bin\activate`。安装Python包通过`python`或`python3`选择版本,使用`pip`进行安装、升级和卸载。`pip`是Python包管理器,自2.7.9和3.4版本起自带,常用命令包括查看版本、安装、升级和卸载包。为提高速度,可使用国内镜像源如阿里云、清华或豆瓣。
|
监控
LabVIEW程序内存泄漏分析与解决方案
LabVIEW程序内存泄漏分析与解决方案
650 0
|
SQL 存储 运维
开启 Alibaba Cloud Lens Copilot 探索之旅
Alibaba Cloud Lens Copilot 大模型助力云设施运维与运营:Alibaba Cloud Lens Copilot 采用基于通义模型的 NL2Query 技术,融合 Alibaba Cloud Lens 中的资产数据与知识图谱,分析运维场景多模态数据,实现了 Q/A 查询和应答功能,准确将自然语言翻译查询语句及时找到结果。
110282 3
v-model绑定
v-model绑定
171 0
Mac神兵利器(四)时间管理工具
时间管理是高效工作与生活必备的核心技能之一,下文将从时间管理知识与时间管理工具及时间管理实践三个方面进行阐述。 一、时间管理知识 二、时间管理工具 Mac Calendar 原生的Mac App,支持多终端,可以通过iCloud通过 定位:时间规划、日程管理 功能列表: 日程管理与提醒 日...
23847 173
|
API Android开发
对于应用研发平台EMAS中安卓 API 32 收不到 FCM 推送的问题
对于应用研发平台EMAS中安卓 API 32 收不到 FCM 推送的问题
223 3
|
机器学习/深度学习 人工智能 算法
视觉智能平台常见问题之人脸比对1:1的服务离线使用如何解决
视觉智能平台是利用机器学习和图像处理技术,提供图像识别、视频分析等智能视觉服务的平台;本合集针对该平台在使用中遇到的常见问题进行了收集和解答,以帮助开发者和企业用户在整合和部署视觉智能解决方案时,能够更快地定位问题并找到有效的解决策略。
197 0
|
存储 持续交付 Docker
介绍 Docker 的基本概念和优势,以及在应用程序开发中的实际应用
介绍 Docker 的基本概念和优势,以及在应用程序开发中的实际应用
193 0