文件结构 :
文件名字 | 用途 |
CmakeList.txt | cmake文件 |
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位