1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-11-02 05:13:43 +08:00
This commit is contained in:
Didnelpsun
2021-10-23 23:25:29 +08:00
parent b188b2e65e
commit 94c62a2082
14 changed files with 734 additions and 494 deletions

View File

@@ -0,0 +1,87 @@
#include "sequence_list.h"
// 动态顺序表
class DynamicSequenceList : public SequenceList {
public:
// 构造函数
DynamicSequenceList();
explicit DynamicSequenceList(int max_size);
// 插入函数
bool Insert(int index, element_type elem) override;
private:
// 分配其他地址增长动态顺序表的数据空间长度
bool OtherIncrease(int len);
// 重新分配地址增长动态顺序表的数据空间长度
bool ReIncrease(int len);
};
DynamicSequenceList::DynamicSequenceList() : SequenceList() {
}
DynamicSequenceList::DynamicSequenceList(int max_size) : SequenceList(max_size) {
}
bool DynamicSequenceList::OtherIncrease(int length) {
if (length <= 0) {
// cout << "OtherIncrease:申请空间应该大于0!" << endl;
cout << "OtherIncrease:The length " << length << " should larger than 0!" << endl;
return false;
}
// 申请一片连续的存储空间
int new_length = this->GetMaxSize() + length;
auto *space = new element_type[new_length];
// 建立中间变量
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);
// delete(temp);
return true;
}
bool DynamicSequenceList::ReIncrease(int length) {
if (length <= 0) {
// cout << "ReIncrease:申请空间应该大于0!" << endl;
cout << "ReIncrease:The length " << length << " should larger than 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;
cout << "Insert:Insert index value " << index << " is out of range!" << endl;
return false;
}
// 当动态顺序表已经满了,需要新增一个位置
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
if (this->GetLength() >= this->GetMaxSize()) {
this->ReIncrease(1);
}
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i - 1));
}
this->SetData(index, elem);
this->SetLength();
return true;
}

View File

