1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-06-16 23:17:21 +08:00

更新数据结构

This commit is contained in:
Didnelpsun
2021-09-15 23:47:38 +08:00
parent 028cf20dde
commit b1998ca91c
13 changed files with 1059 additions and 465 deletions

View File

@@ -1,4 +1,4 @@
#include <cstdlib>
#include <cstdlib>
#include <iostream>
#include "head.h"
@@ -16,71 +16,56 @@ private:
int _length{};
public:
// 设置数据
bool SetData(element_type* elem);
bool SetData(element_type *elem);
bool SetData(int index, element_type elem);
// 获取数据
element_type* GetData() const;
element_type *GetData() const;
element_type GetData(int index) const;
// 长度自加1
bool SetLength();
// 设置长度
bool SetLength(int length);
// 设置长度
int GetLength() const;
// 构造函数
SequenceList();
// 插入函数
virtual bool Insert(int index, element_type elem) = 0;
// 打印函数
bool Print() const;
// 循环插入函数
bool LoopInsert(element_type *elem, int index, int length);
// 删除函数
element_type Delete(int index);
// 多个删除函数
element_type* LoopDelete(int index, int length);
element_type *LoopDelete(int index, int length);
// 按位获取元素
element_type GetElem(int index) const;
// 按值获取元素
int Locate(element_type elem) const;
// 判空
bool Empty() const;
// 销毁
bool Destroy() const;
};
// 静态顺序表
class StaticSequenceList: public SequenceList{
public:
// 构造函数
StaticSequenceList();
// 插入函数
bool Insert(int index, element_type elem) override;
};
// 动态顺序表
class DynamicSequenceList: public SequenceList{
private:
// 已分配的最大容量
int _max_size{};
public:
// 设置最大容量
bool SetMaxSize(int max_size);
// 获取最大容量
int GetMaxSize() const;
// 构造函数
DynamicSequenceList();
// 插入函数
bool Insert(int index, element_type elem) override;
private:
// 分配其他地址增长动态顺序表的数据空间长度
bool OtherIncrease(int len);
// 重新分配地址增长动态顺序表的数据空间长度
bool ReIncrease(int len);
};
bool SequenceList::SetData(element_type* elem) {
bool SequenceList::SetData(element_type *elem) {
this->_data = elem;
return true;
}
@@ -99,7 +84,7 @@ element_type SequenceList::GetData(int index) const {
}
bool SequenceList::SetLength() {
this->_length ++;
this->_length++;
return true;
}
@@ -116,33 +101,6 @@ SequenceList::SequenceList() {
this->SetLength(0);
}
StaticSequenceList::StaticSequenceList() : SequenceList() {
this->SetData((element_type*)malloc(MAXSIZE * sizeof(element_type)));
}
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 = (element_type*)malloc(MAXSIZE * sizeof(element_type));
if (space) {
this->SetData(space);
this->SetMaxSize(MAXSIZE);
}
else {
cout << "InitSequenceList:分配空间失败!" << endl;
}
}
bool SequenceList::Print() const {
for (int i = 0; i < this->GetLength(); i++) {
cout << "" << i + 1 << "个元素值为" << this->GetData(i) << endl;
@@ -150,94 +108,6 @@ bool SequenceList::Print() const {
return true;
}
bool DynamicSequenceList::OtherIncrease(int len) {
if (len <= 0) {
cout << "OtherIncrease:申请空间应该大于0" << endl;
return false;
}
// 申请一片连续的存储空间
int new_length = this->GetMaxSize() + len;
auto* space = (element_type*)malloc(new_length * sizeof(element_type));
if (space) {
// 建立中间变量
this->SetData(space);
element_type* temp = this->GetData();
for (int i = 0; i < this->GetLength(); i++) {
this->SetData(i, temp[i]);
}
this->SetMaxSize(new_length);
free(temp);
return true;
}
else {
cout << "OtherIncrease:重新分配空间失败!" << endl;
return false;
}
}
bool DynamicSequenceList::ReIncrease(int length) {
if (length <= 0) {
cout << "ReIncrease:申请空间应该大于0" << endl;
return false;
}
// 申请一片连续的存储空间
int new_length = this->GetMaxSize() + length;
auto* space = (element_type*)realloc(this->GetData(), new_length * sizeof(element_type));
if (space) {
this->SetData(space);
this->SetMaxSize(this->GetMaxSize()+length);
return true;
}
else {
this->SetMaxSize(0);
this->SetLength(0);
cout << "ReIncrease:分配其他地址空间失败!" << endl;
return false;
}
}
bool StaticSequenceList::Insert(int index, element_type elem) {
// 当静态顺序表已经满了就不能插入任何元素
if (this->GetLength() >= MAXSIZE) {
cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
return false;
}
// 索引位置从0开始所以可以插入的范围是0到list->length
if (index > this->GetLength() || index < 0) {
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
return false;
}
// 从最后一个元素开始交换后移list->length是空的
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i-1));
}
this->SetData(index, elem);
this->SetLength();
return true;
}
bool DynamicSequenceList::Insert(int index, element_type elem) {
if (index > this->GetLength() || index < 0) {
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
return false;
}
// 当动态顺序表已经满了,需要新增一个位置
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
if (this->GetLength() >= MAXSIZE) {
bool result = this->ReIncrease(1);
if (!result) {
cout << "Insert:申请空间失败!" << endl;
return false;
}
}
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i-1));
}
this->SetData(index, elem);
this->SetLength();
return true;
}
bool SequenceList::LoopInsert(element_type *elem, int index, int length) {
for (int i = 0; i < length; i++) {
bool result = this->Insert(i, elem[i + index]);
@@ -255,18 +125,18 @@ element_type SequenceList::Delete(int index) {
return false;
}
for (int i = index; i < this->GetLength(); i++) {
this->SetData(i, this->GetData(i+1));
this->SetData(i, this->GetData(i + 1));
}
this->SetLength(this->GetLength()-1);
this->SetLength(this->GetLength() - 1);
return this->GetData(index);
}
element_type* SequenceList::LoopDelete(int index, int length) {
element_type *SequenceList::LoopDelete(int index, int length) {
if (index + length > this->GetLength() || index < 0) {
cout << "LoopDelete:删除索引" << index + length << "超过索引范围!" << endl;
return nullptr;
}
auto* elem = (element_type*)malloc(length * sizeof(element_type));
auto *elem = new element_type[length];
if (elem) {
for (int i = index; i <= this->GetLength() - length; i++) {
if (i < index + length) {
@@ -275,8 +145,7 @@ element_type* SequenceList::LoopDelete(int index, int length) {
this->SetData(i, this->GetData(i + length));
}
this->SetLength(this->GetLength() - length);
}
else {
} else {
cout << "LoopDelete:申请空间失败!" << endl;
}
return elem;
@@ -285,7 +154,7 @@ element_type* SequenceList::LoopDelete(int index, int length) {
element_type SequenceList::GetElem(int index) const {
if (index >= this->GetLength() || index < 0) {
cout << "GetElem:查找索引" << index << "超过索引范围!" << endl;
return DEFAULTELEM;
return NULL;
}
return this->GetData(index);
}
@@ -302,12 +171,7 @@ int SequenceList::Locate(element_type elem) const {
}
bool SequenceList::Empty() const {
if (this->GetLength() == 0) {
return false;
}
else {
return true;
}
return this->GetLength() == 0;
}
bool SequenceList::Destroy() const {
@@ -317,3 +181,150 @@ bool SequenceList::Destroy() const {
return true;
}
// 静态顺序表
class StaticSequenceList : public SequenceList {
public:
// 构造函数
StaticSequenceList();
// 插入函数
bool Insert(int index, element_type elem) override;
};
StaticSequenceList::StaticSequenceList() : SequenceList() {
this->SetData(new element_type[MAXSIZE]);
}
bool StaticSequenceList::Insert(int index, element_type elem) {
// 当静态顺序表已经满了就不能插入任何元素
if (this->GetLength() >= MAXSIZE) {
cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
return false;
}
// 索引位置从0开始所以可以插入的范围是0到list->length
if (index > this->GetLength() || index < 0) {
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
return false;
}
// 从最后一个元素开始交换后移list->length是空的
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i - 1));
}
this->SetData(index, elem);
this->SetLength();
return true;
}
// 动态顺序表
class DynamicSequenceList : public SequenceList {
private:
// 已分配的最大容量
int _max_size{};
public:
// 设置最大容量
bool SetMaxSize(int max_size);
// 获取最大容量
int GetMaxSize() const;
// 构造函数
DynamicSequenceList();
// 插入函数
bool Insert(int index, element_type elem) override;
private:
// 分配其他地址增长动态顺序表的数据空间长度
bool OtherIncrease(int len);
// 重新分配地址增长动态顺序表的数据空间长度
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];
if (space) {
this->SetData(space);
this->SetMaxSize(MAXSIZE);
} else {
cout << "SequenceList:分配空间失败!" << endl;
}
}
bool DynamicSequenceList::OtherIncrease(int len) {
if (len <= 0) {
cout << "OtherIncrease:申请空间应该大于0" << endl;
return false;
}
// 申请一片连续的存储空间
int new_length = this->GetMaxSize() + len;
auto *space = new element_type[new_length];
if (space) {
// 建立中间变量
this->SetData(space);
element_type *temp = this->GetData();
for (int i = 0; i < this->GetLength(); i++) {
this->SetData(i, temp[i]);
}
this->SetMaxSize(new_length);
free(temp);
return true;
} else {
cout << "OtherIncrease:重新分配空间失败!" << endl;
return false;
}
}
bool DynamicSequenceList::ReIncrease(int length) {
if (length <= 0) {
cout << "ReIncrease:申请空间应该大于0" << endl;
return false;
}
// 申请一片连续的存储空间
int new_length = this->GetMaxSize() + length;
auto *space = (element_type *) realloc(this->GetData(), new_length * sizeof(element_type));
if (space) {
this->SetData(space);
this->SetMaxSize(this->GetMaxSize() + length);
return true;
} else {
this->SetMaxSize(0);
this->SetLength(0);
cout << "ReIncrease:分配其他地址空间失败!" << endl;
return false;
}
}
bool DynamicSequenceList::Insert(int index, element_type elem) {
if (index > this->GetLength() || index < 0) {
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
return false;
}
// 当动态顺序表已经满了,需要新增一个位置
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
if (this->GetLength() >= MAXSIZE) {
bool result = this->ReIncrease(1);
if (!result) {
cout << "Insert:申请空间失败!" << endl;
return false;
}
}
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i - 1));
}
this->SetData(index, elem);
this->SetLength();
return true;
}