9.25作业

手动实现队列

代码如下

MyQueue.h

#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

#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

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

运行结果

在这里插入图片描述

思维导图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值