当前位置: 首页 > news >正文

队列的实现与讲解

一.概念与结构

1.概念

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

入队列:进⾏插⼊操作的⼀端称为队尾

​ 出队列:进⾏删除操作的⼀端称为队头

注意:与上文讲到的栈对比,栈是先进后出,队列是先进先出。

2.结构

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

数组头删时间复杂度:O(N) ** 数组尾插时间复杂度:O(1)

单链表头删时间复杂度:O(1) 单链表尾插时间复杂度:O(N)

 因此下午队列的实现中,我们采用链表的结构来进行。

二.队列的实现

queue.h

完整头文件如下。

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int QDataType;
typedef struct QueueNode//队列节点的结构,即单链表节点的结构
{QDataType data;struct QueueNode* next;
}QNode;
typedef struct Queue//队列的结构,定义指向队列头尾的指针,以及队列节点的个数
{QNode* phead;QNode* ptail;QDataType size;
}Q;void QueueInit(Q*);//入队列,队尾
void QueuePush(Q*, QDataType);//出队列,队头
void QueuePop(Q*);//队列判空
bool QueueEmpty(Q*);//取队头数据
QDataType QueueFront(Q*);//取队尾数据
QDataType QueueBack(Q*);//队列有效元素个数
int QueueSize(Q*);void QueueDestroy(Q*);

分析:

1.此处我们定义了两个结构体,一个是队列的基本结构QNode,一个是用来表示队列的队头,队尾和元素个数的Q。

2.Q存在的意义主要是为了便于后续表示队列的队头,队尾时可以简化二级指针的个数,且更加清晰。

test.c

相关测试代码如下。

#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueTest01()
{Q q;//定义队列QueueInit(&q);QueuePush(&q, 1);QueuePush(&q, 2);QueuePush(&q, 3);QueuePush(&q, 4);///printf("head:%d\n", QueueFront(&q));printf("tail:%d\n", QueueBack(&q));printf("size:%d\n", QueueSize(&q));QueuePop(&q);QueueDestroy(&q);
}int main()
{QueueTest01();return 0;
}

队列的初始化

#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueInit(Q* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}

分析:

1.需注意,我们此处传入的是指针Q*而非QNode*。

2.其余置空断言操作如常。

队列的销毁

void QueueDestroy(Q* pq)
{assert(pq);assert(!QueueEmpty(pq));QNode* pcur = pq->phead;while (pcur){QNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}

分析:

1.由于每个节点与之前的链表类似,都是动态开辟的,因此我们通过Q*找到队列头phead,之后逐个释放节点即可。

2.依次释放之后置空并将元素个数置为0即可。

节点的创建

首先创建一个专门用来生产节点的函数,避免后续插入时代码的冗余。

QNode* BuyNode(QDataType x)
{QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;return newnode;
}

注意:在创建节点后记得把size++。

队尾插入数据——入队列

void QueuePush(Q* pq, QDataType x)
{assert(pq);if (pq->phead == NULL)pq->phead = pq->ptail = BuyNode(x);else{pq->ptail->next = BuyNode(x);pq->ptail = pq->ptail->next;}pq->size++;
}

分析:与链表的尾插原理基本相同。

队列判空

在进入删除之前,首先需要判断队列数据个数是否为空。

bool QueueEmpty(Q* pq)
{assert(pq);return pq->phead == NULL && pq->ptail == NULL;
}

此处通过使用size个数是否为0进行判空也可。

队头删除数据——出队列

void QueuePop(Q* pq)
{assert(pq);assert(!QueueEmpty(pq));//只有一个节点的情况,避免ptail变成野指针if (pq->ptail == pq->phead){free(pq->phead);pq->phead = pq->ptail = NULL;}else{QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}

分析:

1.首先判断队列数据是否为空。

2.之后考虑两种情况,如果队列只有一个节点,那么直接删除之后需要将队头和队尾都置为空。

3,如果队列不止存在一个节点,同链表的删除原理类似。

返回队头数据

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

返回队尾数据

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

输出队列数据个数

int QueueSize(Q* pq)
{assert(pq);//不规范且时间复杂度O(n)//int size = 0;//QNode* pcur = pq->phead;//while (pcur)//{//	size++;//	pcur = pcur->next;//}//return size;return pq->size;}

分析:

1.如果我们不在最初引入变量size记录数据个数,由于队列是链表结构无法直接通过下标访问元素个数,需要逐个遍历记录,时间复杂度为O(n)。

2.直接返回size的话就可以减少时间消耗。

三.完整代码

#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueInit(Q* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}QNode* BuyNode(QDataType x)
{QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;return newnode;
}void QueuePush(Q* pq, QDataType x)
{assert(pq);if (pq->phead == NULL)pq->phead = pq->ptail = BuyNode(x);else{pq->ptail->next = BuyNode(x);pq->ptail = pq->ptail->next;}pq->size++;
}bool QueueEmpty(Q* pq)
{assert(pq);return pq->phead == NULL && pq->ptail == NULL;
}
void QueuePop(Q* pq)
{assert(pq);assert(!QueueEmpty(pq));//只有一个节点的情况,避免ptail变成野指针if (pq->ptail == pq->phead){free(pq->phead);pq->phead = pq->ptail = NULL;}else{QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}QDataType QueueFront(Q* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->phead->data;
}QDataType QueueBack(Q* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->ptail->data;
}int QueueSize(Q* pq)
{assert(pq);//不规范且时间复杂度O(n)//int size = 0;//QNode* pcur = pq->phead;//while (pcur)//{//	size++;//	pcur = pcur->next;//}//return size;return pq->size;}void QueueDestroy(Q* pq)
{assert(pq);assert(!QueueEmpty(pq));QNode* pcur = pq->phead;while (pcur){QNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}

以上就是关于队列的概念,结构和用链表方式实现队列的讲解,欢迎各位大佬前来支持斧正!!!

http://www.lryc.cn/news/453931.html

相关文章:

  • hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
  • 自动驾驶-参考线生成
  • 厂商资源分享网站
  • 【ONE·Web || HTML】
  • MongoDB的安装与增删改查基本操作
  • Python水循环标准化对比算法实现
  • 【TypeScript】知识点梳理(一)
  • DMA方式在执行中断处理程序时不中断现行程序吗
  • Redis:string类型
  • 【C++ STL】手撕vector,深入理解vector的底层
  • 【Android】CarWatchDog I/O监控服务
  • 如何使用 Django 框架进行用户认证的详细指南,涵盖用户注册和登录功能的实现。
  • C++ 语言特性21 - 别名模板
  • Jenkins pipeline配置示例
  • Navicat for MySQL 常见问题
  • Windows:win11旗舰版连接无线显示器,连接失败
  • Android2024.2.1升级错误
  • 【PHP陪玩系统源码】游戏陪玩系统app,陪玩小程序优势
  • Arduino UNO R3自学笔记20 之 Arduino如何测定电机速度?
  • ChatGPT全新功能Canvas上线:开启智能编程与写作新篇章
  • 南沙C++信奥赛陈老师解一本通题 2005:【20CSPJ普及组】直播获奖
  • Llama 3.2 视觉能力评估
  • 前端性能优化 面试如何完美回答
  • 程序猿成长之路之设计模式篇——设计模式简介
  • 基于Node2Vec的图嵌入实现过程
  • 国庆刷题(day4)
  • 如何在 Python 3 中制作一个计算器程序
  • 搭建shopify本地开发环境
  • 【在Linux世界中追寻伟大的One Piece】进程信号
  • MySQL中NULL值是否会影响索引的使用