当前位置:网站首页>I implement queue with C I
I implement queue with C I
2022-07-05 07:22:00 【MT_ one hundred and twenty-five】
Catalog
Two 、 The structure of the queue :
3、 ... and 、 Code implementation
One 、 Nature of queues :
Last time we learned stack , Understand that the way the stack stores and releases data is : First in, then out
The queue is the opposite , The queue is : fifo , last in last out .
Two 、 The structure of the queue :
Multiple linked list nodes + Head to tail pointer ( Linked list queue )
List nodes Responsible for storing data ; Head node Responsible for locating advanced starting data , Convenient first out ;
Tail node Be responsible for recording tail data , It is convenient to determine the current status of the queue .

3、 ... and 、 Code implementation
The header file :
It is convenient to call uniformly , Define the head and tail pointers as a structure .
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int Quetype; // Define the data type of the queue
typedef struct QueNode // Define data nodes
{
struct QueNode* Next;
Quetype data;
}QueNode;
typedef struct Quetail
{
struct QueNode* head; // Define the head and tail pointer
struct QueNode* tail;
}Quetail;
void Que_Init(Quetail* pq); // Initialization of the queue
void Que_Destory(Quetail* pq); // Destruction of queues
void Que_push(Quetail* pq ,Quetype data); // insert data
void Que_pop(Quetail* pq); // Delete data
bool Que_Empty(Quetail* pq); // Determines if the queue is empty
int Que_size(Quetail* pq); // Count the queue length
int Que_front(Quetail* pq); // Find the header data of the queue Function function :
1. Initialization of the queue :
Set the head and tail pointer to NULL Convenient for subsequent use .
void Que_Init(Quetail* pq) // Initialization of the queue
{
assert(pq);
pq->head = pq->tail = NULL;
}
2. insert data :
Create a linked list node >> Import data >> The header pointer points to the header node >> The tail pointer points to the tail node
// insert data
void Que_push(Quetail* pq,Quetype data)
{
assert(pq);
QueNode* NewNode = (QueNode*)malloc(sizeof(QueNode));// Create nodes
if (NewNode == NULL)
{
printf("Que_push->malloc");
exit(-1);
}
NewNode->Next = NULL;
NewNode->data = data;
if (pq->head == NULL) // Determine whether to create a header node
{
pq->head = NewNode; // Update head pointer
}
else
{
pq->tail->Next = NewNode; // Not a header node , It is normally linked after the tail node
}
pq->tail = NewNode; // Update tail pointer
}3. Delete data :
Record the next position of the header node >> Judge whether it is the last data >> Update head pointer
Minutiae : If there are more nodes left in the queue , After deleting the head node , The tail pointer always points to the tail node , No need to change ;
But if there is only one data node left , After deletion, you need to set the tail pointer to null .

// Delete data
void Que_pop(Quetail* pq)
{
assert(pq);
assert(!Que_Empty(pq)); // Determines if the queue is empty
QueNode* Next = pq->head->Next; // Record deleted data
if (pq->head == pq->tail) // Determine whether it is the last data
{
free(pq->head);
pq->tail = NULL; // Update tail pointer
}
else
{
free(pq->head);
}
pq->head = Next; // Update head pointer
}4. Determine whether the list is empty :
use bool As return type

// Determines if the queue is empty
bool Que_Empty(Quetail* pq)
{
assert(pq);
return pq->head == NULL;
}5. Find the header data of the queue :
Determines if the queue is empty >> Return header data
// Find the header data of the queue
Quetype Que_front(Quetail* pq)
{
assert(pq);
assert(!Que_Empty(pq)); // Determines if the queue is empty
return pq->head->data; // Return header data
}6. system Count the length of the queue :
It is to count the number of linked list nodes
int Que_size(Quetail* pq)
{
assert(pq);
int size;
QueNode* pphead = pq->head;
for (size = 0; pphead; pphead = pphead->Next, size++);
return size;
}7. Destruction of queues :
Delete data in turn >> Release the application space
Minutiae : Here you can reuse : Determines if the queue is empty 、 Delete data
void Que_Destory(Quetail* pq)
{
for (; !Que_Empty(pq); ) // Determines if the queue is empty
{
Que_pop(pq); // Delete data
}
}Thank you for your support !!!

边栏推荐
- 【idea】Could not autowire. No beans of xxx type found
- Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换
- PHY drive commissioning - phy controller drive (II)
- Unforgettable summary of 2021
- M2dgr slam data set of multi-source and multi scene ground robot
- DelayQueue延迟队列的使用和场景
- Shadowless cloud desktop - online computer
- PostMessage communication
- Ros2 - first acquaintance with ros2 (I)
- [node] differences among NPM, yarn and pnpm
猜你喜欢

Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)

PostMessage communication
![[software testing] 04 -- software testing and software development](/img/bd/49bba7ee455ce59e726a2fdeafc7c3.jpg)
[software testing] 04 -- software testing and software development

Docker installs MySQL and uses Navicat to connect

SOC_ SD_ CMD_ FSM

HDU1231 最大连续子序列(分治or动规or双指针)

Microservice registry Nacos introduction

一文揭开,测试外包公司的真实情况

How to delete the virus of inserting USB flash disk copy of shortcut to

U-boot initialization and workflow analysis
随机推荐
CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
[node] differences among NPM, yarn and pnpm
网易To B,柔外刚中
[vscode] recommended plug-ins
Three body goal management notes
Simple operation of running water lamp (keil5)
并查集理论讲解和代码实现
CADD课程学习(5)-- 构建靶点已知的化合结构(ChemDraw)
HDU1232 畅通工程(并查集)
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
Shadowless cloud desktop - online computer
[OBS] x264 Code: "buffer_size“
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
ImportError: No module named ‘Tkinter‘
[tf1] save and load parameters
arcgis_ spatialjoin
window navicat连接阿里云服务器mysql步骤及常见问题
How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
IPage能正常显示数据,但是total一直等于0
公安基础知识--fb