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;
}

运行结果

思维导图

相关推荐
小小年纪不学好5 分钟前
【60.组合总和】
java·算法·面试
关于不上作者榜就原神启动那件事5 分钟前
力扣hot100,739每日温度(单调栈)详解
算法·leetcode·职场和发展
小卡皮巴拉7 分钟前
【力扣刷题实战】丢失的数字
c++·算法·leetcode·位运算
凤年徐8 分钟前
【C/C++】深入理解指针(六)
c语言·开发语言·c++·经验分享·笔记·指针
学了就忘17 分钟前
JavaScript 异步编程与请求取消全指南
开发语言·javascript·ecmascript
通南北21 分钟前
使用python实现自动化拉取压缩包并处理流程
开发语言·python·自动化
十五年专注C++开发28 分钟前
Qt中的全局函数讲解集合(全)
开发语言·c++·qt·算法
浅笑安然ELIAUK32 分钟前
算法-移除元素
算法
潇湘夜雨6971 小时前
2023ICPC合肥题解
算法
martian6651 小时前
信创时代技术栈选择与前景分析:国产替代背景下的战略路径与实践指南
开发语言·科技·系统安全·创业创新