数据结构 循环队列

头文件

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;
}
相关推荐
tobias.b9 小时前
计算机基础知识-数据结构
java·数据结构·考研
不想看见40411 小时前
Valid Parentheses栈和队列--力扣101算法题解笔记
开发语言·数据结构·c++
计算机安禾11 小时前
【C语言程序设计】第37篇:链表数据结构(一):单向链表的实现
c语言·开发语言·数据结构·c++·算法·链表·蓝桥杯
皮卡狮12 小时前
高阶数据结构:AVL树
数据结构·算法
不要秃头的小孩13 小时前
50. 随机数排序
数据结构·python·算法
故事和你9113 小时前
sdut-python-实验四-python序列结构(21-27)
大数据·开发语言·数据结构·python·算法
丶小鱼丶14 小时前
数据结构和算法之【栈】
java·数据结构
不要秃头的小孩14 小时前
力扣刷题——111.二叉树的最小深度
数据结构·python·算法·leetcode
散峰而望14 小时前
【基础算法】从入门到实战:递归型枚举与回溯剪枝,暴力搜索的初级优化指南
数据结构·c++·后端·算法·机器学习·github·剪枝
elseif12316 小时前
CSP-S提高级大纲
开发语言·数据结构·c++·笔记·算法·大纲·考纲