当前位置:网站首页>Circular queue (C language)
Circular queue (C language)
2022-07-06 14:16:00 【An Ruoxi~】
What we need to pay attention to in the loop queue is the position of each head pointer and tail pointer , Because it's a circular queue , So every time you join the team , Both the head pointer and the tail pointer will +1, But every time I leave the team , Only the tail pointer -1, When the head pointer meets the tail pointer , Indicates that the queue is empty , We can use this to judge the space .
Header file function declaration
#define MAX_SIZE 100
typedef int ELEM_TYPE;
typedef struct Queue {
ELEM_TYPE* base;// Accept malloc Space to apply
int front;// The head pointer
int rear;// Tail pointer
//int count;
}Queue,*PQueue;
void Init(PQueue pq);
bool Push(PQueue pq,ELEM_TYPE val);
bool Pop(PQueue pq);
bool Top(PQueue pq,ELEM_TYPE* rtval);
bool IsEmpty(PQueue pq);
bool IsFull(PQueue pq);
int Get_length(PQueue pq);
void Clear(PQueue pq);
void Destory(PQueue pq);
void show(PQueue pq);Implementation of header file function declaration
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"cqueue.h"
void Init(PQueue pq) {
assert(pq!=NULL);
pq->base = (ELEM_TYPE*)malloc(MAX_SIZE * sizeof(ELEM_TYPE));
assert(pq->base != NULL);
pq->front = 0;
pq->rear = 0;
}
bool Push(PQueue pq, ELEM_TYPE val) {
assert(pq != NULL);
if (IsFull(pq)) {
return false;
}
pq->base[pq->rear] = val;
pq->rear = (pq->rear + 1) % MAX_SIZE;
return true;
}
bool Pop(PQueue pq) {
assert(pq != NULL);
if (IsEmpty(pq)) {
return false;
}
pq->front=(pq->front+1)%MAX_SIZE;
return true;
}
bool Top(PQueue pq, ELEM_TYPE* rtval) {
assert(pq != NULL);
if (IsEmpty(pq)) {
return false;
}
*rtval = pq->base[pq->front];
return true;
}
bool IsEmpty(PQueue pq) {
assert(pq != NULL);
return pq->front == pq->rear;
}
bool IsFull(PQueue pq) {
assert(pq != NULL);
return (pq->rear + 1) % MAX_SIZE == pq->front;
}
int Get_length(PQueue pq) {
assert(pq != NULL);
int count= (pq->rear - pq->front + MAX_SIZE) % MAX_SIZE;// The first is to prevent negative numbers , The latter is to prevent positive numbers from increasing
return count;
}
void Clear(PQueue pq) {
assert(pq != NULL);
pq->front = pq->rear = 0;
}
void Destory(PQueue pq) {
assert(pq != NULL);
free(pq->base);
pq->front = pq->rear = 0;
}
void show(PQueue pq) {
assert(pq != NULL);
for (int i = pq->front;i!= pq->rear;i=(i+1)%MAX_SIZE) {
printf("%5d",pq->base[i]);
}
printf("\n");
}Here we should pay attention to , Per head pointer , Tail pointer +1 In order to prevent cross-border , We balance the capacity between it and the circular queue , To prevent cross-border .
Test functions
#include<stdio.h>
#include"cqueue.h"
int main() {
struct Queue head;
Init(&head);
for (int i = 0; i < 20;i++) {
Push(&head,i+1);
}
show(&head);
printf("front %d\n",head.front);
printf("rear %d\n", head.rear);
Pop(&head);
Pop(&head);
Pop(&head);
show(&head);
printf("front %d\n", head.front);
printf("rear %d\n", head.rear);
ELEM_TYPE temp;
bool tag=Top(&head,&temp);
if (tag) {
printf("temp= %d\n",temp);
}
printf("length= %d\n", Get_length(&head));
for (int i = 0; i < 81; i++) {
Push(&head, i + 1);
}
show(&head);
printf("front %d\n", head.front);
printf("rear %d\n", head.rear);
printf("length= %d\n",Get_length(&head));
Clear(&head);
Destory(&head);
show(&head);
return 0;
}The test case

边栏推荐
- 中间件漏洞复现—apache
- Experiment 7 use of common classes (correction post)
- 【Numpy和Pytorch的数据处理】
- Tencent map circle
- Intranet information collection of Intranet penetration (5)
- It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
- Hackmyvm target series (1) -webmaster
- Harmonyos JS demo application development
- DVWA (5th week)
- [paper reproduction] cyclegan (based on pytorch framework) {unfinished}
猜你喜欢

Record once, modify password logic vulnerability actual combat

7-7 7003 组合锁(PTA程序设计)

Strengthen basic learning records

Xray and Burp linked Mining

7-5 staircase upgrade (PTA program design)

Canvas foundation 1 - draw a straight line (easy to understand)

Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)

强化学习基础记录

Record an API interface SQL injection practice
![[paper reproduction] cyclegan (based on pytorch framework) {unfinished}](/img/16/43d8929d1a37c1c68e959d5854e18c.jpg)
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
随机推荐
Web vulnerability - File Inclusion Vulnerability of file operation
【educoder数据库实验 索引】
Simply understand the promise of ES6
Internet Management (Information Collection)
. How to upload XMIND files to Jinshan document sharing online editing?
Hackmyvm target series (2) -warrior
【头歌educoder数据表中数据的插入、修改和删除】
Strengthen basic learning records
7-5 走楼梯升级版(PTA程序设计)
Renforcer les dossiers de base de l'apprentissage
Yugu p1012 spelling +p1019 word Solitaire (string)
《英特尔 oneAPI—打开异构新纪元》
内网渗透之内网信息收集(四)
7-3 construction hash table (PTA program design)
Low income from doing we media? 90% of people make mistakes in these three points
小程序web抓包-fiddler
Hackmyvm Target Series (3) - vues
记一次edu,SQL注入实战
实验七 常用类的使用
强化学习基础记录