队列的概念及结构(内有成型代码可供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;
}

目录
打赏
0
0
0
0
0
分享
相关文章
|
10月前
|
【软件设计师备考 专题 】编写内部设计文档:构件划分图和接口
【软件设计师备考 专题 】编写内部设计文档:构件划分图和接口
130 0
大模型的特点、重要概念及工作方式详解
大模型是具有大量参数和复杂结构的深度学习模型,通过处理大量数据实现高效任务解决。其特点包括参数规模庞大、深层网络结构、预训练与微调、多任务学习和自适应能力。重要概念有注意力机制、Transformer架构、迁移学习和分布式训练。大模型的工作方式包括输入处理、特征提取、预测与损失计算、反向传播与优化,以及评估与微调。这些特性使其在自然语言处理、计算机视觉等领域取得显著进展。
547 0
除庄周梦蝶外,庄子还讲过哪些梦你知道吗?新故事引出新版本——
除庄周梦蝶外,庄子还讲过哪些梦你知道吗?新故事引出新版本——
197 0
软件基本功:不会代码共用,因为没有设计能力;代码共用都不会,谈什么设计
软件基本功:不会代码共用,因为没有设计能力;代码共用都不会,谈什么设计
148 0
点线面的工作学习方式
  本文主要介绍我个人的一种工作学习方式:点线面的工作学习方式。希望对大家以后的工作和职业发展有所启发和帮助。   7月份的时候,我去京东外面的世界转了转,聊了聊。切身体会到:别人其实并不关心你之前做的具体工作,关心的是你从中得到了什么。当然,如果你是一直深耕一个业务领域的专家,除外,例如一直从事金融风控领域的技术开发。   面试中,我之前在啥啥公司做了啥啥项目,这个项目业务怎么怎么的复杂,功能怎么怎么的牛批,一顿业务功能的输出。   so ?然后呢 ?
164 0
带你读《好设计,有方法:我们在搜狐做产品体验设计》之三:区分不同载体的设计
那些激动人心、让人拍手叫好的设计,到底有没有方法可循?背后到底有没有设计理论支撑?答案是肯定的!本书作者是资深体验设计专家,拥有超过10年的产品体验设计和团队管理经验,他们将试图为大家总结和揭示那些优秀设计背后的理论和方法。
再谈研发那些事——两项核心工作的区别与联系
导读:不久前,技术工程师zhuoqun发表了两篇博客在《开发与研发:区别很大》和《开发与研发:领会编程魅力所在》引发了技术人士的热议。在那两篇文章里,zhuoqun谈到了程序开发两大类别:开发和研发的区别以及两类工程师的职业规划。
1309 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等