当前位置:网站首页>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;
}
边栏推荐
- 探索Cassandra的去中心化分布式架构
- 【斯坦福计网CS144项目】Lab3: TCPSender
- A bit of knowledge - about Apple Certified MFI
- Iterable、Collection、List 的常见方法签名以及含义
- dash plotly
- 这5个摸鱼神器太火了!程序员:知道了快删!
- 2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
- Linux server development, detailed explanation of redis related commands and their principles
- padavan手动安装php
- Problem solving: unable to connect to redis
猜你喜欢
【webrtc】m98 screen和window采集
Detailed explanation of Kalman filter for motion state estimation
[GUET-CTF2019]虚假的压缩包
今日现货白银操作建议
Leetcode 40: combined sum II
Numbers that appear only once
What are the positions of communication equipment manufacturers?
buuctf misc USB
[SUCTF 2019]Game
Li Kou interview question 04.01 Path between nodes
随机推荐
Linux server development, redis source code storage principle and data model
Qt学习28 主窗口中的工具栏
[SUCTF 2019]Game
Linux server development, MySQL cache strategy
芯片 设计资料下载
json 数据展平pd.json_normalize
Visualization Document Feb 12 16:42
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
Solve could not find or load the QT platform plugin "xcb" in "
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
Rust versus go (which is my preferred language?)
2022焊工(初级)判断题及在线模拟考试
buuctf misc USB
Problem solving: unable to connect to redis
mysql多列索引(组合索引)特点和使用场景
Cnopendata American Golden Globe Award winning data
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
The charm of SQL optimization! From 30248s to 0.001s
Linux server development, MySQL process control statement