数据结构 循环队列

头文件

cpp 复制代码
#pragma once
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
using namespace std;
#define max_size 10
typedef struct circlequeue {
	int* data;
	int front;//队头指针
	int rear;//队尾指针
}circlequeue, * pcirclequeue;

//初始化
void init_queue(circlequeue* p);
//入队
bool push(circlequeue* p, int val);
//出队
bool pop(circlequeue* p);
//获取队头元素值
int front(circlequeue* p);
//查找
int search(circlequeue* p, int val);
//判空
bool is_empty(circlequeue* p);
//判满 
bool is_full(circlequeue* p);
//清空
void clear(circlequeue* p);
//销毁
void destroy(circlequeue* p);
//打印
void show(circlequeue* p);
//有效值个数
int getsize(circlequeue* p);

源文件

cpp 复制代码
#include"循环队列.h"

//初始化
void init_queue(circlequeue* p) {
    assert(p != nullptr);
    p->data = (int*)malloc(sizeof(int) * max_size);
    if (p->data == nullptr) {
        printf("内存分配失败\n");
        return;
    }
    p->front = 0;
    p->rear = 0;
}
//判空
bool is_empty(circlequeue* p) {
    assert(p != nullptr);
    return p->front == p->rear;
}
//入队
bool push(circlequeue* p, int val) {
    assert(p != nullptr);
    if (is_full(p))return false;
    p->data[p->rear] = val;
    p->rear = (p->rear + 1) % max_size;
    return true;
}
//出队
bool pop(circlequeue* p) {
    assert(p != nullptr);
    if (is_empty(p))return false;
    p->front = (p->front + 1) % max_size;
    return true;
}
//获取队头元素值
int front(circlequeue* p) {
    assert(p != nullptr);
    if (is_empty(p))return NULL;
    return p->data[p->front];
}
//查找
int search(circlequeue* p, int val) {
    assert(p != nullptr);
    if (is_empty(p))return -1;
    int i = p->front;
    int pos = 0;
    while (i != p->rear) {
        if (p->data[i] == val) return pos;
        i = (i + 1) % max_size;
        pos++;
    }
    return -1;
}

//判满 
bool is_full(circlequeue* p) {
    assert(p != nullptr);
    return ((p->rear + 1) % max_size == p->front);
}
//清空
void clear(circlequeue* p) {
    assert(p != nullptr);
    p->front = 0;
    p->rear = 0;
}
//销毁
void destroy(circlequeue* p) {
    assert(p != nullptr);
    free(p->data);
    p->data = nullptr;
    p->front = 0;
    p->rear = 0;
}
//打印
void show(circlequeue* p) {
    assert(p != nullptr);
    if (is_empty(p)) return;
    printf("队列元素: ");
    int i = p->front;
    while (i != p->rear) {
        printf("%d ", p->data[i]);
        i = (i + 1) % max_size;
    } printf("\n");
}
//有效值个数
int getsize(circlequeue* p) {
    assert(p != nullptr);
    return (p->rear - p->front + max_size) % max_size;
}
相关推荐
不知名的老吴7 分钟前
经典算法实战:重新排列日志文件(二)
数据结构·算法
CS创新实验室23 分钟前
数据结构和算法:斐波那契堆
数据结构·算法·斐波那契堆
guslegend3 小时前
2.Redis核心数据结构
数据结构·数据库·redis
Daydream.V3 小时前
Redis 零基础入门到实战:数据结构 + 常用命令 + 场景全覆盖
数据结构·数据库·redis
bnmoel3 小时前
数据结构深度剖析二叉树・中篇:堆的概念及结构 ,实现应用全解析
数据结构·算法·二叉树··top-k问题
fu的博客3 小时前
【数据结构15】哈夫曼树构建、编码(附手绘图解)
数据结构
bnmoel3 小时前
数据结构深度剖析二叉树・上篇:基础概念、结构特性、存储结构全解析
c语言·数据结构·二叉树·
Dlrb12113 小时前
数据结构-单链表与双链表
c语言·数据结构·链表·排序·双链表
小龙报3 小时前
【优选算法】双指针专项:1.移动零 2. 复写零 3.快乐数
java·c语言·数据结构·c++·python·算法·面试
sukioe4 小时前
深入理解 MySQL 索引:底层数据结构与 B+ 树设计原理
数据结构·mysql·oracle