@@ -1,3 +1,7 @@
#ifndef _LINK_LIST_
#define _LINK_LIST_
#include <iostream>
#include "head.h"
using namespace std;
@@ -265,341 +269,4 @@ bool LinkList::Destroy() {
return true;
}
class LinkListWithHead : public LinkList {
public:
// 构造函数
LinkListWithHead();
// 打印
bool Print() override;
// 插入
bool Insert(int index, element_type elem) override;
// 删除
element_type *Delete(int index, int length) override;
};
LinkListWithHead::LinkListWithHead() {
this->SetType(true);
};
bool LinkListWithHead::Print() {
int i = 1;
// cout << "第0个元素值为空" << endl;
cout << "index: 0 -> value: NULL" << endl;
if (this->GetLength() == 0) {
return true;
}
// 当前遍历指针
LinkListNode *node = this->GetNext();
while (node != nullptr) {
// cout << "第" << i << "个元素值为" << node->GetData() << endl;
cout << "index: " << i << " -> value: " << node->GetData() << endl;
i++;
node = node->GetNext();
}
return true;
}
bool LinkListWithHead::Insert(int index, element_type elem) {
if (index < 1) {
// cout << "Insert:插入索引值" << index << "过小!" << endl;
cout << "Insert:Insert index value " << index << " is too small!" << endl;
return false;
}
// 定义一个结点指针p指向当前扫描到的结点
LinkListNode *node;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向node,为第1个结点
node = this->GetNext();
// 设置一个新结点进行插入
auto *new_node = new LinkListNode(elem);
// 如果该链表为空链表
if (node == nullptr) {
this->SetNext(new_node);
this->SetLength();
return true;
}
// 当插入的是第一个节点
if (index == 1) {
new_node->SetNext(node);
this->SetNext(new_node);
this->SetLength();
return true;
}
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (node->GetNext() != nullptr && i < index - 1) {
node = node->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1) {
// cout << "Insert:插入索引值" << index << "过大!" << endl;
cout << "Insert:Insert index value" << index << " is too large!";
return false;
}
// 此时i==index-1
// 将node原来的后继给新的结点
new_node->SetNext(node->GetNext());
node->SetNext(new_node);
this->SetLength();
return true;
}
element_type *LinkListWithHead::Delete(int index, int length) {
auto *data = new element_type[length];
if (index < 1) {
// cout << "Delete:删除索引值" << index << "过小!" << endl;
cout << "Delete:Delete index value " << index << " is too small!" << endl;
return data;
}
if (length < 1) {
// cout << "Delete:删除长度" << length << "过小!" << endl;
cout << "Delete:Delete length value " << length << " is too small!" << endl;
return data;
}
// 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个
LinkListNode *start;
// 定义一个结点指针start指向当前扫描到的结点,要删除最后的元素
LinkListNode *end;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向start,为第1个结点
start = this->GetNext();
// 如果链表没有任何数据
if (start == nullptr) {
// cout << "Delete:链表为空!" << endl;
cout << "Delete:Link list is empty!" << endl;
return data;
}
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (start->GetNext() != nullptr && i < index - 1) {
start = start->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1) {
// cout << "Delete:删除索引值" << index << "过大!" << endl;
cout << "Delete:Delete index value " << index << " is too large!" << endl;
return data;
}
// 此时i==index-1,start到达,求end
end = start;
for (int i = 0; i < length; i++) {
data[i] = end->GetData();
end = end->GetNext();
if (end == nullptr) {
// cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length - 1 << "!" << endl;
cout << "Delete:Delete index value" << index + length -1 << "is larger than link list's biggest index " << length - 1 << "!" << endl;
return data;
}
}
if (index == 1) {
this->SetNext(end);
} else {
start->SetNext(end->GetNext());
}
this->SetLength(this->GetLength() - length);
return data;
}
class LinkListWithoutHead : public LinkList {
private:
// 数据
element_type _data{};
public:
// 设置数据
bool SetData(element_type elem);
// 获取数据
element_type GetData() const;
// 构造函数
LinkListWithoutHead();
// 打印
bool Print() override;
// 插入
bool Insert(int index, element_type elem) override;
// 删除
element_type *Delete(int index, int length) override;
// 按位查找
element_type GetElem(int index) override;
// 按值查找
int Locate(element_type elem) override;
// 销毁
bool Destroy() override;
};
bool LinkListWithoutHead::SetData(element_type elem) {
this->_data = elem;
return true;
}
element_type LinkListWithoutHead::GetData() const {
return this->_data;
}
LinkListWithoutHead::LinkListWithoutHead() {
this->SetType(false);
this->SetData(DEFAULTELEM);
}
bool LinkListWithoutHead::Print() {
int i = 0;
if (this->GetLength() == 0) {
return true;
}
// cout << "第" << i << "个元素值为" << this->GetData() << endl;
cout << "index: " << i << " -> value: " << this->GetData() << endl;
// 当前遍历指针
LinkListNode *node = this->GetNext();
while (node != nullptr) {
i++;
// cout << "第" << i << "个元素值为" << node->GetData() << endl;
cout << "index: " << i << " -> value: " << node->GetData() << endl;
node = node->GetNext();
}
return true;
}
bool LinkListWithoutHead::Insert(int index, element_type elem) {
if (index < 0) {
// cout << "Insert:插入索引值" << index << "过小!" << endl;
cout << "Insert:Insert index value " << index << " is too small!" << endl;
return false;
}
if (index == 0) {
if (this->GetLength() == 0) {
this->SetData(elem);
this->SetLength();
} else {
auto *node = new LinkListNode(this->GetData());
node->SetNext(this->GetNext());
this->SetData(elem);
this->SetNext(node);
this->SetLength();
}
return true;
}
// 定义一个结点指针node指向当前扫描到的结点
LinkListNode *node;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向p,为第1个结点
node = this->GetNext();
// 设置一个新结点进行插入
auto *new_node = new LinkListNode(elem);
// 如果该链表为空链表
if (node == nullptr) {
this->SetLength();
this->SetNext(new_node);
return true;
}
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (node->GetNext() != nullptr && i < index - 1) {
node = node->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1) {
// cout << "Insert:插入索引值" << index << "过大!" << endl;
cout << "Insert:Insert index value" << index << " is too large!";
return false;
}
// 此时i==index-1
// 将p原来的后继给新的结点
new_node->SetNext(node->GetNext());
node->SetNext(new_node);
this->SetLength();
return true;
}
element_type *LinkListWithoutHead::Delete(int index, int length) {
auto *data = new element_type[length];
if (index < 0) {
// cout << "Delete:删除索引值" << index << "过小!" << endl;
cout << "Delete:Delete index value " << index << " is too small!";
return data;
}
if (length < 1) {
// cout << "Delete:删除长度" << length << "过小!" << endl;
cout << "Delete:Delete length value " << length << " is too small!";
return data;
}
// 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个
LinkListNode *start;
// 定义一个结点指针start指向当前扫描到的结点,要删除最后的元素
LinkListNode *end;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向start,为第1个结点
start = this->GetNext();
// 如果链表没有任何数据
if (this->GetData() == DEFAULTELEM) {
// cout << "Delete:链表为空!" << endl;
cout << "Delete:Link list is empty!" << endl;
return data;
}
data[0] = this->GetData();
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (start->GetNext() != nullptr && i < index - 1) {
start = start->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1) {
// cout << "Delete:删除索引值" << index << "过大!" << endl;
cout << "Delete:Delete index value " << index << " is too large!";
return data;
}
// 从1开始遍历
end = this->GetNext();
for (int i = 1; i < index + length - 1; i++) {
if (i >= index) {
data[i - index] = end->GetData();
}
end = end->GetNext();
if (end == nullptr) {
// cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length - 1 << "!" << endl;
cout << "Delete:Delete index value" << index + length -1 << "is larger than link list's biggest index " << length - 1 << "!" << endl;
return data;
}
}
data[length - 1] = end->GetData();
if (index == 0) {
this->SetData(end->GetNext()->GetData());
this->SetNext(end->GetNext()->GetNext());
}
if (index == 1) {
this->SetNext(end->GetNext());
} else {
start->SetNext(end->GetNext());
}
this->SetLength(this->GetLength() - length);
return data;
}
element_type LinkListWithoutHead::GetElem(int index) {
return index == 0 ? this->GetData() : LinkList::GetElem(index);
}
int LinkListWithoutHead::Locate(element_type elem) {
return this->GetData() == elem ? 0 : LinkList::Locate(elem);
}
bool LinkListWithoutHead::Destroy() {
this->SetData(DEFAULTELEM);
return LinkList::Destroy();
}
#endif

