基于C++面向对象技术实现顺序队列(附源代码)

文件结构 : 

文件名字用途
CmakeList.txtcmake文件
how.md简述思路以及其他说明
main.cpp主测试程序
SeqQuence.cpp核心实现文件
SeqQuence.h核心头文件

SeqQuence.cpp

//
// Created by A Luck Boy on 2023/1/18.
//
#include "SeqQueue.h"

// 创建并且初始化
Queue::Queue(int size) {
    front = rear = 0;
    data = new int[5]  ;
    maxSize = size;
    cout << "创建并且初始化成功!" << endl;
}

// 销毁
Queue::~Queue() {
    delete[] data;
    cout << "销毁成功!" << endl;
}

// 空队
bool Queue::isEmpty() const {
    return rear == front;       // 或者 rear - front == 0;
};

// 长度
int Queue::getLength() const {
    return -front + rear;
};

// 入队(类似于添加)
void Queue::push(int element) {
    // 判断满了吗,满了就扩容,每次扩容2个元素量
    if (maxSize == getLength()){
        int *another_data = new int[maxSize + 2];
        for (int i=0;i<getLength();i++){
            another_data[i] = data[i];
        }
        delete[] data;
        data = another_data;
        maxSize += 2;
    } else{}
    data[rear] = element;
    rear++;
    // 如果是空队列,需要让队头赋值
    if (isEmpty()){
        data[front] = element;
    } else{}
};

// 出队(类似于删除,不过删除的是头)
void Queue::pop() {
    // 删除的是front的数据,然后把front重新赋值
    // 优先考虑是不是空队
    if (isEmpty()){
        cout << "空队, 无法操作!\n";
    } else{
        int *another_data_2 = new int[maxSize];
        for (int i=0;i<getLength()-1;i++){
            another_data_2[i] = data[i+1];
        }
        delete[] data;
        data = another_data_2;
        rear--;
        data[rear] = data[getLength()];
    }
};

// 遍历
void Queue::show() {
    // 考虑空队,什么不做
    if (isEmpty()) return; // 其实这句话没必要,但我还是习惯上写了
    else{
        for (int i=0;i<getLength();i++){
            cout << "Index " << i << "Number is " << data[i] <<endl;
        }
    }
}

// 清空
void Queue::clear() {
    // 考虑空队,什么不做
    if (isEmpty()){
        return;
    } else{
       int *new_data = new int [maxSize];
       delete[] data;
       data = new_data;
       // 别忘了把rear重载
       rear = front;
    }
};

// 最大容量
int Queue::allSize() const{
    return maxSize;
}

SeqQuence.h

//
// Created by A Luck Boy on 2023/1/18.
//
#include <iostream>

using namespace std;

#define END return 0;
#define Waring cout<<"代码有误"<<endl;
#define main int main

class Queue{
private:
    // 队头索引
    int front;
    // 队尾后面那个位置索引
    int rear;
    // 元素
    int *data;
    // 最大允许数据量
    int maxSize;
public:
    // 创建并且初始化
    Queue(int size=5);
    // 销毁
    ~Queue();
    // 空队
    bool isEmpty() const;
    // 长度
    int getLength() const;
    // 入队(类似于添加)
    void push(int element);
    // 出队(类似于删除,不过删除的是头)
    void pop();
    // 遍历
    void show();
    // 清空
    void clear();
    // 最大容量
    int allSize() const;
};

main.cpp

//
// Created by A Luck Boy on 2023/1/18.
//
#include "SeqQueue.h"

main(){
    // 创建一个最大允许5个数据的队列
    Queue queue(5);
    // 判空
    if (queue.isEmpty()) cout << "Successfully!" << endl;
    else Waring
    // 添加两个之后,遍历
    queue.push(1);
    queue.push(11);
    queue.show();
    // 再次添加4个之后,看看最大允许容量是不是7(因为溢出后每次扩容2个)
    queue.push(1);
    queue.push(11);
    queue.push(1);
    queue.push(11);
    cout << "MaxSize is " << queue.allSize() <<endl;
    // 清空,然后出队,看看能不能报错
    queue.clear();
    cout << "Current len is " << queue.getLength()  <<endl;
    queue.pop();
    // 添4个, 删除一个, 看效果
    queue.push(5);
    queue.push(15);
    queue.push(115);
    queue.push(1115);
    queue.pop();
    queue.show()    ;
    END
}

终端运行结果

创建并且初始化成功!
Successfully!
Index 0Number is 1
Index 1Number is 11
MaxSize is 7
Current len is 0
空队, 无法操作!
Index 0Number is 15
Index 1Number is 115
Index 2Number is 1115
销毁成功!

特别说明 : g++编译器

Windows位

源代码链接 : 自己实现的数据结构与算法合集: 自己实现的数据结构与算法合集,使用C或者C++ - Gitee.com 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PythonNotJava

若您有别的建议,请在评论区留言

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值