mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-16 06:56:54 +08:00
更新队列
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 单链表结点
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
4
Code/CPP-Code/head/link_queue.h
Normal file
4
Code/CPP-Code/head/link_queue.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
// 链队
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
@@ -14,8 +13,14 @@ private:
|
||||
element_type *_data{};
|
||||
// 长度
|
||||
int _length{};
|
||||
// 最大容量
|
||||
int _max_size{};
|
||||
public:
|
||||
// 设置数据
|
||||
bool SetData();
|
||||
|
||||
bool SetData(int max_size);
|
||||
|
||||
bool SetData(element_type *elem);
|
||||
|
||||
bool SetData(int index, element_type elem);
|
||||
@@ -31,12 +36,22 @@ public:
|
||||
// 设置长度
|
||||
bool SetLength(int length);
|
||||
|
||||
// 设置长度
|
||||
// 获取长度
|
||||
int GetLength() const;
|
||||
|
||||
// 设置最大容量
|
||||
bool SetMaxSize();
|
||||
|
||||
bool SetMaxSize(int max_size);
|
||||
|
||||
// 获取最大容量
|
||||
int GetMaxSize() const;
|
||||
|
||||
// 构造函数
|
||||
SequenceList();
|
||||
|
||||
explicit SequenceList(int max_size);
|
||||
|
||||
// 插入函数
|
||||
virtual bool Insert(int index, element_type elem) = 0;
|
||||
|
||||
@@ -65,6 +80,16 @@ public:
|
||||
bool Destroy() const;
|
||||
};
|
||||
|
||||
bool SequenceList::SetData() {
|
||||
this->_data = new element_type[MAXSIZE];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceList::SetData(int max_size) {
|
||||
this->_data = new element_type[max_size];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceList::SetData(element_type *elem) {
|
||||
this->_data = elem;
|
||||
return true;
|
||||
@@ -97,7 +122,29 @@ int SequenceList::GetLength() const {
|
||||
return this->_length;
|
||||
}
|
||||
|
||||
bool SequenceList::SetMaxSize() {
|
||||
this->_max_size = MAXSIZE;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceList::SetMaxSize(int max_size) {
|
||||
this->_max_size = max_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceList::GetMaxSize() const {
|
||||
return this->_max_size;
|
||||
}
|
||||
|
||||
SequenceList::SequenceList() {
|
||||
this->SetData();
|
||||
this->SetMaxSize();
|
||||
this->SetLength(0);
|
||||
}
|
||||
|
||||
SequenceList::SequenceList(int max_size) {
|
||||
this->SetData(max_size);
|
||||
this->SetMaxSize(max_size);
|
||||
this->SetLength(0);
|
||||
}
|
||||
|
||||
@@ -189,19 +236,24 @@ public:
|
||||
// 构造函数
|
||||
StaticSequenceList();
|
||||
|
||||
explicit StaticSequenceList(int max_size);
|
||||
|
||||
// 插入函数
|
||||
bool Insert(int index, element_type elem) override;
|
||||
};
|
||||
|
||||
StaticSequenceList::StaticSequenceList() : SequenceList() {
|
||||
this->SetData(new element_type[MAXSIZE]);
|
||||
|
||||
}
|
||||
|
||||
StaticSequenceList::StaticSequenceList(int max_size) : SequenceList(max_size) {
|
||||
}
|
||||
|
||||
bool StaticSequenceList::Insert(int index, element_type elem) {
|
||||
// 当静态顺序表已经满了就不能插入任何元素
|
||||
if (this->GetLength() >= MAXSIZE) {
|
||||
if (this->GetLength() >= this->GetMaxSize()) {
|
||||
// cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
|
||||
cout << "Insert:The space size of " << MAXSIZE << " is not enough!" << endl;
|
||||
cout << "Insert:The space size of " << this->GetMaxSize() << " is not enough!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 索引位置从0开始,所以可以插入的范围是0到list->length
|
||||
@@ -221,19 +273,13 @@ bool StaticSequenceList::Insert(int index, element_type elem) {
|
||||
|
||||
// 动态顺序表
|
||||
class DynamicSequenceList : public SequenceList {
|
||||
private:
|
||||
// 已分配的最大容量
|
||||
int _max_size{};
|
||||
public:
|
||||
// 设置最大容量
|
||||
bool SetMaxSize(int max_size);
|
||||
|
||||
// 获取最大容量
|
||||
int GetMaxSize() const;
|
||||
|
||||
// 构造函数
|
||||
DynamicSequenceList();
|
||||
|
||||
explicit DynamicSequenceList(int max_size);
|
||||
|
||||
// 插入函数
|
||||
bool Insert(int index, element_type elem) override;
|
||||
|
||||
@@ -245,22 +291,10 @@ private:
|
||||
bool ReIncrease(int len);
|
||||
};
|
||||
|
||||
bool DynamicSequenceList::SetMaxSize(int max_size) {
|
||||
this->_max_size = max_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
int DynamicSequenceList::GetMaxSize() const {
|
||||
return this->_max_size;
|
||||
}
|
||||
|
||||
DynamicSequenceList::DynamicSequenceList() : SequenceList() {
|
||||
// 初初始化动态顺序表长度为0
|
||||
this->SetMaxSize(0);
|
||||
// 申请一片连续的存储空间
|
||||
auto *space = new element_type[MAXSIZE];
|
||||
this->SetData(space);
|
||||
this->SetMaxSize(MAXSIZE);
|
||||
}
|
||||
|
||||
DynamicSequenceList::DynamicSequenceList(int max_size) : SequenceList(max_size) {
|
||||
}
|
||||
|
||||
bool DynamicSequenceList::OtherIncrease(int length) {
|
||||
@@ -312,7 +346,7 @@ bool DynamicSequenceList::Insert(int index, element_type elem) {
|
||||
}
|
||||
// 当动态顺序表已经满了,需要新增一个位置
|
||||
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
|
||||
if (this->GetLength() >= MAXSIZE) {
|
||||
if (this->GetLength() >= this->GetMaxSize()) {
|
||||
this->ReIncrease(1);
|
||||
}
|
||||
for (int i = this->GetLength(); i > index; i--) {
|
||||
|
||||
232
Code/CPP-Code/head/sequence_queue.h
Normal file
232
Code/CPP-Code/head/sequence_queue.h
Normal file
@@ -0,0 +1,232 @@
|
||||
#include "head.h"
|
||||
|
||||
// 顺序队列
|
||||
class SequenceQueue {
|
||||
private:
|
||||
// 数据
|
||||
element_type *_data{};
|
||||
// 队头队尾指针
|
||||
int _front{}, _rear{};
|
||||
// 队列最大容量
|
||||
int _max_size{};
|
||||
public:
|
||||
// 设置数据
|
||||
bool SetData();
|
||||
|
||||
bool SetData(int max_size);
|
||||
|
||||
bool SetData(element_type *elem);
|
||||
|
||||
bool SetData(int index, element_type elem);
|
||||
|
||||
// 获取数据
|
||||
element_type *GetData();
|
||||
|
||||
element_type GetData(int index);
|
||||
|
||||
// 队头自加
|
||||
bool SetFront();
|
||||
|
||||
// 设置队头
|
||||
bool SetFront(int front);
|
||||
|
||||
// 获取队头
|
||||
int GetFront() const;
|
||||
|
||||
// 队尾自加
|
||||
bool SetRear();
|
||||
|
||||
// 设置队尾
|
||||
bool SetRear(int rear);
|
||||
|
||||
// 获取队尾
|
||||
int GetRear() const;
|
||||
|
||||
// 设置最大容量
|
||||
bool SetMaxSize();
|
||||
|
||||
bool SetMaxSize(int max_size);
|
||||
|
||||
// 获取最大容量
|
||||
int GetMaxSize() const;
|
||||
|
||||
// 构造函数
|
||||
SequenceQueue();
|
||||
|
||||
explicit SequenceQueue(int max_size);
|
||||
|
||||
// 判空
|
||||
bool Empty() const;
|
||||
|
||||
// 判满
|
||||
bool Full() const;
|
||||
|
||||
// 循环队列判满
|
||||
bool FullCircular() const;
|
||||
|
||||
// 进队
|
||||
bool Enter(element_type elem);
|
||||
|
||||
// 循环队列进队
|
||||
bool EnterCircular(element_type elem);
|
||||
|
||||
// 出队
|
||||
element_type Depart();
|
||||
|
||||
// 循环队列出队
|
||||
element_type DepartCircular();
|
||||
|
||||
// 获取队长
|
||||
int Length() const;
|
||||
|
||||
// 读队头
|
||||
element_type Head();
|
||||
};
|
||||
|
||||
bool SequenceQueue::SetData() {
|
||||
this->_data = new element_type[MAXSIZE];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetData(int max_size) {
|
||||
this->_data = new element_type[max_size];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetData(element_type *elem) {
|
||||
this->_data = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetData(int index, element_type elem) {
|
||||
this->_data[index] = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
element_type *SequenceQueue::GetData() {
|
||||
return this->_data;
|
||||
}
|
||||
|
||||
element_type SequenceQueue::GetData(int index) {
|
||||
return this->_data[index];
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetFront() {
|
||||
this->_front++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetFront(int front) {
|
||||
this->_front = front;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceQueue::GetFront() const {
|
||||
return this->_front;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetRear() {
|
||||
this->_rear++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetRear(int rear) {
|
||||
this->_rear = rear;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceQueue::GetRear() const {
|
||||
return this->_rear;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetMaxSize() {
|
||||
this->_max_size = MAXSIZE;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceQueue::SetMaxSize(int max_size) {
|
||||
this->_max_size = max_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SequenceQueue::GetMaxSize() const {
|
||||
return this->_max_size;
|
||||
}
|
||||
|
||||
SequenceQueue::SequenceQueue() {
|
||||
this->SetData();
|
||||
this->SetMaxSize();
|
||||
this->SetFront(0);
|
||||
this->SetRear(0);
|
||||
}
|
||||
|
||||
SequenceQueue::SequenceQueue(int max_size) {
|
||||
this->SetData(max_size);
|
||||
this->SetMaxSize(max_size);
|
||||
this->SetFront(0);
|
||||
this->SetRear(0);
|
||||
}
|
||||
|
||||
bool SequenceQueue::Empty() const {
|
||||
return this->GetFront() == this->GetRear();
|
||||
}
|
||||
|
||||
bool SequenceQueue::Full() const {
|
||||
return this->GetFront() == this->GetMaxSize();
|
||||
}
|
||||
|
||||
bool SequenceQueue::FullCircular() const {
|
||||
return (this->GetRear() + 1) % this->GetMaxSize() == this->GetFront();
|
||||
}
|
||||
|
||||
bool SequenceQueue::Enter(element_type elem) {
|
||||
if (this->Full()) {
|
||||
cout << "Enter:The queue is full!" << endl;
|
||||
return false;
|
||||
}
|
||||
this->SetData(this->GetRear(), elem);
|
||||
this->SetRear();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceQueue::EnterCircular(element_type elem) {
|
||||
if (this->FullCircular()) {
|
||||
cout << "EnterCircular:The queue is full!" << endl;
|
||||
return false;
|
||||
}
|
||||
this->SetData(this->GetRear(), elem);
|
||||
this->SetRear((this->GetRear() + 1) % this->GetMaxSize());
|
||||
return true;
|
||||
}
|
||||
|
||||
element_type SequenceQueue::Depart() {
|
||||
if (this->Empty()) {
|
||||
cout << "Depart:The queue is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = this->GetData(this->GetFront());
|
||||
this->SetFront();
|
||||
return temp;
|
||||
}
|
||||
|
||||
element_type SequenceQueue::DepartCircular() {
|
||||
if (this->Empty()) {
|
||||
cout << "DepartCircular:The queue is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = this->GetData(this->GetFront());
|
||||
this->SetFront((this->GetFront() + 1) % this->GetMaxSize());
|
||||
return temp;
|
||||
}
|
||||
|
||||
int SequenceQueue::Length() const {
|
||||
return (this->GetRear() - this->GetFront() + this->GetMaxSize()) % this->GetMaxSize();
|
||||
}
|
||||
|
||||
element_type SequenceQueue::Head() {
|
||||
if (this->Empty()) {
|
||||
cout << "Head:The queue is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return this->GetData(this->GetFront());
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -1,22 +1,29 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include "head.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 共享栈
|
||||
class ShareStack{
|
||||
private:
|
||||
// 栈内元素
|
||||
element_type *_data{};
|
||||
// 栈顶指针,left从0开始,right从MAXSIZE开始
|
||||
// 栈顶指针,left从-1开始,right从MAXSIZE开始
|
||||
int _top_left{}, _top_right{};
|
||||
// 最大容量
|
||||
int _max_size{};
|
||||
public:
|
||||
// 设置数据
|
||||
bool SetData();
|
||||
|
||||
bool SetData(element_type *data);
|
||||
|
||||
bool SetData(int max_size);
|
||||
|
||||
bool SetData(int index, element_type elem);
|
||||
|
||||
// 获取数据
|
||||
element_type * GetData();
|
||||
|
||||
element_type GetData(int index);
|
||||
|
||||
// 左栈顶指针自加
|
||||
bool SetTopLeft();
|
||||
|
||||
@@ -36,6 +43,8 @@ public:
|
||||
int GetTopRight() const;
|
||||
|
||||
// 设置最大容量
|
||||
bool SetMaxSize();
|
||||
|
||||
bool SetMaxSize(int max_size);
|
||||
|
||||
// 获取最大容量
|
||||
@@ -80,11 +89,34 @@ public:
|
||||
element_type TopRight();
|
||||
};
|
||||
|
||||
bool ShareStack::SetData() {
|
||||
this->_data = new element_type[MAXSIZE];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShareStack::SetData(element_type *data) {
|
||||
this->_data = data;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShareStack::SetData(int max_size) {
|
||||
this->_data = new element_type [max_size];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShareStack::SetData(int index, element_type elem) {
|
||||
this->_data[index] = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
element_type *ShareStack::GetData() {
|
||||
return this->_data;
|
||||
}
|
||||
|
||||
element_type ShareStack::GetData(int index) {
|
||||
return this->_data[index];
|
||||
}
|
||||
|
||||
bool ShareStack::SetTopLeft() {
|
||||
this->_top_left++;
|
||||
return true;
|
||||
@@ -113,6 +145,11 @@ int ShareStack::GetTopRight() const {
|
||||
return this->_top_right;
|
||||
}
|
||||
|
||||
bool ShareStack::SetMaxSize() {
|
||||
this->_max_size = MAXSIZE;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShareStack::SetMaxSize(int max_size) {
|
||||
this->_max_size = max_size;
|
||||
return true;
|
||||
@@ -123,14 +160,14 @@ int ShareStack::GetMaxSize() const {
|
||||
}
|
||||
|
||||
ShareStack::ShareStack() {
|
||||
this->SetData(new element_type[MAXSIZE]);
|
||||
this->SetMaxSize(MAXSIZE);
|
||||
this->SetData();
|
||||
this->SetMaxSize();
|
||||
this->SetTopLeft(-1);
|
||||
this->SetTopRight(MAXSIZE);
|
||||
}
|
||||
|
||||
ShareStack::ShareStack(int max_size) {
|
||||
this->SetData(new element_type[max_size]);
|
||||
this->SetData(max_size);
|
||||
this->SetMaxSize(max_size);
|
||||
this->SetTopLeft(-1);
|
||||
this->SetTopRight(max_size);
|
||||
@@ -162,7 +199,8 @@ bool ShareStack::PushLeft(element_type elem) {
|
||||
cout << "PushLeft:The stack is full!";
|
||||
return false;
|
||||
}
|
||||
this->_data[this->SetTopLeft()] = elem;
|
||||
this->SetTopLeft();
|
||||
this->SetData(this->GetTopLeft(), elem);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -171,8 +209,8 @@ bool ShareStack::PushRight(element_type elem) {
|
||||
// cout << "PushRight:栈满无法进栈!" << endl;
|
||||
cout << "PushRight:The stack is full!" << endl;
|
||||
return false;
|
||||
}
|
||||
this->_data[this->SetTopRight()] = elem;
|
||||
}this->SetTopRight();
|
||||
this->SetData(this->GetTopRight(), elem);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -182,7 +220,9 @@ element_type ShareStack::PopLeft() {
|
||||
cout << "PopLeft:The stack is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return this->_data[this->SetTopLeft(this->GetTopLeft() - 1)];
|
||||
element_type temp = this->GetData(this->GetTopLeft());
|
||||
this->SetTopLeft(this->GetTopLeft() - 1);
|
||||
return temp;
|
||||
}
|
||||
|
||||
element_type ShareStack::PopRight() {
|
||||
@@ -191,7 +231,9 @@ element_type ShareStack::PopRight() {
|
||||
cout << "PopRight:The stack is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return this->_data[this->SetTopRight(this->GetTopRight() + 1)];
|
||||
element_type temp = this->GetData(this->GetTopRight());
|
||||
this->SetTopRight(this->GetTopRight() + 1);
|
||||
return temp;
|
||||
}
|
||||
|
||||
element_type ShareStack::TopLeft() {
|
||||
@@ -200,7 +242,7 @@ element_type ShareStack::TopLeft() {
|
||||
cout << "TopLeft:The stack is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return this->_data[this->GetTopLeft() - 1];
|
||||
return this->GetData(this->GetTopLeft());
|
||||
}
|
||||
|
||||
element_type ShareStack::TopRight() {
|
||||
@@ -209,5 +251,5 @@ element_type ShareStack::TopRight() {
|
||||
cout << "TopRight:The stack is empty!" << endl;
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return this->_data[this->GetTopRight() + 1];
|
||||
return this->GetData(this->GetTopRight());
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
class StaticLinkListNode {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
// SequenceListTest();
|
||||
LinkListTest();
|
||||
SequenceListTest();
|
||||
// LinkListTest();
|
||||
return 0;
|
||||
}
|
||||
@@ -7,13 +7,15 @@
|
||||
#include "../head/sequence_stack.h"
|
||||
#include "../head/share_stack.h"
|
||||
#include "../head/link_stack.h"
|
||||
#include "../head/sequence_queue.h"
|
||||
#include "../head/link_queue.h"
|
||||
|
||||
bool SequenceListTest() {
|
||||
DynamicSequenceList list;
|
||||
element_type a[6] = {'1','2','3','4','5','6'};
|
||||
list.LoopInsert(a, 0, 6);
|
||||
list.Print();
|
||||
element_type * data = list.GetData();
|
||||
// element_type * data = list.GetData();
|
||||
element_type* b = list.LoopDelete(1, 3);
|
||||
list.Print();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
|
||||
Reference in New Issue
Block a user