数据结构 循环队列

头文件

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;
}
相关推荐
INGNIGHT13 小时前
91 · 最小调整代价(序列dp)
数据结构·算法·leetcode
从小白到软工17 小时前
5-2 从查表到控件:造按钮
c语言·开发语言·数据结构·经验分享·ai编程
David猪大卫19 小时前
【C++修炼】STL容器—stack与queue
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
瑶池酒剑仙19 小时前
C++变量初始化五法完全指南:默认/拷贝/直接/列表/值初始化与窄化转换陷阱(青云之志)
c语言·开发语言·数据结构·c++·算法·visual studio
秋天的怀念_David21 小时前
听故事学语言 · 数据结构篇 · 序章一 算法之问
数据结构·程序人生·算法
-dzk-1 天前
【链表】LC 25.K 个一组翻转链表
数据结构·链表
careathers1 天前
【数据结构】基于比较的排序算法 - 插入排序
java·数据结构·排序算法
MicrosoftCloud1 天前
数据结构 07(上)|查找:顺序、折半、分块,ASL 到底怎么
数据结构·查找·顺序查找·折半查找·分块查找·平均查找长度·asl
Mortalbreeze1 天前
0 - 1 BFS 算法详解:从基本原理到经典题目建模
数据结构·c++·算法·图论·宽度优先
shehuiyuelaiyuehao2 天前
算法56,重排链表
数据结构·算法·链表