当前位置:网站首页>C language queue implementation
C language queue implementation
2022-06-12 07:16:00 【Sour plum is not sour】
The following explanation is one-sided , But it is easy for beginners to understand the code :
1. Queues are first in first out data structures
2. The queue structure has only two pointers, the head and the tail , Does not contain stored data . It is only responsible for recording the beginning and end of the queue , It is convenient to understand the implementation of queues .
3. The queue member structure contains a variable for storing data , And a pointer to the next member , Pointer to the next queue member .
4. To join the queue, change the end of queue pointer of the queue structure to the next queue member , At the same time, the pointer of the queue member structure should point to the new member .
5. The pointer of the queue structure is used to record that the head and tail of the entire queue point to . The pointer in the queue member structure is used to record where the new member is .
Code :
#include <stdio.h>// fifo
#include <stdlib.h>
#define TURE 1
#define FALSE 0
struct STU
{
int num;
struct STU *next;
};
// queue yes from Tail insert element , Delete element from header
struct QUEUE
{
struct STU * front;
struct STU * rear;
};
struct QUEUE * create_queue(void);// Create a queue
int pd_queue(struct QUEUE *p);// Judge the queue
void insert_queue(struct QUEUE *p);// Insert queue
void Tra_queue(struct QUEUE *p);// Output
void delete_queue(struct QUEUE *p);// Delete
int main(void)
{
struct QUEUE *p;
p = create_queue(); // Create a queue
insert_queue(p); // Insert
insert_queue(p);// Insert
insert_queue(p);// Insert
Tra_queue(p);// Output
delete_queue(p);// Delete
Tra_queue(p);// Output
insert_queue(p);// Insert
insert_queue(p);// Net income
Tra_queue(p);// Output
delete_queue(p);// Delete
delete_queue(p);// Delete
delete_queue(p);// Delete
delete_queue(p);// Delete
Tra_queue(p);// Output
return 0;
}
// Create a queue
struct QUEUE * create_queue(void)
{
struct QUEUE *p;
p = (struct QUEUE *)malloc(sizeof(struct QUEUE));
if(p == NULL)
{
printf(" Space application failed , The system is shutting down ~\n");
return 0;
}
p->front = NULL; // Header
p->rear = NULL; // Tail
printf(" Queue created successfully ~\n");
return p;
}
// Determines if the queue is empty :
int pd_queue(struct QUEUE *p)
{
if(p->rear == NULL)
{
printf("\t The queue is empty ~\n");
return FALSE;
}
printf("\t The queue is not empty ~\n");
return TURE;
}
// The team
void insert_queue(struct QUEUE *p)
{
struct STU *New;
New = (struct STU *)malloc(sizeof(struct STU));
if(New == NULL)
{
printf(" Space application identification , The system is exiting !\n");
return;
}
if(pd_queue(p) == FALSE)
{
p->front = p->rear = New;
}
else
{
p->rear->next = New; // The team
p->rear = New; // Change the direction of the tail of the team
}
printf("\t The data of joining the team is :");
scanf("%d", &New->num);
New->next = NULL;
}
void Tra_queue(struct QUEUE *p)
{
struct STU *r;
r = p->front;
if(pd_queue(p) == FALSE)
{
printf(" Space traversal failed !\n");
return ;
}
do
{
printf(" The element in the queue is :%d\n", r->num);
r = r->next;
}while(r != NULL);
}
void delete_queue(struct QUEUE *p)
{
struct STU *r;
if(pd_queue(p) == FALSE)
{
printf(" Unable to complete the delete operation !\n");
return ;
}
r = p->front;
if(p->front == p->rear)
{
p->front = p->rear = NULL;
}
else
{
p->front = r->next;
}
printf("%d Out of the team !\n", r->num);
free(r);
}
function :
Queue created successfully ~
The queue is empty ~
The data of joining the team is :1
The queue is not empty ~
The data of joining the team is :2
The queue is not empty ~
The data of joining the team is :3
The queue is not empty ~
The element in the queue is :1
The element in the queue is :2
The element in the queue is :3
The queue is not empty ~
1 Out of the team !
The queue is not empty ~
The element in the queue is :2
The element in the queue is :3
The queue is not empty ~
The data of joining the team is :
边栏推荐
- RT thread studio learning (x) mpu9250
- Pyhon的第五天
- Expansion of D @nogc
- Stm32cubemx learning (I) USB HID bidirectional communication
- Pyhon的第四天
- Decryption game of private protocol: from secret text to plaintext
- 5 statement
- 2022年G3锅炉水处理复训题库及答案
- 8086/8088 instruction execution pipeline disconnection reason
- How to update kubernetes certificates
猜你喜欢

Descscheduler secondary scheduling makes kubernetes load more balanced

AI狂想|来这场大会,一起盘盘 AI 的新工具!

The most understandable explanation of coordinate transformation (push to + diagram)

3 strings, containers, and arrays

Freshmen are worried about whether to get a low salary of more than 10000 yuan from Huawei or a high salary of more than 20000 yuan from the Internet

sql——课程实验考查

5 ROS simulation modeling (4-navigation navigation simulation)

LED lighting experiment with simulation software proteus

2022电工(初级)考试题库及模拟考试

Imx6q PWM drive
随机推荐
Imx6q pwm3 modify duty cycle
LED lighting experiment with simulation software proteus
【图像去噪】基于高斯滤波、均值滤波、中值滤波、双边滤波四种滤波实现椒盐噪声图像去噪附matlab代码
LVDS drive adapter
推荐17个提升开发效率的“轮子”
SQL -- course experiment examination
Expansion of D @nogc
Unable to load bean of class marked with @configuration
AI狂想|来这场大会,一起盘盘 AI 的新工具!
公众号也能带货?
New knowledge: monkey improved app crawler
2022电工(初级)考试题库及模拟考试
I met 15 people recently and found that I couldn't answer the basic question of this test
【WAX链游】发布一个免费开源的Alien Worlds【外星世界】脚本TLM
postman拼接替换参数循环调用接口
leetcode. 39 --- combined sum
Source code learning - [FreeRTOS] privileged_ Understanding of function meaning
右击文件转圈卡住、刷新、白屏、闪退、桌面崩溃的通用解决方法
D cannot use a non CTFE pointer
1.3-1.9 summary