当前位置:网站首页>C language queue
C language queue
2022-07-07 07:57:00 【pythoncjavac++】
Catalog
Determines if the queue is empty
Queue related concepts
First of all, the biggest feature of the queue is “ fifo ”, Only insert... At one end , Delete on the other end , The inserted end is the tail of the team , The deleted end is the head of the team .
Queue creation
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;Here we will create two structures , Because here we need to record his head and tail , This makes it easy to insert and delete .
Initialization of the queue
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}Queue insertion
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;
}
}Queue deletion
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;
}
}Take the head of the queue
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}Take the end of the queue
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}Number of queue elements
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QNode* cur = pq->head;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}Determines if the queue is empty
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}Destroy queue
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;
}边栏推荐
- Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
- Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
- Rust Versus Go(哪种是我的首选语言?)
- Sign up now | oar hacker marathon phase III, waiting for your challenge
- 2022焊工(初级)判断题及在线模拟考试
- leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
- Idea add class annotation template and method template
- 【VHDL 并行语句执行】
- 探索Cassandra的去中心化分布式架构
- 自定义类加载器加载网络Class
猜你喜欢
![[Stanford Jiwang cs144 project] lab4: tcpconnection](/img/fd/704d19287a12290f779cfc223c71c8.png)
[Stanford Jiwang cs144 project] lab4: tcpconnection
![[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
![[GUET-CTF2019]虚假的压缩包](/img/a2/7da2a789eb49fa0df256ab565d5f0e.png)
[GUET-CTF2019]虚假的压缩包

After the interview, the interviewer roast in the circle of friends
![[webrtc] m98 Screen and Window Collection](/img/b1/1ca13b6d3fdbf18ff5205ed5584eef.png)
[webrtc] m98 Screen and Window Collection

Idea add class annotation template and method template

Technology cloud report: from robot to Cobot, human-computer integration is creating an era
![[2022 actf] Web Topic recurrence](/img/e4/ab9a1771489d751ee73a79f151d374.png)
[2022 actf] Web Topic recurrence

Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)

2022焊工(初级)判断题及在线模拟考试
随机推荐
[experience sharing] how to expand the cloud service icon for Visio
SQL优化的魅力!从 30248s 到 0.001s
C语言航班订票系统
Rust versus go (which is my preferred language?)
Shell 脚本的替换功能实现
[Stanford Jiwang cs144 project] lab4: tcpconnection
Cnopendata geographical distribution data of religious places in China
IO stream file
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
Li Kou interview question 04.01 Path between nodes
Iterable、Collection、List 的常见方法签名以及含义
C语言队列
C language communication travel card background system
【VHDL 并行语句执行】
Thinkcmf6.0安装教程
[Stanford Jiwang cs144 project] lab3: tcpsender
C语言通信行程卡后台系统
2022制冷与空调设备运行操作复训题库及答案
【数学笔记】弧度
What is the interval in gatk4??