当前位置:网站首页>队列的介绍与实现(详解)
队列的介绍与实现(详解)
2022-07-28 15:28:00 【世_生】
一、什么是队列
队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表
二、队列的结构
队列是一种先进先出(First In First Out)的线性表,简称FIFO。
队头:允许删除的一端称为队头
队尾:允许插入的一端称为队尾
假设队列是q=(A1,A2,A3·····An),那么A1为队头元素,An为队尾元素。我们在删除时就从A1开始,插入时就从An开始。
如图:
三、队列的实现
我是以链式存储结构实现的,你们也可以用线性存储结构实现。
先创建队列结构体
head:在创建节点的时候,我们需要head指针指向队头
tail:需要head指针指向队尾
next:指针域,用来连接节点的
data:数据
typedef int QDataType;
struct queue
{
struct QListNode*head;
struct QListNode*tail;
};
struct QListNode
{
struct QlistNode*next;
QDataType data;
};
1.初始化队列操作
先初始化队列。
void InitQueue(struct queue*qst)
{
qst->head = NULL;
qst->tail = NULL;
}
2.队列存在,则销毁队列
先断言一下qst
销毁队列时一定要释放掉动态开辟的空间,避免内存泄漏。
void DestroyQueue(struct queue*qst)
{
assert(qst);
struct QListNode*cur = qst->head;
while (cur)
{
struct QListNode*next = cur->next;
free(cur);
cur = next;
}
qst->head = NULL;
qst->tail = NULL;
}
3.队列清空
在不是释放空间的情况下,直接把tail指向队头。
void ClearQueue(struct queue*qst)
{
assert(qst);
assert(!EmptyQueue(qst));
qst->tail = qst->head;
}
4.判断队列是否为空
判断有没有节点。
QDataType EmptyQueue(struct queue*qst)
{
assert(qst);
return qst->head == NULL;
}
5.返回队列队头元素
QDataType GetHead(struct queue*qst)
{
assert(qst);
assert(!EmptyQueue(qst));//断言队列是否为空
return qst->head->data;//返回头元素
}
6.返回队列元素的个数
用计数法来计算元素的个数
QDataType SizeQueue(struct queue*qst)//队列中元素的个数
{
assert(qst);
assert(!EmptyQueue(qst));
int count = 0;
struct QListNode*cur = qst->head;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
7.插入
先断言,后判断是否有节点,然后连接起来。
开辟空间的next要为空
void PuchQueue(struct queue*qst, QDataType x)
{
assert(qst);
struct QListNode*newhead =(struct QListNode*) malloc(sizeof(struct QListNode));
if (newhead == NULL)
{
printf("malloc fail");
exit(-1);
}
newhead->next = NULL;
newhead->data = x;
if (qst->head == NULL)//队列中原本无节点
{
qst->head = qst->tail = newhead;//队头队尾直接指向新节点
}
else
{
struct QListNode*cur = qst->head;
cur->next = newhead;//连接节点
qst->tail = newhead;//队尾指向新节点,改变队尾
}
}
8.删除
删除一个元素,就是free掉这个节点,队列的队头也会改变。
void PopQueue(struct queue*qst)
{
assert(qst);
assert(!EmptyQueue(qst));
if (qst->head->next == NULL)//队列只有一个节点
{
free(qst->head);
qst->head = NULL;
qst->tail = NULL;
}
else//队列有多个节点
{
struct QListNode*cur = qst->head;
cur = cur->next;
free(qst->head);
qst->head = cur;
}
}
边栏推荐
- 2021-04-02
- “蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
- 2021 Yahong pen test questions
- 排序5-计数排序
- Two of C language programming!! Role of
- R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
- Record doc
- 一小时内学会Abaqus脚本编程秘籍
- Advantages of optical rain gauge over tipping bucket rain gauge
- A program for judging the result of cyclic input
猜你喜欢

1. Simple command line connection to database

排序3-选择排序与归并排序(递归实现+非递归实现)

Wei Jianjun couldn't catch up with Li Shufu by riding a BMW

Redis series 4: sentinel (sentinel mode) with high availability

Connection and application of portable borehole inclinometer data acquisition instrument and inclinometer probe

优化Hypermesh脚本性能的几点建议

ANSA二次开发 - 界面开发工具介绍

Paging query in applet

IT远程运维是什么意思?远程运维软件哪个好?

A good start
随机推荐
LabVIEW Linx toolkit controls Arduino equipment (expansion-1)
LwIP development | socket | TCP | client
Huada chip hc32f4a0 realizes RS485 communication DMA transceiver
flashfxp 530 User cannot log in. ftp
The epidemic dividend disappeared, and the "home fitness" foam dissipated
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
R language uses file of FS package_ Delete function deletes the specified file under the specified folder, draw inferences from one instance, dir_ Delete function, link_ The delete function can be use
加速投资的小红书,“病急乱投医”?
Numpy ndarray learning < I > Foundation
PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
Temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
PHP mb_ Substr Chinese garbled code
ANSA二次开发 - 界面开发工具介绍
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
C language exception handling mechanism: jump function jump function setjmp/sigsetjmp and longjmp/siglongjmp
2021-04-02
解决电脑恶意广告弹窗的思路
R language uses ggpairs function of ggally package to visualize pairwise relationship graph of grouping multivariable, set alpha parameter to change image transparency, diagonal continuous variable de
带你来浅聊一下!单商户功能模块汇总
CoDeSys realizes bubble sorting