diff --git a/.gitignore b/.gitignore index b2aaec3..a0518bb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.html *.htm *.vs/ +*.vscode/ *.txt *.idea/ *Debug/ diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 7337529..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "xstring": "cpp" - } -} \ No newline at end of file diff --git a/Code/CPP-Code/head/dynamic_sequence_list.h b/Code/CPP-Code/head/dynamic_sequence_list.h new file mode 100644 index 0000000..603086b --- /dev/null +++ b/Code/CPP-Code/head/dynamic_sequence_list.h @@ -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; +} diff --git a/Code/CPP-Code/head/link_list.h b/Code/CPP-Code/head/link_list.h index 7b23c8a..f3572fb 100644 --- a/Code/CPP-Code/head/link_list.h +++ b/Code/CPP-Code/head/link_list.h @@ -1,3 +1,7 @@ +#ifndef _LINK_LIST_ +#define _LINK_LIST_ + +#include #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 \ No newline at end of file diff --git a/Code/CPP-Code/head/link_list_with_head.h b/Code/CPP-Code/head/link_list_with_head.h new file mode 100644 index 0000000..e02274a --- /dev/null +++ b/Code/CPP-Code/head/link_list_with_head.h @@ -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; +} \ No newline at end of file diff --git a/Code/CPP-Code/head/link_list_without_head.h b/Code/CPP-Code/head/link_list_without_head.h new file mode 100644 index 0000000..7cb5d82 --- /dev/null +++ b/Code/CPP-Code/head/link_list_without_head.h @@ -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; +} diff --git a/Code/CPP-Code/head/list.h b/Code/CPP-Code/head/list.h new file mode 100644 index 0000000..f4f833c --- /dev/null +++ b/Code/CPP-Code/head/list.h @@ -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" \ No newline at end of file diff --git a/Code/CPP-Code/head/sequence_list.h b/Code/CPP-Code/head/sequence_list.h index 4fb7398..5545744 100644 --- a/Code/CPP-Code/head/sequence_list.h +++ b/Code/CPP-Code/head/sequence_list.h @@ -1,8 +1,11 @@ +#ifndef _SEQUENCE_LIST_ +#define _SEQUENCE_LIST_ +// 避免重复编译 #include #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;iGetLength();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;iGetLength();i++){ + if(this->GetData(i)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 \ No newline at end of file diff --git a/Code/CPP-Code/head/static_link_list.h b/Code/CPP-Code/head/static_link_list.h index aa3116f..a3a82ce 100644 --- a/Code/CPP-Code/head/static_link_list.h +++ b/Code/CPP-Code/head/static_link_list.h @@ -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]; diff --git a/Code/CPP-Code/head/static_sequence_list.h b/Code/CPP-Code/head/static_sequence_list.h new file mode 100644 index 0000000..a09895e --- /dev/null +++ b/Code/CPP-Code/head/static_sequence_list.h @@ -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; +} \ No newline at end of file diff --git a/Code/CPP-Code/source/main.cpp b/Code/CPP-Code/source/main.cpp index 095caeb..9e6e33b 100644 --- a/Code/CPP-Code/source/main.cpp +++ b/Code/CPP-Code/source/main.cpp @@ -3,7 +3,7 @@ int main() { - SequenceListTest(); -// LinkListTest(); +// SequenceListTest(); + LinkListTest(); return 0; } \ No newline at end of file diff --git a/Code/CPP-Code/source/main.exe b/Code/CPP-Code/source/main.exe new file mode 100644 index 0000000..6e0e6f0 Binary files /dev/null and b/Code/CPP-Code/source/main.exe differ diff --git a/Code/CPP-Code/source/test.cpp b/Code/CPP-Code/source/test.cpp index a2a9b86..9cf2714 100644 --- a/Code/CPP-Code/source/test.cpp +++ b/Code/CPP-Code/source/test.cpp @@ -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; } diff --git a/Computer-Organization/1-data-representation-and-operation.md b/Computer-Organization/1-data-representation-and-operation.md index 9312edd..f6a4c59 100644 --- a/Computer-Organization/1-data-representation-and-operation.md +++ b/Computer-Organization/1-data-representation-and-operation.md @@ -95,7 +95,7 @@ $8421$码是一种有权码,第$1$、$2$、$3$、$4$位分别对应$8$、$4$ :-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-: 0000|0001|0010|0011|0100|0101|0110|0111|1000|1001 -如$985$用$8421$码表示就是$1001\,1000\,0101$。 +$8421$码就是十六进制的表达形式。如$46H=0100\,0110$,$985$用$8421$码表示就是$1001\,1000\,0101$。 使用$8421$码表示的数字进行算术运算的方式是先按照二进制的方式进行运算,若最后结果不在映射表中,即落在没有定义的$1010$到$1111$中,就直接加上$6$(因为有六位无效,所以加上六位跳过无效的位数)从而进一位,多了一段补全$0$。每个段对应的数值合在一起就是原来的结果。