当前位置:网站首页>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 !!!
边栏推荐
- 纯碱是做什么的?
- How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
- Mipi interface, DVP interface and CSI interface of camera
- An article was opened to test the real situation of outsourcing companies
- U-Boot初始化及工作流程分析
- U-boot initialization and workflow analysis
- 【idea】Could not autowire. No beans of xxx type found
- M2dgr slam data set of multi-source and multi scene ground robot
- C learning notes
- ImportError: No module named ‘Tkinter‘
猜你喜欢
Ros2 topic (VIII)
Microservice registry Nacos introduction
Negative number storage and type conversion in programs
[idea] efficient plug-in save actions to improve your work efficiency
Rough notes of C language (1)
网易To B,柔外刚中
【Node】nvm 版本管理工具
window navicat连接阿里云服务器mysql步骤及常见问题
[node] NVM version management tool
Jenkins reported an error. Illegal character: '\ufeff'. Class, interface or enum are required
随机推荐
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
Unity ugui how to match and transform coordinates between different UI panels or uis
Application of MATLAB in Linear Algebra (4): similar matrix and quadratic form
Hdu1232 unimpeded project (and collection)
Docker installs MySQL and uses Navicat to connect
window navicat连接阿里云服务器mysql步骤及常见问题
611. 有效三角形的个数
PostMessage communication
arcgis_ spatialjoin
C learning notes
[software testing] 04 -- software testing and software development
1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
What if the DataGrid cannot see the table after connecting to the database
【Node】npm、yarn、pnpm 区别
An article was opened to test the real situation of outsourcing companies
NPM and package common commands
[software testing] 05 -- principles of software testing
Install deeptools in CONDA mode
Course learning accumulation ppt
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"