当前位置:网站首页>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 :
边栏推荐
- Detailed explanation of coordinate tracking of TF2 operation in ROS (example + code)
- When SQL server2019 is installed, the next step cannot be performed. How to solve this problem?
- Lambda function perfect use guide
- Embedded gd32 code read protection
- 1.3-1.9 summary
- libprint2
- paddlepaddl 28 支持任意维度数据的梯度平衡机制GHM Loss的实现(支持ignore_index、class_weight,支持反向传播训练,支持多分类)
- Esp8266 firmware upgrade method (esp8266-01s module)
- Detailed explanation of TF2 command line debugging tool in ROS (parsing + code example + execution logic)
- Map to sort
猜你喜欢

Kotlin插件 kotlin-android-extensions

Detailed explanation of multi coordinate transformation in ROS (example + code)

RT thread studio learning (IX) TF Card File System

Pyhon的第五天

sql——课程实验考查

Elegantly spliced XML

Thoroughly understand the "rotation matrix / Euler angle / quaternion" and let you experience the beauty of three-dimensional rotation

SQL -- course experiment examination

Imx6q pwm3 modify duty cycle

LVDS drive adapter
随机推荐
Paddepaddl 28 supports the implementation of GHM loss, a gradient balancing mechanism for arbitrary dimensional data (supports ignore\u index, class\u weight, back propagation training, and multi clas
企业微信官方 加解密库 PHP7版本报错 mcrypt_module_open 未定义方法 并且被PHP抛弃 解决方法使用 openssl解决
Category 7
New knowledge: monkey improved app crawler
五、EL 表达式& JSTL 标签库
RT thread studio learning (VIII) connecting Alibaba cloud IOT with esp8266
TypeScript基础知识全集
Curry carries the fourth game of the warriors against the Celtics
8086/8088 instruction execution pipeline disconnection reason
Kotlin插件 kotlin-android-extensions
Detailed explanation of coordinate tracking of TF2 operation in ROS (example + code)
【图像去噪】基于偏微分方程(PDE)实现图像去噪附matlab代码
Imx6q pwm3 modify duty cycle
报表工具的二次革命
Go common usage
[image denoising] image denoising based on nonlocal Euclidean median (nlem) with matlab code
Node: cannot open /node: access denied
[image detection] SAR image change detection based on depth difference and pcanet with matlab code
Lambda function perfect use guide
5 lines of code identify various verification codes