当前位置:网站首页>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;
}
边栏推荐
- Iterable、Collection、List 的常见方法签名以及含义
- Visualization Document Feb 12 16:42
- 242. Bipartite graph determination
- Problem solving: unable to connect to redis
- Linux server development, MySQL stored procedures, functions and triggers
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- Wechat applet data binding multiple data
- Live broadcast platform source code, foldable menu bar
- Solve could not find or load the QT platform plugin "xcb" in "
- gslx680触摸屏驱动源码码分析(gslX680.c)
猜你喜欢
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
LeetCode 90:子集 II
numpy中dot函数使用与解析
Hands on deep learning (IV) -- convolutional neural network CNN
Visualization Document Feb 12 16:42
Codeforces Global Round 19
[guess-ctf2019] fake compressed packets
[GUET-CTF2019]虚假的压缩包
You Li takes you to talk about C language 6 (common keywords)
Linux server development, SQL statements, indexes, views, stored procedures, triggers
随机推荐
Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
pytest+allure+jenkins环境--填坑完毕
Problem solving: unable to connect to redis
【数学笔记】弧度
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
【斯坦福计网CS144项目】Lab4: TCPConnection
[UTCTF2020]file header
[performance pressure test] how to do a good job of performance pressure test?
Pytest+allure+jenkins environment -- completion of pit filling
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
Six methods of flattening arrays with JS
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
《动手学深度学习》(四) -- 卷积神经网络 CNN
JSON introduction and JS parsing JSON
Write CPU yourself -- Chapter 9 -- learning notes
@component(““)
解决:Could NOT find KF5 (missing: CoreAddons DBusAddons DocTools XmlGui)
Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
Cnopendata geographical distribution data of religious places in China
3D reconstruction - stereo correction