当前位置:网站首页>循环队列(C语言)
循环队列(C语言)
2022-07-06 09:23:00 【安若兮~】
循环队列里需要注意的是每次头指针和尾指针的位置,因为是循环队列,所以每次入队,头指针和尾指针都会+1,但是每次出队,只有尾指针-1,当头指针与尾指针相遇的时候,表示这个队列已经是空的了,我们可以利用这个来判空。
头文件函数声明
#define MAX_SIZE 100
typedef int ELEM_TYPE;
typedef struct Queue {
ELEM_TYPE* base;//接受malloc 申请的空间
int front;//表头指针
int rear;//表尾指针
//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);头文件函数声明的实现
#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;//前面是防止出现负数,后面是防止正数加多
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");
}这里我们要注意的点是,每次头指针,尾指针+1为了防止出现越界,我们对其与循环队列的容量取余,防止越界。
测试函数
#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;
}测试用例

边栏推荐
- Record once, modify password logic vulnerability actual combat
- 小程序web抓包-fiddler
- 7-8 7104 Joseph problem (PTA program design)
- Wechat applet
- 链队实现(C语言)
- 7-5 staircase upgrade (PTA program design)
- 2. First knowledge of C language (2)
- Beautified table style
- 【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
- Read only error handling
猜你喜欢

Hackmyvm target series (4) -vulny

Build domain environment (win)

1143_ SiCp learning notes_ Tree recursion

Hackmyvm target series (6) -videoclub

网络基础之路由详解

Detailed explanation of network foundation routing

Meituan dynamic thread pool practice ideas, open source

"Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?

HackMyvm靶机系列(7)-Tron

WEB漏洞-文件操作之文件包含漏洞
随机推荐
Canvas foundation 2 - arc - draw arc
记一次猫舍由外到内的渗透撞库操作提取-flag
Record a penetration of the cat shed from outside to inside. Library operation extraction flag
强化学习基础记录
Safe driving skills on ice and snow roads
Experiment 4 array
【educoder数据库实验 索引】
Experiment 8 exception handling
外网打点(信息收集)
Experiment 7 use of common classes
HackMyvm靶机系列(4)-vulny
7-6 local minimum of matrix (PTA program design)
实验八 异常处理
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
Poker game program - man machine confrontation
7-5 staircase upgrade (PTA program design)
How to turn wechat applet into uniapp
XSS之冷门事件
【数据库 三大范式】一看就懂
UGUI—Text