数据结构 循环队列

头文件

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;
}
相关推荐
ValhallaCoder6 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
月挽清风8 小时前
代码随想录第十五天
数据结构·算法·leetcode
NEXT068 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
小妖66612 小时前
js 实现快速排序算法
数据结构·算法·排序算法
独好紫罗兰14 小时前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n14 小时前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
2401_8414956414 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
独好紫罗兰15 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_8414956415 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
数智工坊15 小时前
【数据结构-树与二叉树】4.5 线索二叉树
数据结构