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

运行结果

思维导图

相关推荐
黑科技Python7 分钟前
生活中的“小智慧”——认识算法
学习·算法·生活
Yupureki7 分钟前
从零开始的C++学习生活 16:C++11新特性全解析
c语言·数据结构·c++·学习·visual studio
紫荆鱼40 分钟前
设计模式-迭代器模式(Iterator)
c++·后端·设计模式·迭代器模式
汤姆yu1 小时前
基于python的化妆品销售分析系统
开发语言·python·化妆品销售分析
ScilogyHunter1 小时前
C语言标准库完全指南
c语言·开发语言
sali-tec1 小时前
C# 基于halcon的视觉工作流-章52-生成标定板
开发语言·图像处理·人工智能·算法·计算机视觉
IT古董1 小时前
【第五章:计算机视觉-项目实战之推荐/广告系统】2.粗排算法-(4)粗排算法模型多目标算法(Multi Task Learning)及目标融合
人工智能·算法·1024程序员节
熬了夜的程序员1 小时前
【LeetCode】89. 格雷编码
算法·leetcode·链表·职场和发展·矩阵
应茶茶1 小时前
C++11 核心新特性:从语法重构到工程化实践
java·开发语言·c++
程子的小段2 小时前
C 语言实例 - 字符串复制
c语言·开发语言