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

运行结果

思维导图

相关推荐
CoovallyAIHub5 小时前
181小时视频丢给GPT-5,准确率只有15%——南大联合NVIDIA等五校发布多模态终身理解数据集
深度学习·算法·计算机视觉
CoovallyAIHub5 小时前
CVPR 2026 | GS-CLIP:3D几何先验+双流视觉融合,零样本工业缺陷检测新SOTA,四大3D工业数据集全面领先!
深度学习·算法·计算机视觉
xlp666hub5 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
会员源码网7 小时前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
有意义7 小时前
深度拆解分割等和子集:一维DP数组与倒序遍历的本质
前端·算法·面试
xlp666hub9 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
用户726876103379 小时前
解放双手的健身助手:基于 Rokid AR 眼镜的运动计时应用
算法
Wect9 小时前
LeetCode 17. 电话号码的字母组合:回溯算法入门实战
前端·算法·typescript
不想写代码的星星9 小时前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++
xlp666hub1 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode