当前位置:网站首页>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;
}
边栏推荐
- Ansible
- [UTCTF2020]file header
- [experience sharing] how to expand the cloud service icon for Visio
- 芯片资料 网站 易特创芯
- 这5个摸鱼神器太火了!程序员:知道了快删!
- Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
- [OBS] win capture requires winrt
- 2022焊工(初级)判断题及在线模拟考试
- Hands on deep learning (IV) -- convolutional neural network CNN
- C language communication travel card background system
猜你喜欢
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
json 数据展平pd.json_normalize
Figure out the working principle of gpt3
Common method signatures and meanings of Iterable, collection and list
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
Problem solving: unable to connect to redis
Hands on deep learning (IV) -- convolutional neural network CNN
Use and analysis of dot function in numpy
随机推荐
[unity] several ideas about circular motion of objects
[UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
Regular e-commerce problems part1
What is the interval in gatk4??
Jenkins remote build project timeout problem
[performance pressure test] how to do a good job of performance pressure test?
【p2p】本地抓包
Weibo publishing cases
Route jump in wechat applet
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
Common validation comments
PHP exports millions of data
SQL优化的魅力!从 30248s 到 0.001s
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
C语言队列
Detailed explanation of Kalman filter for motion state estimation
[P2P] local packet capturing
[webrtc] m98 Screen and Window Collection
Qt学习28 主窗口中的工具栏