当前位置:网站首页>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 .
边栏推荐
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- 编译原理——自上而下分析与递归下降分析构造(笔记)
- Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
- Why should Li Shufu personally take charge of building mobile phones?
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- Interview assault 63: how to remove duplication in MySQL?
- FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
- High precision operation
- Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
- node の SQLite
猜你喜欢
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
Awk command exercise
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
Spark accumulator and broadcast variables and beginners of sparksql
IP, subnet mask, gateway, default gateway
10 advanced concepts that must be understood in learning SQL
Windows连接Linux上安装的Redis
随机推荐
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
趣-关于undefined的问题
Pytest learning ----- detailed explanation of the request for interface automation test
分布式不来点网关都说不过去
[introduction to MySQL] the first sentence · first time in the "database" Mainland
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
【Swoole系列2.1】先把Swoole跑起来
2019 Alibaba cluster dataset Usage Summary
开源与安全的“冰与火之歌”
Interview shock 62: what are the precautions for group by?
F200 - UAV equipped with domestic open source flight control system based on Model Design
MSF横向之MSF端口转发+路由表+SOCKS5+proxychains
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
OliveTin能在网页上安全运行shell命令(上)
QT中Model-View-Delegate委托代理机制用法介绍
RB157-ASEMI整流桥RB157
Introduction to the usage of model view delegate principal-agent mechanism in QT