当前位置:网站首页>队列的介绍与实现(详解)
队列的介绍与实现(详解)
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;
}
}
边栏推荐
- 为什么学编程的人大多数都去了深圳和北京?
- PHP获取小程序码,小程序带参数跳转
- 2021-04-02
- CoDeSys realizes bubble sorting
- QT QString详解
- Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
- Roson的Qt之旅#102 ListModel
- Automatic conversion and cast
- Fifth uncle's thinking
- I can only sell the company after the capital has been "cut off" for two years
猜你喜欢

视频号找到金钥匙,抖音模仿后来人

Installation points and precautions of split angle probe

CoDeSys realizes bubble sorting

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

laravel

500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued
![[Multisim Simulation] LM339 zero crossing circuit simulation](/img/ca/f6dae5fd298c00570407c2bdfa5118.png)
[Multisim Simulation] LM339 zero crossing circuit simulation

Rosen's QT journey 101 models and views in QT quick

Two of C language programming!! Role of

Huada chip hc32f4a0 realizes RS485 communication DMA transceiver
随机推荐
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
R language ggplot2 visually draws line plots, and uses gghighlight package to highlight the lines that meet the combination judgment conditions in the line graphs (satisfies both condition a and b)
2021 Yahong pen test questions
PHP计算坐标距离
自动打包压缩备份下载及删除 bat脚本命令
The little red book of accelerating investment, "rush to medical treatment"?
5 亿用户,比微信还早四年……这个运营了 15 年的 APP 即将永久停服
PHP图片合成技术
李宏毅《机器学习》丨4. Deep Learning(深度学习)
Qt学习之信号和槽机制
Stm32f103c8t6 + 0.96 "I2C OLED display 3d_cube
RF module wireless transceiver rf63u chip application data transmission and infrastructure network
IT远程运维是什么意思?远程运维软件哪个好?
QT打包
加速投资的小红书,“病急乱投医”?
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
Redis series 4: sentinel (sentinel mode) with high availability
Fifth uncle's thinking
Brief tutorial for soft exam system architecture designer | software debugging
深入理解Istio流量管理的熔断配置