当前位置:网站首页>I 用c I 实现队列
I 用c I 实现队列
2022-07-05 07:22:00 【MT_125】
目录
一、队列的性质:
上次我们学习栈,了解到栈储存释放数据的方式是:先进后出
而队列与其相反,队列是:先进先出,后进后出。
二、队列的结构:
多个链表节点 + 头尾指针 (链表式队列)
链表节点负责存储数据;头节点 负责定位先进的起始数据,方便先出;
尾节点负责记录尾部数据,方便确定队列当前状态。

三、代码实现
头文件:
这里方便统一调用,将头尾指针定义成一个结构体 。
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int Quetype; //定义队列的数据类型
typedef struct QueNode //定义数据节点
{
struct QueNode* Next;
Quetype data;
}QueNode;
typedef struct Quetail
{
struct QueNode* head; //定义头尾指针
struct QueNode* tail;
}Quetail;
void Que_Init(Quetail* pq); //队列的初始化
void Que_Destory(Quetail* pq); //队列的销毁
void Que_push(Quetail* pq ,Quetype data); //插入数据
void Que_pop(Quetail* pq); //删除数据
bool Que_Empty(Quetail* pq); //判断队列是否为空
int Que_size(Quetail* pq); //统计队列长度
int Que_front(Quetail* pq); //查找队列的头部数据功能函数:
1.队列的初始化:
将头尾指针置为NULL 方便后续使用。
void Que_Init(Quetail* pq) //队列的初始化
{
assert(pq);
pq->head = pq->tail = NULL;
}
2.插入数据:
创建链表节点 >> 导入数据 >> 头部指针指向头节点 >> 尾部指针指向尾节点
//插入数据
void Que_push(Quetail* pq,Quetype data)
{
assert(pq);
QueNode* NewNode = (QueNode*)malloc(sizeof(QueNode));//创建节点
if (NewNode == NULL)
{
printf("Que_push->malloc");
exit(-1);
}
NewNode->Next = NULL;
NewNode->data = data;
if (pq->head == NULL) //判断是否创建为头节点
{
pq->head = NewNode; //更新头指针
}
else
{
pq->tail->Next = NewNode; //不为头节点,就正常链接在尾节点后
}
pq->tail = NewNode; //更新尾指针
}3.删除数据:
记录头节点的下一个位置 >> 判断是否为最后的数据 >> 更新头指针
细节点:如果队列中还剩多个节点时,删除头节点后,尾指针始终指向尾节点,不需要改动;
但是如果只剩一个数据节点的话,删除后需要将尾指针置空。

//删除数据
void Que_pop(Quetail* pq)
{
assert(pq);
assert(!Que_Empty(pq)); //判断队列是否为空
QueNode* Next = pq->head->Next; //记录删除数据的
if (pq->head == pq->tail) //判断是否是最后的数据
{
free(pq->head);
pq->tail = NULL; //更新尾指针
}
else
{
free(pq->head);
}
pq->head = Next; //更新头指针
}4.判断列表是否为空:
用bool 作为返回类型

//判断队列是否为空
bool Que_Empty(Quetail* pq)
{
assert(pq);
return pq->head == NULL;
}5.查找队列的头部数据:
判断队列是否为空 >> 返回头部数据
//查找队列的头部数据
Quetype Que_front(Quetail* pq)
{
assert(pq);
assert(!Que_Empty(pq)); //判断队列是否为空
return pq->head->data; //返回头部数据
}6. 统计队列的长度:
就是统计有多少个链表节点
int Que_size(Quetail* pq)
{
assert(pq);
int size;
QueNode* pphead = pq->head;
for (size = 0; pphead; pphead = pphead->Next, size++);
return size;
}7.队列的销毁:
依次删除数据 >> 将申请空间释放
细节点:这里可以进行复用:判断队列是否为空 、 删除数据
void Que_Destory(Quetail* pq)
{
for (; !Que_Empty(pq); ) //判断队列是否为空
{
Que_pop(pq); //删除数据
}
}感谢各位大佬的支持!!!

边栏推荐
- SD_ CMD_ SEND_ SHIFT_ REGISTER
- Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
- 目标检测系列——Faster R-CNN原理详解
- U-boot initialization and workflow analysis
- 1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
- Course learning accumulation ppt
- Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
- IPage can display data normally, but total is always equal to 0
- Unconventional ending disconnected from the target VM, address: '127.0.0.1:62635', transport: 'socket‘
- [solved] there is something wrong with the image
猜你喜欢

1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS

Ros2 - function package (VI)

Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui

Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘

PHY drive commissioning - phy controller drive (II)

window navicat连接阿里云服务器mysql步骤及常见问题

Ros2 topic (VIII)

PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)
![[software testing] 06 -- basic process of software testing](/img/fe/3d8b9b68f95ac7899ab87d6993284d.jpg)
[software testing] 06 -- basic process of software testing

一文揭开,测试外包公司的真实情况
随机推荐
HDU1231 最大连续子序列(分治or动规or双指针)
Rough notes of C language (1)
氫氧化鈉是什麼?
[vscode] prohibit the pylance plug-in from automatically adding import
Reading literature sorting 20220104
Interpretation of the earliest sketches - image translation work sketchygan
What is soda?
CADD课程学习(5)-- 构建靶点已知的化合结构(ChemDraw)
Simple operation with independent keys (hey, a little fancy) (keil5)
DelayQueue延迟队列的使用和场景
目标检测系列——Faster R-CNN原理详解
Energy conservation and creating energy gap
氢氧化钠是什么?
2022.06.27_每日一题
Ros2 - Service Service (IX)
The golang timer uses the stepped pit: the timer is executed once a day
Mid 2022 documentary -- the experience of an ordinary person
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
Ros2 - ros2 vs. ros1 (II)
Concurrent programming - deadlock troubleshooting and handling