当前位置:网站首页>Introduction and implementation of queue (detailed explanation)
Introduction and implementation of queue (detailed explanation)
2022-07-28 16:31:00 【World_ living】
Catalog
One 、 What is a queue
queue (queue) It is only allowed to insert at one end , A linear table with deletion at the other end
Two 、 The structure of the queue
A queue is a first in, first out (First In First Out) The linear table , abbreviation FIFO.
Team head : The end allowed to be deleted is called the team leader
A party : The end allowed to be inserted is called the end of the queue
Suppose the queue is q=(A1,A2,A3·····An), that A1 For the team leader element ,An End of team element . We deleted it from A1 Start , Insert from An Start .
Pictured :
3、 ... and 、 Implementation of queues
I implemented it in a chain storage structure , You can also use a linear storage structure .
First create the queue structure
head: When creating nodes , We need to head The pointer points to the head of the team
tail: need head The pointer points to the end of the queue
next: Pointer to the domain , Used to connect nodes
data: data
typedef int QDataType;
struct queue
{
struct QListNode*head;
struct QListNode*tail;
};
struct QListNode
{
struct QlistNode*next;
QDataType data;
};
1. Initialize queue operation
First initialize the queue .
void InitQueue(struct queue*qst)
{
qst->head = NULL;
qst->tail = NULL;
}
2. Queue exists , Then destroy the queue
First of all, make an assertion qst
When destroying the queue, we must release the dynamically opened space , Avoid memory leaks .
void DestroyQueue(struct queue*qst)
{
assert(qst);
struct QListNode*cur = qst->head;
while (cur)
{
struct QListNode*next = cur->next;
free(cur);
cur = next;
}
qst->head = NULL;
qst->tail = NULL;
}
3. The queue to empty
Without releasing space , Put... Directly tail Point to the team leader .
void ClearQueue(struct queue*qst)
{
assert(qst);
assert(!EmptyQueue(qst));
qst->tail = qst->head;
}
4. Determines if the queue is empty
Determine whether there are nodes .
QDataType EmptyQueue(struct queue*qst)
{
assert(qst);
return qst->head == NULL;
}
5. Returns the queue header element
QDataType GetHead(struct queue*qst)
{
assert(qst);
assert(!EmptyQueue(qst));// Asserts whether the queue is empty
return qst->head->data;// Returns the header element
}
6. Returns the number of queue elements
Count the number of elements by counting
QDataType SizeQueue(struct queue*qst)// The number of elements in the queue
{
assert(qst);
assert(!EmptyQueue(qst));
int count = 0;
struct QListNode*cur = qst->head;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
7. Insert
Let's say first , Then judge whether there is a node , And connect them .
Open up space next To be empty
void PuchQueue(struct queue*qst, QDataType x)
{
assert(qst);
struct QListNode*newhead =(struct QListNode*) malloc(sizeof(struct QListNode));
if (newhead == NULL)
{
printf("malloc fail");
exit(-1);
}
newhead->next = NULL;
newhead->data = x;
if (qst->head == NULL)// There was no node in the queue
{
qst->head = qst->tail = newhead;// The head and tail of the team point directly to the new node
}
else
{
struct QListNode*cur = qst->head;
cur->next = newhead;// Connecting nodes
qst->tail = newhead;// The end of the queue points to the new node , Change the tail of the team
}
}
8. Delete
Delete an element , Namely free Drop this node , The head of the queue will also change .
void PopQueue(struct queue*qst)
{
assert(qst);
assert(!EmptyQueue(qst));
if (qst->head->next == NULL)// The queue has only one node
{
free(qst->head);
qst->head = NULL;
qst->tail = NULL;
}
else// The queue has multiple nodes
{
struct QListNode*cur = qst->head;
cur = cur->next;
free(qst->head);
qst->head = cur;
}
}
边栏推荐
- Automatic conversion and cast
- [Multisim Simulation] LM339 zero crossing circuit simulation
- Darknet training yolov4 record
- 一小时内学会Abaqus脚本编程秘籍
- 2021-10-21 notes
- Detectron2 installation and testing
- Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
- 1. Simple command line connection to database
- QT打包
- MySQL view event status statements and modification methods
猜你喜欢

Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation

使用js直传oss阿里云存储文件,解决大文件上传服务器限制

I'll show you a little chat! Summary of single merchant function modules

Redis series 4: sentinel (sentinel mode) with high availability

ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境

LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)

为什么学编程的人大多数都去了深圳和北京?

ANSA二次开发 - 抽中面的两种方法

MySQL view event status statements and modification methods

在abaqus中使用PyQt设计GUI
随机推荐
视频号找到金钥匙,抖音模仿后来人
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
c语言编程当中两个!!的作用
LwIP development | socket | TCP | client
Image semantic segmentation practice: tensorflow deeplobv3+ train your own dataset
About the web docking pin printer, lodop uses
排序2-冒泡排序与快速排序(递归加非递归讲解)
8051 series MCU firmware upgrade IAP
I'll show you a little chat! Summary of single merchant function modules
Dynamic programming -- digital statistics DP
Headline article_ signature
PHP image upload
Pop up layer prompt in the background
LwIP development | socket | DNS domain name resolution
PHP mb_substr 中文乱码
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
The video Number finds the golden key, and Tiktok imitates the latecomers
Redis系列4:高可用之Sentinel(哨兵模式)
IT远程运维是什么意思?远程运维软件哪个好?