mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-10-31 04:14:52 +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++) {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 双链表结点
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 单链表结点
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 顺序队列
|
||||
typedef struct {
|
||||
// 链队结点
|
||||
typedef struct LinkQueueNode {
|
||||
// 数据
|
||||
element_type* data;
|
||||
// 指针
|
||||
struct LinkQueueNode *next;
|
||||
} LinkQueueNode;
|
||||
|
||||
// 链队
|
||||
typedef struct {
|
||||
// 队头指针和队尾指针
|
||||
int front, rear;
|
||||
};
|
||||
LinkQueueNode *front, *rear;
|
||||
} LinkQueue;
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
typedef struct LinkStackNode{
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 静态顺序表
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 顺序队列
|
||||
@@ -48,13 +46,76 @@ SequenceQueue InitSequenceQueue(int max_size) {
|
||||
}
|
||||
|
||||
// 判空
|
||||
bool EmptySequenceQueue(SequenceQueue queue){
|
||||
bool EmptySequenceQueue(SequenceQueue queue) {
|
||||
return queue.front == queue.rear;
|
||||
}
|
||||
|
||||
// 判满(存在假溢出)
|
||||
bool FullSequenceQueue(SequenceQueue queue){
|
||||
bool FullSequenceQueue(SequenceQueue queue) {
|
||||
return queue.rear == queue.max_size;
|
||||
}
|
||||
|
||||
// 进队
|
||||
// 判循环队列满
|
||||
bool FullCircularSequenceQueue(SequenceQueue queue) {
|
||||
return (queue.rear + 1) % queue.max_size == queue.front;
|
||||
}
|
||||
|
||||
// 进队
|
||||
bool EnterSequenceQueue(SequenceQueue &queue, element_type elem) {
|
||||
// 判断队满
|
||||
if (FullSequenceQueue(queue)) {
|
||||
printf("EnterSequenceQueue:队满无法进队!\n");
|
||||
return false;
|
||||
}
|
||||
queue.data[queue.rear++] = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 进循环队
|
||||
bool EnterCircularSequenceQueue(SequenceQueue &queue, element_type elem) {
|
||||
// 判循环队满
|
||||
if (FullCircularSequenceQueue(queue)) {
|
||||
printf("EnterCircularSequenceQueue:队满无法进队!\n");
|
||||
return false;
|
||||
}
|
||||
queue.data[queue.rear] = elem;
|
||||
queue.rear = (queue.rear + 1) % queue.max_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 出队
|
||||
element_type DepartSequenceQueue(SequenceQueue &queue) {
|
||||
// 判断队空
|
||||
if (EmptySequenceQueue(queue)) {
|
||||
printf("DepartSequenceQueue:队空无法出队!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return queue.data[queue.front++];
|
||||
}
|
||||
|
||||
// 出循环队
|
||||
element_type DepartCircularDepartSequence(SequenceQueue &queue) {
|
||||
// 判断队空
|
||||
if (EmptySequenceQueue(queue)) {
|
||||
printf("DepartCircularDepartSequence:队空无法出队!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = queue.data[queue.front];
|
||||
queue.front = (queue.front + 1) % queue.max_size;
|
||||
return temp;
|
||||
}
|
||||
|
||||
// 获取队长
|
||||
int LengthSequenceQueue(SequenceQueue queue) {
|
||||
return (queue.rear - queue.front + queue.max_size) % queue.max_size;
|
||||
}
|
||||
|
||||
// 读队头
|
||||
element_type HeadSequenceQueue(SequenceQueue &queue) {
|
||||
// 判断队空
|
||||
if (EmptySequenceQueue(queue)) {
|
||||
printf("HeadSequenceQueue:队空无法读队头!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return queue.data[queue.front];
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 顺序栈
|
||||
|
||||
130
Code/Code/head/share_stack.h
Normal file
130
Code/Code/head/share_stack.h
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "head.h"
|
||||
|
||||
typedef struct {
|
||||
// 数据
|
||||
element_type *data;
|
||||
// 栈顶指针
|
||||
int top_left, top_right;
|
||||
// 最大容量
|
||||
int max_size;
|
||||
} ShareStack;
|
||||
|
||||
// 初始化
|
||||
bool InitShareStack(ShareStack &stack) {
|
||||
stack.data = (element_type *) malloc(sizeof(element_type) * MAXSIZE);
|
||||
stack.top_left = -1;
|
||||
stack.top_right = MAXSIZE;
|
||||
stack.max_size = MAXSIZE;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InitShareStack(ShareStack &stack, int max_size) {
|
||||
stack.data = (element_type *) malloc(sizeof(element_type) * max_size);
|
||||
stack.top_left = -1;
|
||||
stack.top_right = max_size;
|
||||
stack.max_size = max_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
ShareStack InitShareStack() {
|
||||
auto *stack = (ShareStack *) malloc(sizeof(ShareStack));
|
||||
stack->data = (element_type *) malloc(sizeof(element_type) * MAXSIZE);
|
||||
stack->top_left = -1;
|
||||
stack->top_right = MAXSIZE;
|
||||
stack->max_size = MAXSIZE;
|
||||
return (ShareStack &) stack;
|
||||
}
|
||||
|
||||
ShareStack InitShareStack(int max_size) {
|
||||
auto *stack = (ShareStack *) malloc(sizeof(ShareStack));
|
||||
stack->data = (element_type *) malloc(sizeof(element_type) * max_size);
|
||||
stack->top_left = -1;
|
||||
stack->top_right = max_size;
|
||||
stack->max_size = max_size;
|
||||
return (ShareStack &) stack;
|
||||
}
|
||||
|
||||
// 判左空
|
||||
bool EmptyLeftShareStack(ShareStack stack){
|
||||
return stack.top_left == -1;
|
||||
}
|
||||
|
||||
// 判右空
|
||||
bool EmptyRightShareStack(ShareStack stack){
|
||||
return stack.top_right == stack.max_size;
|
||||
}
|
||||
|
||||
// 判满
|
||||
bool FullShareStack(ShareStack stack){
|
||||
return stack.top_right - stack.top_left == 1;
|
||||
}
|
||||
|
||||
// 左栈长
|
||||
int LengthLeftShareStack(ShareStack stack){
|
||||
return stack.top_left + 1;
|
||||
}
|
||||
|
||||
// 右栈长
|
||||
int LengthRightShareStack(ShareStack stack){
|
||||
return stack.max_size - stack.top_right;
|
||||
}
|
||||
|
||||
// 左进栈
|
||||
bool PushLeftShareStack(ShareStack &stack, element_type elem){
|
||||
if(FullShareStack(stack)){
|
||||
printf("PushLeftShareStack:栈满无法进栈!\n");
|
||||
return false;
|
||||
}
|
||||
stack.data[++stack.top_left] = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 右进栈
|
||||
bool PushRightShareStack(ShareStack &stack, element_type elem){
|
||||
if(FullShareStack(stack)){
|
||||
printf("PushRightShareStack:栈满无法进栈!\n");
|
||||
return false;
|
||||
}
|
||||
stack.data[--stack.top_right] = elem;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 左出栈
|
||||
element_type PopLeftShareStack(ShareStack &stack){
|
||||
if(EmptyLeftShareStack(stack)){
|
||||
printf("PopLeftShareStack:栈空无法出栈!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = stack.data[stack.top_left];
|
||||
stack.top_left--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
// 右出栈
|
||||
element_type PopRightShareStack(ShareStack &stack){
|
||||
if(EmptyLeftShareStack(stack)){
|
||||
printf("PopRightShareStack:栈空无法出栈!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
element_type temp = stack.data[stack.top_right];
|
||||
stack.top_left++;
|
||||
return temp;
|
||||
}
|
||||
|
||||
// 读取左首部
|
||||
element_type TopLeftShareStack(ShareStack stack){
|
||||
if(EmptyLeftShareStack(stack)){
|
||||
printf("PopLeftShareStack:栈空无法出栈!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return stack.data[stack.top_left];
|
||||
}
|
||||
|
||||
// 读取右首部
|
||||
element_type TopRightShareStack(ShareStack stack){
|
||||
if(EmptyLeftShareStack(stack)){
|
||||
printf("TopRightShareStack:栈空无法出栈!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return stack.data[stack.top_right];
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 静态链表
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "../head/static_link_list.h"
|
||||
#include "../head/sequence_stack.h"
|
||||
#include "../head/link_stack.h"
|
||||
#include "../head/share_stack.h"
|
||||
#include "../head/sequence_queue.h"
|
||||
#include "../head/link_queue.h"
|
||||
|
||||
|
||||
@@ -14,24 +14,26 @@
|
||||
|
||||
如果出队,则前面的空间会空闲,但是假如队尾指针会依照插入而不断加$1$,则我们的队尾指针最后会指向最后一个区域,计算机不知道前面是怎么样,所以就认为空间已经满了,实际上没有。这就是假溢出。
|
||||
|
||||
解决的方法就是使用模运算,将队尾指针不仅仅是加一,而是加一后再取整个静态数组大小$MAXSIZE$的模,这样如果队列尾指针超过了范围也仍能回到最开始插入数据。这时候物理结构虽然是线性的,而逻辑上已经变成了环型的了。
|
||||
|
||||
所以与此同时,队列已满的条件也不再是队尾指针$=MAXSIZE$了,而是队尾指针的下一个指针是队头指针,这里最后一个存储单元是不能存储数据的,因为如果存储了数据那么头指针就等于尾指针,这在我们的定义中是空队列的意思,会混淆,所以必须牺牲一个存储单元。
|
||||
|
||||
如果我们最开始定义时,让队首指针指向$-1$,队尾指针指向$0$,则可以相等。
|
||||
|
||||
从而队列元素个数$=(rear+MAXSIZE-front)\%MAXSIZE$。
|
||||
|
||||
#### 顺序队列删除
|
||||
|
||||
当如果我们必须保证所有的存储空间被利用,可以定义一个$size$表明队列当前的长度,就可以完全利用所有空间。
|
||||
|
||||
同理我们可以定义一个$int$类型的$tag$,当进行删除操作就置$tag$为$0$,插入操作时置$tag$为$1$,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。
|
||||
|
||||
## 循环队列
|
||||
|
||||
对于顺序队列假溢出的解决的方法就是使用模运算,将队尾指针不仅仅是加一,而是加一后再取整个静态数组大小$MAXSIZE$的模,这样如果队列尾指针超过了范围也仍能回到最开始插入数据。这时候物理结构虽然是线性的,而逻辑上已经变成了环型的了。
|
||||
|
||||
所以与此同时,队列已满的条件也不再是队尾指针$=MAXSIZE$了,而是队尾指针的下一个指针是队头指针,这里最后一个存储单元是不能存储数据的,因为如果存储了数据那么头指针就等于尾指针,这在我们的定义中是空队列的意思。但是如何判断满队?
|
||||
|
||||
1. 牺牲最后的一个存储单元。入队少用一个队列单元。队满条件:$(rear+1)\%MAXSIZE==front$,队空条件$front==rear$,从而队列元素个数$=(rear+MAXSIZE-front)\%MAXSIZE$。
|
||||
2. 增设一个用来表示数据个数的成员。队满条件$length==MAXSIZE$,队空条件$length==0$。队空和队满都是$front==rear$。
|
||||
3. 可以定义一个$int$类型的$tag$,当进行删除操作就置$tag$为$0$,插入操作时置$tag$为$1$,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。队空和队满都是$front==rear$。
|
||||
|
||||
## 链队
|
||||
|
||||
定义链队需要定义一个队头指针和一个队尾指针,队头指向队头结点,队尾指向队尾结点,即单链表最后一个结点,这跟顺序存储不同。
|
||||
|
||||
由于带头节点的链表对于出队比较简单,所以一般都定义为带头节点的链表。
|
||||
|
||||
## 双端队列
|
||||
|
||||
双端队列:只允许从两端插入、两端删除的线性表。
|
||||
|
||||
Reference in New Issue
Block a user