当前位置:网站首页>C语言队列
C语言队列
2022-07-07 04:33:00 【pythoncjavac++】
目录
队列有关概念
首先队列最大的特点是“先进先出”,只能在一端插入,另一端删除,插入的一端是队尾,删除的一端是队头。
队列的创建
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;这里要创建两个结构体,因为这里要记录一下他的头和尾,这样可以方便进行插入和删除。
队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}队列的插入
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
pq->tail = pq->head = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}队列的删除
void QueuePoq(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* cur = pq->head->next;
free(pq->head);
pq->head = cur;
}
}取队列的头
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}取队列的尾
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}队列元素的数量
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QNode* cur = pq->head;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}判断队列是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}销毁队列
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* aq = cur->next;
free(cur);
cur = aq;
}
pq->head = pq->tail = NULL;
}边栏推荐
- leanote私有云笔记搭建
- @component(““)
- [UVM foundation] what is transaction
- Common method signatures and meanings of Iterable, collection and list
- 今日现货白银操作建议
- [SUCTF 2019]Game
- Redis technology leak detection and filling (II) - expired deletion strategy
- 知识点滴 - 关于苹果认证MFI
- numpy中dot函数使用与解析
- Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
猜你喜欢

After the interview, the interviewer roast in the circle of friends
![[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替

KBU1510-ASEMI电源专用15A整流桥KBU1510

测试周期被压缩?教你9个方法去应对

探索干货篇!Apifox 建设思路

Use and analysis of dot function in numpy

IO流 file

有 Docker 谁还在自己本地安装 Mysql ?

Problem solving: unable to connect to redis
![[Stanford Jiwang cs144 project] lab3: tcpsender](/img/82/5f99296764937e7d119b8ab22828fd.png)
[Stanford Jiwang cs144 project] lab3: tcpsender
随机推荐
Figure out the working principle of gpt3
【性能压测】如何做好性能压测?
【obs】win-capture需要winrt
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
[2022 ciscn] replay of preliminary web topics
Info | webrtc M97 update
Weibo publishing cases
Visualization Document Feb 12 16:42
[GUET-CTF2019]虚假的压缩包
Idea add class annotation template and method template
Qt学习28 主窗口中的工具栏
Button wizard script learning - about tmall grabbing red envelopes
今日现货白银操作建议
Explore Cassandra's decentralized distributed architecture
nacos
@component(““)
Tongda injection 0day
C语言通信行程卡后台系统
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)