当前位置:网站首页>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 !!!
边栏推荐
- Reading literature sorting 20220104
- When jupyter notebook is encountered, erroe appears in the name and is not output after running, but an empty line of code is added downward, and [] is empty
- Brief description of inux camera (Mipi interface)
- [idea] efficient plug-in save actions to improve your work efficiency
- Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
- Simple operation with independent keys (hey, a little fancy) (keil5)
- 【Node】nvm 版本管理工具
- Ros2 - function package (VI)
- 并发编程 — 死锁排查及处理
- 2022.06.27_每日一题
猜你喜欢
M2DGR 多源多场景 地面机器人SLAM数据集
【无标题】
SD_ CMD_ SEND_ SHIFT_ REGISTER
Ros2 - ros2 vs. ros1 (II)
Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory
Concurrent programming - deadlock troubleshooting and handling
U-Boot初始化及工作流程分析
C learning notes
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
Netease to B, soft outside, hard in
随机推荐
PowerManagerService(一)— 初始化
Basic operation of external interrupt (keil5)
Matlab在线性代数中的应用(四):相似矩阵及二次型
(top) pretty girl binary color code portal
What does soda ash do?
[software testing] 04 -- software testing and software development
Three body goal management notes
IPage can display data normally, but total is always equal to 0
Clickhouse database installation deployment and remote IP access
Matrix keyboard scan (keil5)
How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
M2DGR 多源多场景 地面机器人SLAM数据集
Hdu1231 maximum continuous subsequence (divide and conquer or dynamic gauge or double pointer)
Concurrent programming - how to interrupt / stop a running thread?
[node] differences among NPM, yarn and pnpm
U-boot initialization and workflow analysis
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
一文揭开,测试外包公司的真实情况
[untitled]
Chapter 2: try to implement a simple bean container