当前位置:网站首页>Basic operation of queue (implemented in C language)
Basic operation of queue (implemented in C language)
2022-07-01 13:53:00 【Brant_ zero】
This blog brings stack's good brother —— queue .
Catalog
One 、 The concept and structure of queue
Two 、 Framework Definition of queue
3、 ... and 、 The function realization of queue
3.1 Initialization of the queue
One 、 The concept and structure of queue
queue : Data insertion is only allowed at one end , A special linear table that deletes data at the other end , The queue has fifo (First In First Out) Characteristics of . Queue entry : The end that performs the insertion operation is called A party ; Outgoing queue : The section for deleting is called Team head .

Two 、 Framework Definition of queue
Queues can also be implemented using arrays or linked lists , Using the structure of linked list to realize more , Because if you use the structure of an array , Out of the queue in the array 0 Data is displayed at the subscript , It will be less efficient .
Next, let's take a look at the basic queue operations to be implemented this time .
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;
void QueueInit(Queue* pq); // Initialization of the queue
void QueueDestroy(Queue* pq); // Destruction of queues
void QueuePush(Queue* pq, QDataType x); // insert data
void QueuePop(Queue* pq); // Delete data
bool QueueEmpty(Queue* pq); // The queue is empty
QDataType QueueFront(Queue* pq); // Queue header data
QDataType QueueBck(Queue* pq); // End of queue data
int QueueSize(Queue* pq); // The size of the queue Be careful :
The stack uses arrays , Arrays are directly accessible . And if we use linked lists to implement queues , Set a head and tail The pointer indicates the head and tail of the team , among head and tail Are ordinary single linked list node structures , So they should be set next Pointer fields and data Data fields , Only in this way can we realize the characteristics of queues .
3、 ... and 、 The function realization of queue
3.1 Initialization of the queue
It is similar to the single linked list initialization implemented before , There is no more explanation here .
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}3.2 Destruction of queues
Because it is in the form of a linked list , So we can't release space directly in whole segment like array , To use while Release each node .
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* Dest = pq->head;
while (Dest)
{
QNode* temp = Dest->next;
free(Dest);
Dest = temp;
}
pq->head = pq->tail = NULL;
}3.3 Queue insert data
Steps of inserting data into the queue :
① take newnode link to head Node next;② take newnode Set as new tail node .
Here we pay attention , When we initialize , We will head and tail Nodes are set to NULL, So we should make a judgment when we insert data for the first time , If (head==NULL), Then we should tail and head Set to newnode node , Only in this way can the linked list be linked normally .
The notes are very detailed , You can see that .
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
// Create a node
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
// Judge pq Is it an empty node
// If it is Will newnode Set as the first node of the current queue
// If not Will newnode link to pq Next position of
if (pq->tail == NULL)
{ // Both nodes point to newnode Indicates that there is only one element in the queue
pq->head = pq->tail = newnode;
}
else
{
//1. take newnode link to tail Behind
pq->tail->next = newnode;
//2. take newnode Replace with new tail
pq->tail = newnode;
}
}3.4 Queue delete data
As mentioned above , The queue is fifo , So we Delete data from the head of the team .
Steps of deleting data in the queue :
① preservation head The location of the next node
② Release head node
③ Set the saved node as new head.
Be careful :
① First judge that the queue cannot be empty
② If there is only one node left in the queue , We released head Node and set it to NULL, But we didn't deal with tail node , here tail Nodes become classic wild pointers . So we need to make an extra judgment when there is only one node left , namely (pq->head->next==NULL), We want to release head node , And its head and tail All are set for NULL.
The code is as follows :
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
// Make an extra judgment , Solve if there is only one node left ,pop Words lead to tail For the wild pointer
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}3.5 The queue is empty
How to judge the empty queue ?
At this time, we can only judge head Is it NULL that will do , namely (head->next==NULL);
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}3.6 Take the header data
Be careful : For these operations of fetching and deleting data, we must first judge whether the queue is empty .
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}3.7 Take the tail data
QDataType QueueBck(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail ->data;
}3.8 The size of the queue
The length of linked list arrays can be calculated by calculating subscripts , So here we can only use circular calculation until cur==NULL;
int QueueSize(Queue *pq)
{
assert(pq);
int count = 0;
QNode* cur = pq->head;
while (cur)
{
cur = cur->next;
count++;
}
return count;
}Four 、 A functional test
The same test method as the stack is used to check whether our queue implementation is successful .
Again , Queues cannot be traversed , So we also need to take out a queue header data , Then pop up the header data , Until the queue is empty .

5、 ... and 、 summary
Compared to stack , The nature of the queue is quite different from that of the stack , Similarly, the chain structure we use here is different from the form of array .
I feel that the only difficulty in implementing queues may be the flexible use of two-tier structures , Others are very similar to the implementation of stack , You can define the framework of queues , Then realize it by yourself , This is very helpful for mastering the structure , The understanding of queues will also deepen .
Okay , This blog is over , Next, after doing a few questions, you will start learning trees , A new journey , Let's learn data structure , Come on together .
边栏推荐
- 基于算力驱动、数据与功能协同的分布式动态(协同)渲染/功能运行时
- 面试题目总结(1) https中间人攻击,ConcurrentHashMap的原理 ,serialVersionUID常量,redis单线程,
- Use lambda function URL + cloudfront to realize S3 image back to source
- 奔涌而来的数字化浪潮,将怎样颠覆未来?
- 开源实习经验分享:openEuler软件包加固测试
- Blind box NFT digital collection platform system development (build source code)
- Introduction to topological sorting
- SAP 智能机器人流程自动化(iRPA)解决方案分享
- el-form-item 正则验证
- 单工,半双工,全双工区别以及TDD和FDD区别
猜你喜欢

Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise

逻辑是个好东西

A new book by teacher Zhang Yujin of Tsinghua University: 2D vision system and image technology (five copies will be sent at the end of the article)

玩转MongoDB—搭建MongoDB集群

Learning to use livedata and ViewModel will make it easier for you to write business

Liu Dui (fire line safety) - risk discovery in cloudy environment

The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise

Etcd 概要 机制 和使用场景

原来程序员搞私活这么赚钱?真的太香了

Computer network interview knowledge points
随机推荐
当你真的学会DataBinding后,你会发现“这玩意真香”!
El form item regular verification
佩服,阿里女程序卧底 500 多个黑产群……
洞态在某互联⽹⾦融科技企业的最佳落地实践
Kongsong (Xintong Institute) - cloud security capacity building and trend in the digital era
SWT/ANR问题--如何捕获性能的trace
1.8新特性-List
开源者的自我修养|为 ShardingSphere 贡献了千万行代码的程序员,后来当了 CEO
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
[flask] flask starts and implements a minimal application based on flask
A new book by teacher Zhang Yujin of Tsinghua University: 2D vision system and image technology (five copies will be sent at the end of the article)
Spark source code reading outline
Machine learning summary (I): linear regression, ridge regression, Lasso regression
6年技术迭代,阿里全球化出海&合规的挑战和探索
Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
arthas使用
Blind box NFT digital collection platform system development (build source code)
二传感器尺寸「建议收藏」
【NLP】预训练模型——GPT1
Applet - applet chart Library (F2 chart Library)