View File

@@ -0,0 +1,229 @@
#include "link_list.h"
class LinkListWithHead : public LinkList
{
public:
// 构造函数
LinkListWithHead();
// 获取数据
element_type GetData(int index);
// 打印
bool Print() override;
// 插入
bool Insert(int index, element_type elem) override;
// 删除
element_type *Delete(int index, int length) override;
// 获取最大值
int Max();
// 获取最小值
int Min();
};
LinkListWithHead::LinkListWithHead()
{
this->SetType(true);
};
element_type LinkListWithHead::GetData(int index)
{
if (index <= 0 || index >= this->GetLength() || this->GetLength() == 0)
{
cout << "GetData:Can't get the element by index of " << index << " !" << endl;
return DEFAULTELEM;
}
LinkListNode *node = this->GetNext();
for (int i = 1; i < index; i++)
{
node = node->GetNext();
}
return node->GetData();
}
bool LinkListWithHead::Print()
{
int i = 1;
// cout << "第0个元素值为空" << endl;
cout << "index: 0 -> value: NULL" << endl;
if (this->GetLength() == 0)
{
return true;
}
// 当前遍历指针
LinkListNode *node = this->GetNext();
while (node != nullptr)
{
// cout << "第" << i << "个元素值为" << node->GetData() << endl;
cout << "index: " << i << " -> value: " << node->GetData() << endl;
i++;
node = node->GetNext();
}
return true;
}
bool LinkListWithHead::Insert(int index, element_type elem)
{
if (index < 1)
{
// cout << "Insert:插入索引值" << index << "过小!" << endl;
cout << "Insert:Insert index value " << index << " is too small!" << endl;
return false;
}
// 定义一个结点指针p指向当前扫描到的结点
LinkListNode *node;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向node,为第1个结点
node = this->GetNext();
// 设置一个新结点进行插入
auto *new_node = new LinkListNode(elem);
// 如果该链表为空链表
if (node == nullptr)
{
this->SetNext(new_node);
this->SetLength();
return true;
}
// 当插入的是第一个节点
if (index == 1)
{
new_node->SetNext(node);
this->SetNext(new_node);
this->SetLength();
return true;
}
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (node->GetNext() != nullptr && i < index - 1)
{
node = node->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1)
{
// cout << "Insert:插入索引值" << index << "过大!" << endl;
cout << "Insert:Insert index value" << index << " is too large!";
return false;
}
// 此时i==index-1
// 将node原来的后继给新的结点
new_node->SetNext(node->GetNext());
node->SetNext(new_node);
this->SetLength();
return true;
}
element_type *LinkListWithHead::Delete(int index, int length)
{
auto *data = new element_type[length];
if (index < 1)
{
// cout << "Delete:删除索引值" << index << "过小!" << endl;
cout << "Delete:Delete index value " << index << " is too small!" << endl;
return data;
}
if (length < 1)
{
// cout << "Delete:删除长度" << length << "过小!" << endl;
cout << "Delete:Delete length value " << length << " is too small!" << endl;
return data;
}
// 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个
LinkListNode *start;
// 定义一个结点指针start指向当前扫描到的结点,要删除最后的元素
LinkListNode *end;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向start,为第1个结点
start = this->GetNext();
// 如果链表没有任何数据
if (start == nullptr)
{
// cout << "Delete:链表为空!" << endl;
cout << "Delete:Link list is empty!" << endl;
return data;
}
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (start->GetNext() != nullptr && i < index - 1)
{
start = start->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1)
{
// cout << "Delete:删除索引值" << index << "过大!" << endl;
cout << "Delete:Delete index value " << index << " is too large!" << endl;
return data;
}
// 此时i==index-1,start到达,求end
end = start;
for (int i = 0; i < length; i++)
{
data[i] = end->GetData();
end = end->GetNext();
if (end == nullptr)
{
// cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length - 1 << "!" << endl;
cout << "Delete:Delete index value" << index + length - 1 << "is larger than link list's biggest index " << length - 1 << "!" << endl;
return data;
}
}
if (index == 1)
{
this->SetNext(end);
}
else
{
start->SetNext(end->GetNext());
}
this->SetLength(this->GetLength() - length);
return data;
}
int LinkListWithHead::Max()
{
if (this->GetLength() == 0)
{
cout << "Max:The list is empty!" << endl;
return -1;
}
element_type temp = this->GetData(1);
int index = 1;
for (int i = 1; i < this->GetLength(); i++)
{
if (this->GetData(i) > temp)
{
temp = this->GetData(i);
index = i;
}
}
return index;
}
int LinkListWithHead::Min()
{
if (this->GetLength() == 0)
{
cout << "Min:The list is empty!" << endl;
return -1;
}
element_type temp = this->GetData(1);
int index = 1;
for (int i = 1; i < this->GetLength(); i++)
{
if (this->GetData(i) < temp)
{
temp = this->GetData(i);
index = i;
}
}
return index;
}

