当前位置:网站首页>C language version: establishment and basic operation of circular queue
C language version: establishment and basic operation of circular queue
2022-04-22 05:28:00 【Li Feiyu_ smile】
come from C Language version data structure : Introduce the establishment of circular queue , The team , Out of the team , Please, captain , Take the team head element operation .
List of articles
Circular queue
A sequential storage of queues , Why is it called circular queue ? First of all : prevent " False spillover ", This is caused by the operation of the team " The team is at the end of the line , Team leader ". There's probably room elsewhere , But it's wrong ( Transboundary ) Specific measures : The book is about using a mold to run (%) Resolved , When a ring appears , They don't show up " False spillover " The situation ofTips : The following is the main body of this article , The following cases can be used for reference
One 、 use CLion The feeling of writing code
CLion Is based on C/C++ A tool of , Xiaobian compiled this article by using this , Personal feeling : senior , so much trouble , But it has complete functions , such as : You introduce a library , But you didn't use the function in it , It will turn gray , Without color , More rigorous , It reminds you , Don't waste memory , Occupancy resources .
Two 、 Basic operations of queues
1. Build and initialize
The code is as follows ( Example ):
// Build queue
typedef struct Queue{
char sa[MAX]; // Store the data
int rear; // Tail pointer
int front; // Header pointer
}SQueue;
// Initialize queue
SQueue *initQueue()
{
SQueue *Q;
Q = (struct Queue*)malloc(sizeof(struct Queue)); // Apply for memory space
Q->front = 0;
Q->rear = 0;
return Q; // Returns the initialized queue
}
The core of initialization and establishment : The tail and head of the team point to 0 and To apply for space
2. The team
// The team
void push(SQueue *p, char x)
{
if((p->rear + 1) % MAX == p->front) // Judge the condition of fullness
{
printf(" Right full ");
exit(0);
}
p->sa[p->rear] = x; // The team is at the end of the line
p->rear = (p->rear + 1) % MAX;
// Using modular operation , Prevent false overflow
}
The core : It was originally the tail pointer +1 Can then , But to prevent overflow , Use modular operation , Form a ring .
3. Out of the team
// Out of the team
char pop(SQueue *p)
{
if(p->rear == p->front) // Judge the condition of team empty
{
printf(" In the air ");
exit(0);
}
char e = p->sa[p->front]; // The element used to store the dequeue
p->front = (p->front + 1) % MAX;
return e;
}
The core : Just like joining the team , Just judge the condition and return value one more step , Head to head , Header pointer +1,
Adopt modular operation .
4. Take the queue header element and queue length
// Take the length of the circular queue
int queuelength(SQueue *p)
{
return (p->rear - p->front + MAX) % MAX;
// effect : Add an absolute value , Remember it
}
// Take the queue header element of the circular queue
char GetHead(SQueue *p)
{
if(p->rear == p->front) // Judge the condition of null
{
exit(0);
}
char e;
e = p->sa[p->front]; // Out of the queue , Just don't move the tail pointer
return e;
}
Code implementation
//
// Created by xhh on 2021/6/30.
//
// Basic operation of circular queue
#include<stdio.h>
#include<stdlib.h>
#define MAX 5 // The maximum length of the circular queue
typedef struct Queue{
char sa[MAX]; // Store the data
int rear; // Pointer to a party
int front; // Team head pointer
}SQueue;
// Create a queue
SQueue *initQueue()
{
SQueue *Q;
Q = (struct Queue*)malloc(sizeof(struct Queue));
Q->front = 0;
Q->rear = 0;
return Q;
}
// The team
void push(SQueue *p, char x)
{
if((p->rear + 1) % MAX == p->front)// Using modular operation , Prevent false overflow
{
printf(" Right full ");
exit(0);
}
p->sa[p->rear] = x; // The team is at the end of the line
p->rear = (p->rear + 1) % MAX;
}
// Out of the team
char pop(SQueue *p)
{
if(p->rear == p->front)
{
printf(" In the air ");
exit(0);
}
char e = p->sa[p->front];
p->front = (p->front + 1) % MAX;
return e;
}
// Take the length of the circular queue
int queuelength(SQueue *p)
{
return (p->rear - p->front + MAX) % MAX;
}
// Take the queue header element of the circular queue
char GetHead(SQueue *p)
{
if(p->rear == p->front)
{
exit(0);
}
char e;
e = p->sa[p->front];
return e;
}
// Print function
void display(SQueue *p)
{
int index = p->front; // Team head pointer
while(p->rear != index)
{
printf("%c->", p->sa[index]);
index = (index + 1) % MAX;
}
}
int main()
{
SQueue *A; // Create a queue
A = initQueue(); // initialization
printf(" The team :");
for(char i = 'A'; i <= 'D'; i++)
{
push(A, i); // The team
}
display(A);
printf("\n captain :%d\n", queuelength(A));
printf(" Head element :%c\n", GetHead(A));
printf(" Out of the team :");
for(int i = 0; i < 4; i++)
{
printf("%c->", pop(A));
}
system("pause");
return 0;
}
result :
The team :A->B->C->D->
captain :4
Head element :A
Out of the team :A->B->C->D->
Recommendation of compiling tools : Xiaobai studies C Language , I still recommend VS, Simple and convenient operation .
Circular queue : It's actually a sequential storage structure , Adopt modular operation , It realizes the connection of head and tail , It's essentially an array , It just changed the operation of moving the pointer , Or in the form of an array .
版权声明
本文为[Li Feiyu_ smile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210618260160.html
边栏推荐
- Pyqt5 + yolov5 + MSS to realize a real-time desktop detection software
- GBase 8s V8.8 SQL 指南:教程-5.4
- abcabc
- Delete Xunlei and see the uninstall residue in the folder right-click menu
- Security challenges in an increasingly tangled web
- 2022-1-17 to 2022-1-30 iterator mode
- GBase 8s V8. 8 SQL Guide: tutorial-6.1
- After unity is connected to the ilruntime, the package method under packages is applied to the hot engineering application, such as unity RenderPipelines. Core. Runtime package
- Empty object mode (3.14-3.20)
- Summary of database Deadlock: (3.7-3.13)
猜你喜欢

2022-1-17 to 2022-1-30 iterator mode

Pyqt5 + yolov5 + MSS to realize a real-time desktop detection software

Mengxin sees the recruitment of volunteers in the open source community of wedatasphere

AWD platform construction – cardinal
My creation anniversary

Apache poi HSSF operation Excel

Multithreaded rendering mechanism of Unreal Engine

Fundamentals of graphics - depth of field / DOF

深圳-西双版纳

【WPF】Popup
随机推荐
【WPF】UpdateSourceTrigger
SQL learning record
Security challenges in an increasingly tangled web
Empty object mode (3.14-3.20)
abcabc
GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.2 (1)
GBase 8s V8. 8 SQL Guide: Tutorial - 5.3.1
Pyqt5+Yolov5+Mss实现一个实时桌面检测软件
Auto. JS canvas setting anti aliasing paint setAntiAlias(true);
时间复杂度和空间复杂度
The signature of the update package is inconsistent with that of the installed app
Supporting early and scalable discovery of information websites
IT配电及防火限流式保护器应用及选型
Unity list uses find or findall to find specific elements and the number of specific elements
Visitor mode from 2022-1-3 to 2022-1-16
Online Tetris with automatic hang-up source code
Codeforces Round #783 (Div. 2) ABCD
13.9.1-PointersOnC-20220421
Unity about the real machine failure of ispointerovergameobject interface
Von Neumann architecture