当前位置:网站首页>Implementation of queue
Implementation of queue
2022-07-06 18:05:00 【qzt__ l0ve】
Catalog
1. The concept and structure of queue
// Get the number of valid elements in the queue
1. 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 , Queue has first in first out
FIFO(First In First Out) Queue entry : The end of the insertion is called the end of the queue
Outgoing queue : The end of the delete operation is called the queue head

2. Implementation of queues
Queues can also be implemented in the structure of arrays and linked lists , It is better to use the structure of linked list , Because if you use the structure of an array , Out of the queue, out of the data on the array header , It will be less efficient .
typedef int QDataType;
// The chain structure : Represents a queue
typedef struct QListNode
{
struct QListNode* _pNext;
QDataType _data;
}QNode;
// The structure of the queue
typedef struct Queue
{
QNode* _front;
QNode* _rear;
}Queue;
// Initialize queue
void QueueInit(Queue* q);
// Tail in queue
void QueuePush(Queue* q, QDataType data);
// Team leader out of line
void QueuePop(Queue* q);
// Get queue header elements
QDataType QueueFront(Queue* q);
// Get queue tail element
QDataType QueueBack(Queue* q);
// Get the number of valid elements in the queue
int QueueSize(Queue* q);
// Check if the queue is empty , If NULL, return non-zero result , If not null, return 0
int QueueEmpty(Queue* q);
// Destroy queue
void QueueDestroy(Queue* q);Initialize queue
void QueueInit(Queue* q);// No matter what structure , We all need to initialize first .
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}Tail in queue
void QueuePush(Queue* q, QDataType data);// Because the queue is FIFO , So it can only be inserted from the tail
void QueuePush(Queue* pq, QDataType x)// Insert , Insert... At the end of the line ; fifo
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));// Every time you insert a data, you open up a space
if (newnode == NULL)// Judge whether the development fails
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)// When no data is inserted for the first time
{
pq->head = pq->tail = newnode;
}
else// With data
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}// Team leader out of line
void QueuePop(Queue* q);// Outgoing queue , The first one is the data of the team leader , Make sure it's first in, first out .
void QueuePop(Queue* pq)// It is equivalent to deleting the team header data
{
assert(pq);
assert(!QueueEmpty(pq));// Determines if the queue is empty
// A node
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
// Multiple nodes
else
{
Queue* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}// Get queue header elements
QDataType QueueFront(Queue* q);// First judge the capacity of the queue , Then return to the team head element
QDataType QueueFront(Queue* pq)// Get the correct header data
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}// Get queue tail element
QDataType QueueBack(Queue* q);// First judge the queue capacity , Then return the tail data
QDataType QueueBack(Queue* pq)// Take the tail data
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}// Get the number of valid elements in the queue
int QueueSize(Queue* q);// Create a current pointer , Go back one by one ,size Count the quantity for size .
int QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;// Create a pointer to the current element
int size = 0;// Used to count the number of elements
while (cur)
{
++size;
cur = cur->next;
}
return size;
}// Check if the queue is empty , If NULL, return non-zero result , If not null, return 0
int QueueEmpty(Queue* q);// here return It's to judge first pq->head Is it equal to empty , Return to true later , Return false before waiting .
bool QueueEmpty(Queue* pq)// Judge capacity ;
{
assert(pq);
return pq->head == NULL;
}// Destroy queue
void QueueDestroy(Queue* q); Because we have opened up space , So we have to release one by one in the end .
void Queuestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)// Release space one by one
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}Expand :
In practice, we sometimes use a queue called circular queue . For example, when the operating system course explains the producer consumer model, you can use the circular queue . Ring queues can be implemented using arrays , You can also use a circular linked list to achieve .


边栏推荐
- Kill -9 system call used by PID to kill process
- FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
- Shell input a string of numbers to determine whether it is a mobile phone number
- How to output special symbols in shell
- 微信为什么使用 SQLite 保存聊天记录?
- declval(指导函数返回值范例)
- Jielizhi obtains the currently used dial information [chapter]
- Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- 78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
- 第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
猜你喜欢
![Jerry's access to additional information on the dial [article]](/img/a1/28b2a5f7c16cbcde1625a796f0d188.jpg)
Jerry's access to additional information on the dial [article]

The easycvr authorization expiration page cannot be logged in. How to solve it?

node の SQLite

中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障

Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day

Introduction to the usage of model view delegate principal-agent mechanism in QT

std::true_type和std::false_type

Olivetin can safely run shell commands on Web pages (Part 1)

递归的方式

Declval (example of return value of guidance function)
随机推荐
How to output special symbols in shell
面向程序员的精品开源字体
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
Spark accumulator and broadcast variables and beginners of sparksql
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
传统家装有落差,VR全景家装让你体验新房落成效果
SQL statement optimization, order by desc speed optimization
C语言通过指针交换两个数
分布式不来点网关都说不过去
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
Jerry's watch reading setting status [chapter]
Jerry's watch reads the file through the file name [chapter]
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
Wechat applet obtains mobile number
Four processes of program operation
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
Getting started with pytest ----- allow generate report
Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
F200——搭载基于模型设计的国产开源飞控系统无人机