View File

@@ -0,0 +1,296 @@
#include "link_list.h"
class LinkListWithoutHead : public LinkList
{
private:
// 数据
element_type _data{};
public:
// 设置数据
bool SetData(element_type elem);
// 获取数据
element_type GetData() const;
element_type GetData(int index);
// 构造函数
LinkListWithoutHead();
// 打印
bool Print() override;
// 插入
bool Insert(int index, element_type elem) override;
// 删除
element_type *Delete(int index, int length) override;
// 按位查找
element_type GetElem(int index) override;
// 按值查找
int Locate(element_type elem) override;
// 销毁
bool Destroy() override;
// 获取最大值
int Max();
// 获取最小值
int Min();
};
bool LinkListWithoutHead::SetData(element_type elem)
{
this->_data = elem;
return true;
}
element_type LinkListWithoutHead::GetData() const
{
return this->_data;
}
element_type LinkListWithoutHead::GetData(int index)
{
if (index < 0 || index >= this->GetLength() || this->GetLength() == 0)
{
cout << "GetData:Can't get the element by index of " << index << " !" << endl;
return DEFAULTELEM;
}
if (index == 0)
return this->GetData();
LinkListNode *node = this->GetNext();
for (int i = 1; i < index; i++)
{
node = node->GetNext();
}
return node->GetData();
}
LinkListWithoutHead::LinkListWithoutHead()
{
this->SetType(false);
this->SetData(DEFAULTELEM);
}
bool LinkListWithoutHead::Print()
{
int i = 0;
if (this->GetLength() == 0)
{
return true;
}
// cout << "第" << i << "个元素值为" << this->GetData() << endl;
cout << "index: " << i << " -> value: " << this->GetData() << endl;
// 当前遍历指针
LinkListNode *node = this->GetNext();
while (node != nullptr)
{
i++;
// cout << "第" << i << "个元素值为" << node->GetData() << endl;
cout << "index: " << i << " -> value: " << node->GetData() << endl;
node = node->GetNext();
}
return true;
}
bool LinkListWithoutHead::Insert(int index, element_type elem)
{
if (index < 0)
{
// cout << "Insert:插入索引值" << index << "过小!" << endl;
cout << "Insert:Insert index value " << index << " is too small!" << endl;
return false;
}
if (index == 0)
{
if (this->GetLength() == 0)
{
this->SetData(elem);
this->SetLength();
}
else
{
auto *node = new LinkListNode(this->GetData());
node->SetNext(this->GetNext());
this->SetData(elem);
this->SetNext(node);
this->SetLength();
}
return true;
}
// 定义一个结点指针node指向当前扫描到的结点
LinkListNode *node;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向p,为第1个结点
node = this->GetNext();
// 设置一个新结点进行插入
auto *new_node = new LinkListNode(elem);
// 如果该链表为空链表
if (node == nullptr)
{
this->SetLength();
this->SetNext(new_node);
return true;
}
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (node->GetNext() != nullptr && i < index - 1)
{
node = node->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1)
{
// cout << "Insert:插入索引值" << index << "过大!" << endl;
cout << "Insert:Insert index value" << index << " is too large!";
return false;
}
// 此时i==index-1
// 将p原来的后继给新的结点
new_node->SetNext(node->GetNext());
node->SetNext(new_node);
this->SetLength();
return true;
}
element_type *LinkListWithoutHead::Delete(int index, int length)
{
auto *data = new element_type[length];
if (index < 0)
{
// cout << "Delete:删除索引值" << index << "过小!" << endl;
cout << "Delete:Delete index value " << index << " is too small!";
return data;
}
if (length < 1)
{
// cout << "Delete:删除长度" << length << "过小!" << endl;
cout << "Delete:Delete length value " << length << " is too small!";
return data;
}
// 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个
LinkListNode *start;
// 定义一个结点指针start指向当前扫描到的结点,要删除最后的元素
LinkListNode *end;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向start,为第1个结点
start = this->GetNext();
// 如果链表没有任何数据
if (this->GetData() == DEFAULTELEM)
{
// cout << "Delete:链表为空!" << endl;
cout << "Delete:Link list is empty!" << endl;
return data;
}
data[0] = this->GetData();
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (start->GetNext() != nullptr && i < index - 1)
{
start = start->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1)
{
// cout << "Delete:删除索引值" << index << "过大!" << endl;
cout << "Delete:Delete index value " << index << " is too large!";
return data;
}
// 从1开始遍历
end = this->GetNext();
for (int i = 1; i < index + length - 1; i++)
{
if (i >= index)
{
data[i - index] = end->GetData();
}
end = end->GetNext();
if (end == nullptr)
{
// cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length - 1 << "!" << endl;
cout << "Delete:Delete index value" << index + length - 1 << "is larger than link list's biggest index " << length - 1 << "!" << endl;
return data;
}
}
data[length - 1] = end->GetData();
if (index == 0)
{
this->SetData(end->GetNext()->GetData());
this->SetNext(end->GetNext()->GetNext());
}
if (index == 1)
{
this->SetNext(end->GetNext());
}
else
{
start->SetNext(end->GetNext());
}
this->SetLength(this->GetLength() - length);
return data;
}
element_type LinkListWithoutHead::GetElem(int index)
{
return index == 0 ? this->GetData() : LinkList::GetElem(index);
}
int LinkListWithoutHead::Locate(element_type elem)
{
return this->GetData() == elem ? 0 : LinkList::Locate(elem);
}
bool LinkListWithoutHead::Destroy()
{
this->SetData(DEFAULTELEM);
return LinkList::Destroy();
}
int LinkListWithoutHead::Max()
{
if (this->GetLength() == 0)
{
cout << "Max:The list is empty!" << endl;
return -1;
}
element_type temp = this->GetData(0);
int index = 0;
for (int i = 1; i < this->GetLength(); i++)
{
if (this->GetData(i) > temp)
{
temp = this->GetData(i);
index = i;
}
}
return index;
}
int LinkListWithoutHead::Min()
{
if (this->GetLength() == 0)
{
cout << "Min:The list is empty!" << endl;
return -1;
}
element_type temp = this->GetData(0);
int index = 0;
for (int i = 1; i < this->GetLength(); i++)
{
if (this->GetData(i) < temp)
{
temp = this->GetData(i);
index = i;
}
}
return index;
}

