当前位置:网站首页>C language queue
C language queue
2022-07-07 07:57:00 【pythoncjavac++】
Catalog
Determines if the queue is empty
Queue related concepts
First of all, the biggest feature of the queue is “ fifo ”, Only insert... At one end , Delete on the other end , The inserted end is the tail of the team , The deleted end is the head of the team .
Queue creation
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
}Queue;Here we will create two structures , Because here we need to record his head and tail , This makes it easy to insert and delete .
Initialization of the queue
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}Queue insertion
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
pq->tail = pq->head = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}Queue deletion
void QueuePoq(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* cur = pq->head->next;
free(pq->head);
pq->head = cur;
}
}Take the head of the queue
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}Take the end of the queue
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}Number of queue elements
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QNode* cur = pq->head;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}Determines if the queue is empty
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}Destroy queue
void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* aq = cur->next;
free(cur);
cur = aq;
}
pq->head = pq->tail = NULL;
}边栏推荐
- Linux server development, detailed explanation of redis related commands and their principles
- Visualization Document Feb 12 16:42
- Li Kou interview question 04.01 Path between nodes
- Value sequence (subsequence contribution problem)
- Why should we understand the trend of spot gold?
- json 数据展平pd.json_normalize
- Common validation comments
- Route jump in wechat applet
- 有 Docker 谁还在自己本地安装 Mysql ?
- C语言二叉树与建堆
猜你喜欢

Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)

【Unity】物体做圆周运动的几个思路

2022茶艺师(初级)考试题模拟考试题库及在线模拟考试

Li Kou interview question 04.01 Path between nodes

Visualization Document Feb 12 16:42

Common validation comments
![[UTCTF2020]file header](/img/e3/818e2d531a06ab90de189055f634ad.png)
[UTCTF2020]file header

【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)

Qt学习28 主窗口中的工具栏

【斯坦福计网CS144项目】Lab4: TCPConnection
随机推荐
Resource create package method
Solve could not find or load the QT platform plugin "xcb" in "
2022 tea master (intermediate) examination questions and mock examination
Explore Cassandra's decentralized distributed architecture
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
Live broadcast platform source code, foldable menu bar
Linux server development, MySQL process control statement
nacos
[P2P] local packet capturing
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
Linux server development, MySQL cache strategy
【obs】win-capture需要winrt
Button wizard collection learning - mineral medicine collection and running map
[experience sharing] how to expand the cloud service icon for Visio
[webrtc] m98 Screen and Window Collection
Leanote private cloud note building
芯片 设计资料下载
QT learning 28 toolbar in the main window
buuctf misc USB
Kbu1510-asemi power supply special 15A rectifier bridge kbu1510