当前位置:网站首页>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;
}
边栏推荐
- Numbers that appear only once
- PHP exports millions of data
- Leanote private cloud note building
- 【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
- Most elements
- 2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
- [P2P] local packet capturing
- [GUET-CTF2019]虚假的压缩包
- Operation suggestions for today's spot Silver
- Force buckle 145 Binary Tree Postorder Traversal
猜你喜欢
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
2022年茶艺师(中级)考试试题及模拟考试
Leetcode 40: combined sum II
2022制冷与空调设备运行操作复训题库及答案
Linux server development, detailed explanation of redis related commands and their principles
Iterable、Collection、List 的常见方法签名以及含义
Problem solving: unable to connect to redis
2022 tea master (intermediate) examination questions and mock examination
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
PHP exports millions of data
随机推荐
Hands on deep learning (IV) -- convolutional neural network CNN
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
What are the positions of communication equipment manufacturers?
[Stanford Jiwang cs144 project] lab3: tcpsender
Zhilian + AV, AITO asked M7 to do more than ideal one
C language flight booking system
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
Common method signatures and meanings of Iterable, collection and list
自定义类加载器加载网络Class
IO stream file
MySQL multi column index (composite index) features and usage scenarios
[VHDL parallel statement execution]
Linux server development, redis source code storage principle and data model
[OBS] win capture requires winrt
C语言航班订票系统
[UTCTF2020]file header
Cnopendata geographical distribution data of religious places in China
Linux server development, detailed explanation of redis related commands and their principles
开源生态|打造活力开源社区,共建开源新生态!
Leetcode 40: combined sum II