View File

@@ -0,0 +1,6 @@
#include "static_sequence_list.h"
#include "dynamic_sequence_list.h"
#include "link_list_with_head.h"
#include "link_list_without_head.h"
#include "static_link_list.h"
#include "double_link_list.h"

View File

@@ -1,8 +1,11 @@
#ifndef _SEQUENCE_LIST_
#define _SEQUENCE_LIST_
// 避免重复编译
#include <iostream>
#include "head.h"
#pragma warning(disable:6385)
#pragma warning(disable:6386)
//#pragma warning(disable:6385)
//#pragma warning(disable:6386)
using namespace std;
@@ -15,6 +18,12 @@ private:
int _length{};
// 最大容量
int _max_size{};
protected:
// 构造函数
SequenceList();
explicit SequenceList(int max_size);
public:
// 设置数据
bool SetData();
@@ -47,11 +56,6 @@ public:
// 获取最大容量
int GetMaxSize() const;
// 构造函数
SequenceList();
explicit SequenceList(int max_size);
// 插入函数
virtual bool Insert(int index, element_type elem) = 0;
@@ -78,6 +82,12 @@ public:
// 销毁
bool Destroy() const;
// 获取最大值
int Max() const;
// 获取最小值
int Min() const;
};
bool SequenceList::SetData() {
@@ -230,129 +240,36 @@ bool SequenceList::Destroy() const {
return true;
}
// 静态顺序表
class StaticSequenceList : public SequenceList {
public:
// 构造函数
StaticSequenceList();
explicit StaticSequenceList(int max_size);
// 插入函数
bool Insert(int index, element_type elem) override;
};
StaticSequenceList::StaticSequenceList() : SequenceList() {
int SequenceList::Max() const {
if(this->GetLength()==0){
cout << "Max:The list is empty!" << endl;
return -1;
}
element_type temp = this->GetData(0);
int index = 0;
for(int i=1;i<this->GetLength();i++){
if(this->GetData(i)>temp){
temp = this->GetData(i);
index = i;
}
}
return index;
}
StaticSequenceList::StaticSequenceList(int max_size) : SequenceList(max_size) {
int SequenceList::Min() const {
if(this->GetLength()==0){
cout << "Min:The list is empty!" << endl;
return -1;
}
element_type temp = this->GetData(0);
int index = 0;
for(int i=1;i<this->GetLength();i++){
if(this->GetData(i)<temp){
temp = this->GetData(i);
index = i;
}
}
return index;
}
bool StaticSequenceList::Insert(int index, element_type elem) {
// 当静态顺序表已经满了就不能插入任何元素
if (this->GetLength() >= this->GetMaxSize()) {
// cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
cout << "Insert:The space size of " << this->GetMaxSize() << " is not enough!" << endl;
return false;
}
// 索引位置从0开始,所以可以插入的范围是0到list->length
if (index > this->GetLength() || index < 0) {
// cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
cout << "Insert:Insert index value " << index << " is out of range!" << 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 {
public:
// 构造函数
DynamicSequenceList();
explicit DynamicSequenceList(int max_size);
// 插入函数
bool Insert(int index, element_type elem) override;
private:
// 分配其他地址增长动态顺序表的数据空间长度
bool OtherIncrease(int len);
// 重新分配地址增长动态顺序表的数据空间长度
bool ReIncrease(int len);
};
DynamicSequenceList::DynamicSequenceList() : SequenceList() {
}
DynamicSequenceList::DynamicSequenceList(int max_size) : SequenceList(max_size) {
}
bool DynamicSequenceList::OtherIncrease(int length) {
if (length <= 0) {
// cout << "OtherIncrease:申请空间应该大于0!" << endl;
cout << "OtherIncrease:The length " << length << " should larger than 0!" << endl;
return false;
}
// 申请一片连续的存储空间
int new_length = this->GetMaxSize() + length;
auto *space = new element_type[new_length];
// 建立中间变量
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);
// delete(temp);
return true;
}
bool DynamicSequenceList::ReIncrease(int length) {
if (length <= 0) {
// cout << "ReIncrease:申请空间应该大于0!" << endl;
cout << "ReIncrease:The length " << length << " should larger than 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;
cout << "Insert:Insert index value " << index << " is out of range!" << endl;
return false;
}
// 当动态顺序表已经满了,需要新增一个位置
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
if (this->GetLength() >= this->GetMaxSize()) {
this->ReIncrease(1);
}
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i - 1));
}
this->SetData(index, elem);
this->SetLength();
return true;
}
#endif

View File

@@ -62,7 +62,7 @@ StaticLinkListNode::StaticLinkListNode(element_type elem, int next) {
bool StaticLinkListNode::Destroy() {
this->SetData(DEFAULTELEM);
this->SetNext(NULL);
this->SetNext(0);
return true;
}
@@ -138,7 +138,7 @@ int StaticLinkList::GetMaxSize() const {
}
StaticLinkList::StaticLinkList() {
this->SetFirst(NULL);
this->SetFirst(DEFAULTELEM);
this->SetLength(0);
this->SetMaxSize(MAXSIZE);
this->data = new StaticLinkListNode[MAXSIZE];

View File

@@ -0,0 +1,43 @@
#include "sequence_list.h"
// 静态顺序表
class StaticSequenceList : public SequenceList {
public:
// 构造函数
StaticSequenceList();
explicit StaticSequenceList(int max_size);
// 插入函数
bool Insert(int index, element_type elem) override;
};
StaticSequenceList::StaticSequenceList() :SequenceList() {
}
StaticSequenceList::StaticSequenceList(int max_size) : SequenceList(max_size) {
}
bool StaticSequenceList::Insert(int index, element_type elem) {
// 当静态顺序表已经满了就不能插入任何元素
if (this->GetLength() >= this->GetMaxSize()) {
// cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
cout << "Insert:The space size of " << this->GetMaxSize() << " is not enough!" << endl;
return false;
}
// 索引位置从0开始,所以可以插入的范围是0到list->length
if (index > this->GetLength() || index < 0) {
// cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
cout << "Insert:Insert index value " << index << " is out of range!" << 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;
}

View File

@@ -3,7 +3,7 @@
int main()
{
SequenceListTest();
// LinkListTest();
// SequenceListTest();
LinkListTest();
return 0;
}

Binary file not shown.

View File

@@ -1,9 +1,6 @@
// 测试文件
#include "../head/sequence_list.h"
#include "../head/link_list.h"
#include "../head/double_link_list.h"
#include "../head/static_link_list.h"
#include "../head/list.h"
#include "../head/sequence_stack.h"
#include "../head/share_stack.h"
#include "../head/link_stack.h"
@@ -18,11 +15,12 @@ bool SequenceListTest() {
list.LoopInsert(a, 0, 6);
list.Print();
// element_type * data = list.GetData();
element_type* b = list.LoopDelete(1, 3);
list.Print();
for (int i = 0; i < 3; i++) {
cout << b[i] << endl;
}
// element_type* b = list.LoopDelete(1, 3);
// list.Print();
// for (int i = 0; i < 3; i++) {
// cout << b[i] << endl;
// }
cout << list.GetData(list.Max()) << endl;
list.Destroy();
return true;
}
@@ -39,16 +37,17 @@ bool LinkListTest() {
list2.PriorInsert(a, 2, 3);
list2.Print();
cout << list2.GetLength() << endl;*/
auto* list = new LinkListWithoutHead();
auto* list = new LinkListWithHead();
list->NextInsert(a, 0 ,5);
list->Print();
int len = 2;
element_type* b = list->Delete(2, len);
for (int i = 0; i < len; i++) {
cout << b[i] << endl;
}
list->Print();
cout << list->Locate('1') << endl;
// int len = 2;
// element_type* b = list->Delete(2, len);
// for (int i = 0; i < len; i++) {
// cout << b[i] << endl;
// }
// list->Print();
// cout << list->Locate('1') << endl;
cout << list->GetData(list->Min()) << endl;
return true;
}