9.25作业

手动实现队列

代码如下

MyQueue.h

cpp 复制代码
#ifndef MYQUEUE_H
#define MYQUEUE_H
#include <iostream>
#include <cstring>
using namespace std;

class Queue{
private:
    char* data;  //字符串数据
    int len;    //当前数量
    int size;   //最大容量
    int front;  //头索引
    int rear;   //尾索引
public:
    //构造函数
    Queue();
    Queue(const char* d, int size);
    //析构函数
    ~Queue();
    //拷贝构造
    Queue (const Queue &other);
    //拷贝赋值
    Queue& operator= (const Queue &other);
    //front函数
    char MyFront();
    //back函数
    char back();
    //empty函数
    bool empty();
    //size函数
    int MySize();
    //push函数
    void push(char e);
    //emplace函数
    void emplace(char e);
    //pop函数
    char pop();
    //swap函数
    void swap(Queue &other);
};


#endif // MYQUEUE_H

MyQueue.cpp

cpp 复制代码
#include "MyQueue.h"

// 无参构造函数
Queue::Queue():len(0),size(20),front(0),rear(0){
    data = new char[size];
    data[0] = '\0';
};
// 有参构造函数
Queue::Queue(const char* d, int size) : len(strlen(d)), size(size), front(0), rear(len) {
    data = new char[size];
    strcpy(data, d);
}
//析构函数
Queue::~Queue(){delete [] data;}
// 拷贝构造函数
Queue::Queue(const Queue &other) : len(other.len), size(other.size), front(other.front), rear(other.rear) {
    data = new char[size];
    strcpy(data, other.data);
}
// 拷贝赋值运算符
Queue& Queue::operator= (const Queue &other) {
    if (this != &other) {
        delete[] data; // 释放旧内存
        len = other.len;
        size = other.size;
        front = other.front;
        rear = other.rear;
        data = new char[size];
        strcpy(data, other.data);
    }
    return *this;
}
// Myfront函数
char Queue::MyFront(){
    if (empty()) {
        cout<<"队列为空"<<endl;
        exit(EXIT_SUCCESS);
    }
    return data[front];
}
//back函数
char Queue::back(){
    if (empty()) {
        cout<<"队列为空"<<endl;
        exit(EXIT_SUCCESS);
    }
    return data[rear - 1]; // rear指向下一个插入位置
}
// empty函数
bool Queue::empty(){
    return front == rear && len == 0;
}
// MySize函数
int Queue::MySize(){
    return len;
}
// push函数
void Queue::push(char e) {
    if (len >= size) {
        cout << "队列已满" << endl;
        return;
    }
    data[rear++] = e;
    len++;
    data[rear] = '\0'; // 确保字符串以'\0'结尾
}

// emplace函数
void Queue::emplace(char e) {
    push(e); // 直接调用 push 函数
}

// pop函数
char Queue::pop() {
    if (empty()) {
        cout << "队列为空" << endl;
        exit(EXIT_FAILURE);
    }
    len--;
    return data[front++];
}

// swap函数
void Queue::swap(Queue &other) {
    std::swap(len, other.len);
    std::swap(size, other.size);
    std::swap(front, other.front);
    std::swap(rear, other.rear);
    std::swap(data, other.data);
}

main.cpp

cpp 复制代码
#include "MyQueue.h"

int main() {
    Queue q("hello world",20);

    // 测试emplace
    q.emplace('A');
    q.emplace('B');
    q.emplace('C');

    cout << "q队首数据:" << q.MyFront() << endl;
    cout << "q队尾数据:" << q.back() << endl;
    cout << "q队列数据数量 " << q.MySize() << endl;
    // 测试 pop
    cout<<"q尾删操作"<<endl;
    q.pop();
    cout << "q队首数据:" << q.MyFront()<< endl;
    Queue q1;
    q1 = q;
    cout << "s队首数据:" << q1.MyFront() << endl;
    cout << "s队尾数据:" << q1.back() << endl;

    Queue q2("nihao", 20); // 创建一个最大容量为 30 的队列
    cout<<"交换操作"<<endl;
    q.swap(q2);
    cout << "q队首数据:" << q.MyFront() << endl;
    cout << "q队尾数据:" << q.back() << endl;
    cout << "q2队首数据:" << q2.MyFront() << endl;
    cout << "q2队尾数据:" << q2.back() << endl;

    return 0;
}

运行结果

思维导图

相关推荐
道不尽世间的沧桑42 分钟前
第17篇:网络请求与Axios集成
开发语言·前端·javascript
久绊A1 小时前
Python 基本语法的详细解释
开发语言·windows·python
软件黑马王子4 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫4 小时前
go orm GORM
开发语言·后端·golang
计算机小白一个5 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^5 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
黑不溜秋的5 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
李白同学6 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?7 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农7 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx