From 74939da6df010db848efba6d4ee727ee502dee37 Mon Sep 17 00:00:00 2001 From: Didnelpsun <2675350965@qq.com> Date: Thu, 16 Sep 2021 16:48:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- Code/CPP-Code/CMakeSettings.json | 5 +- Code/CPP-Code/head/double_link_list.h | 4 +- Code/CPP-Code/head/head.h | 2 +- Code/CPP-Code/head/link_list.h | 87 ++++-- Code/CPP-Code/head/link_stack.h | 4 +- Code/CPP-Code/head/sequence_list.h | 90 +++--- Code/CPP-Code/head/sequence_stack.h | 7 +- Code/CPP-Code/head/share_stack.h | 26 +- Code/CPP-Code/head/static_link_list.h | 4 +- Code/CPP-Code/source/main.cpp | 2 +- Code/Code/CMakeLists.txt | 6 + Code/Code/CMakeSettings.json | 15 + Code/Code/Code.sln | 31 -- Code/Code/Code.vcxproj | 168 ----------- Code/Code/Code.vcxproj.filters | 78 ----- Code/Code/Code.vcxproj.user | 4 - Code/Code/double_link_list.h | 186 ------------ Code/Code/graph.h | 27 -- Code/Code/head.h | 8 - Code/Code/head/double_link_list.h | 30 ++ Code/Code/head/head.h | 6 + Code/Code/head/link_list.h | 325 +++++++++++++++++++++ Code/Code/head/sequence_list.h | 178 +++++++++++ Code/Code/head/sequence_stack.h | 65 +++++ Code/Code/head/static_link_list.h | 11 + Code/Code/link_list.h | 406 -------------------------- Code/Code/link_queue.h | 154 ---------- Code/Code/link_stack.h | 9 - Code/Code/link_string.h | 12 - Code/Code/link_tree.h | 190 ------------ Code/Code/main.c | 10 - Code/Code/search.h | 13 - Code/Code/sequence_list.h | 322 -------------------- Code/Code/sequence_queue.h | 65 ----- Code/Code/sequence_stack.h | 99 ------- Code/Code/sequence_string.h | 104 ------- Code/Code/sequence_tree.h | 46 --- Code/Code/sort.h | 259 ---------------- Code/Code/source/main.cpp | 8 + Code/Code/source/test.cpp | 49 ++++ Code/Code/static_link_list.h | 12 - Code/Code/test.c | 40 --- Code/Code/thread_tree.h | 84 ------ Data-Structrue/2-stack-ex.md | 45 +++ Data-Structrue/3-queue.md | 14 +- 46 files changed, 875 insertions(+), 2438 deletions(-) create mode 100644 Code/Code/CMakeLists.txt create mode 100644 Code/Code/CMakeSettings.json delete mode 100644 Code/Code/Code.sln delete mode 100644 Code/Code/Code.vcxproj delete mode 100644 Code/Code/Code.vcxproj.filters delete mode 100644 Code/Code/Code.vcxproj.user delete mode 100644 Code/Code/double_link_list.h delete mode 100644 Code/Code/graph.h delete mode 100644 Code/Code/head.h create mode 100644 Code/Code/head/double_link_list.h create mode 100644 Code/Code/head/head.h create mode 100644 Code/Code/head/link_list.h create mode 100644 Code/Code/head/sequence_list.h create mode 100644 Code/Code/head/sequence_stack.h create mode 100644 Code/Code/head/static_link_list.h delete mode 100644 Code/Code/link_list.h delete mode 100644 Code/Code/link_queue.h delete mode 100644 Code/Code/link_stack.h delete mode 100644 Code/Code/link_string.h delete mode 100644 Code/Code/link_tree.h delete mode 100644 Code/Code/main.c delete mode 100644 Code/Code/search.h delete mode 100644 Code/Code/sequence_list.h delete mode 100644 Code/Code/sequence_queue.h delete mode 100644 Code/Code/sequence_stack.h delete mode 100644 Code/Code/sequence_string.h delete mode 100644 Code/Code/sequence_tree.h delete mode 100644 Code/Code/sort.h create mode 100644 Code/Code/source/main.cpp create mode 100644 Code/Code/source/test.cpp delete mode 100644 Code/Code/static_link_list.h delete mode 100644 Code/Code/test.c delete mode 100644 Code/Code/thread_tree.h create mode 100644 Data-Structrue/2-stack-ex.md diff --git a/.gitignore b/.gitignore index 43ed921..9963ab2 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ *Debug/ *x64/ *out/ -*cmake-build-debug/ \ No newline at end of file +*cmake-build-debug/ +*cmake-build-debug-mingw/ \ No newline at end of file diff --git a/Code/CPP-Code/CMakeSettings.json b/Code/CPP-Code/CMakeSettings.json index 9204f06..825b59d 100644 --- a/Code/CPP-Code/CMakeSettings.json +++ b/Code/CPP-Code/CMakeSettings.json @@ -6,10 +6,7 @@ "configurationType": "Debug", "inheritEnvironments": [ "msvc_x64_x64" ], "buildRoot": "${projectDir}\\out\\build\\${name}", - "installRoot": "${projectDir}\\out\\install\\${name}", - "cmakeCommandArgs": "", - "buildCommandArgs": "", - "ctestCommandArgs": "" + "installRoot": "${projectDir}\\out\\install\\${name}" } ] } \ No newline at end of file diff --git a/Code/CPP-Code/head/double_link_list.h b/Code/CPP-Code/head/double_link_list.h index ac69e24..27adac6 100644 --- a/Code/CPP-Code/head/double_link_list.h +++ b/Code/CPP-Code/head/double_link_list.h @@ -71,7 +71,7 @@ DoubleLinkListNode *DoubleLinkListNode::GetNext() { DoubleLinkListNode::DoubleLinkListNode() { this->SetPrior(nullptr); this->SetNext(nullptr); - this->SetData(NULL); + this->SetData(DEFAULTELEM); } DoubleLinkListNode::DoubleLinkListNode(element_type elem) { @@ -97,6 +97,6 @@ bool DoubleLinkListNode::Destory() { free(this->GetNext()); this->SetPrior(nullptr); this->SetNext(nullptr); - this->SetData(NULL); + this->SetData(DEFAULTELEM); return true; } \ No newline at end of file diff --git a/Code/CPP-Code/head/head.h b/Code/CPP-Code/head/head.h index 6ab723b..54afd84 100644 --- a/Code/CPP-Code/head/head.h +++ b/Code/CPP-Code/head/head.h @@ -1,7 +1,7 @@ // 初始化最大长度 #define MAXSIZE 5 // 定义默认值 -#define DEFAULTELEM '0' +#define DEFAULTELEM '\0' // 定义最大值 //#define INFINITY 32767 // 定义默认数据类型 diff --git a/Code/CPP-Code/head/link_list.h b/Code/CPP-Code/head/link_list.h index 9de23c0..2f1b336 100644 --- a/Code/CPP-Code/head/link_list.h +++ b/Code/CPP-Code/head/link_list.h @@ -54,7 +54,7 @@ LinkListNode *LinkListNode::GetNext() { } LinkListNode::LinkListNode() { - this->SetData(NULL); + this->SetData(DEFAULTELEM); this->SetNext(nullptr); } @@ -72,7 +72,7 @@ bool LinkListNode::Destory() { if (this->GetNext() != nullptr) { free(this->GetNext()); } - this->SetData(NULL); + this->SetData(DEFAULTELEM); this->SetNext(nullptr); return true; } @@ -180,8 +180,9 @@ LinkList::LinkList() { element_type LinkList::GetElem(int index) { if (index >= this->GetLength() || index < 0) { - cout << "GetElem:查找索引" << index << "超过索引范围!" << endl; - return NULL; + // cout << "GetElem:查找索引" << index << "超过索引范围!" << endl; + cout << "GetElem:The index " << index << " is out of range!" << endl; + return DEFAULTELEM; } LinkListNode *node = this->GetNext(); for (int i = 1; i < index; i++) { @@ -197,7 +198,8 @@ int LinkList::Locate(element_type elem) { return i; } } - cout << "Locate:未能定位到值为" << elem << "的元素!" << endl; + // cout << "Locate:未能定位到值为" << elem << "的元素!" << endl; + cout << "Locate:Can't locate the element with value " << elem << " !" << endl; return -1; } @@ -210,7 +212,8 @@ bool LinkList::PriorInsert(element_type *elem, int start, int length) { for (int i = 0; i < length; i++) { bool result = this->Insert(1, elem[i + start]); if (!result) { - cout << "PriorInsert:循环插入失败!索引值为" << i + start << endl; + // cout << "PriorInsert:循环插入失败!索引值为" << i + start << endl; + cout << "PriorInsert:Loop Insert of element with index value of " << i + start << " failed!" << endl; return false; } } @@ -219,7 +222,8 @@ bool LinkList::PriorInsert(element_type *elem, int start, int length) { for (int i = 0; i < length; i++) { bool result = this->Insert(0, elem[i + start]); if (!result) { - cout << "PriorInsert:循环插入失败!索引值为" << i + start << endl; + // cout << "PriorInsert:循环插入失败!索引值为" << i + start << endl; + cout << "PriorInsert:Loop Insert of element with index value of " << i + start << "failed!" << endl; return false; } } @@ -232,7 +236,8 @@ bool LinkList::NextInsert(element_type *elem, int start, int length) { for (int i = 0; i < length; i++) { bool result = this->Insert(i + 1, elem[i + start]); if (!result) { - cout << "NextInsert:循环插入失败!索引值为" << i + start << endl; + // cout << "NextInsert:循环插入失败!索引值为" << i + start << endl; + cout << "NextInsert:Loop insert of element with index value of " << i + start << "failed!" << endl; return false; } } @@ -241,7 +246,8 @@ bool LinkList::NextInsert(element_type *elem, int start, int length) { for (int i = 0; i < length; i++) { bool result = this->Insert(i, elem[i + start]); if (!result) { - cout << "NextInsert:循环插入失败!索引值为" << i + start << endl; + // cout << "NextInsert:循环插入失败!索引值为" << i + start << endl; + cout << "NextInsert:Loop insert of element with index value of " << i + start << "failed!" << endl; return false; } } @@ -257,7 +263,7 @@ bool LinkList::Destroy() { this->SetLength(0); free(this->GetNext()); this->SetNext(nullptr); - this->SetType(NULL); + this->SetType(true); return true; } @@ -282,14 +288,16 @@ LinkListWithHead::LinkListWithHead() { bool LinkListWithHead::Print() { int i = 1; - cout << "第0个元素值为空" << endl; + // 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 << "第" << i << "个元素值为" << node->GetData() << endl; + cout << "index: " << i << " -> value: " << node->GetData() << endl; i++; node = node->GetNext(); } @@ -298,7 +306,8 @@ bool LinkListWithHead::Print() { bool LinkListWithHead::Insert(int index, element_type elem) { if (index < 1) { - cout << "Insert:插入索引值" << index << "过小!" << endl; + // cout << "Insert:插入索引值" << index << "过小!" << endl; + cout << "Insert:Insert index value " << index << " is too small!" << endl; return false; } // 定义一个结点指针p指向当前扫描到的结点 @@ -330,7 +339,8 @@ bool LinkListWithHead::Insert(int index, element_type elem) { } // 如果此时i小于index-1,表示遍历完还没有到达对应的索引 if (i < index - 1) { - cout << "Insert:插入索引值" << index << "过大!" << endl; + // cout << "Insert:插入索引值" << index << "过大!" << endl; + cout << "Insert:Insert index value" << index << " is too large!"; return false; } // 此时i==index-1 @@ -344,11 +354,13 @@ bool LinkListWithHead::Insert(int index, element_type elem) { element_type *LinkListWithHead::Delete(int index, int length) { auto *data = new element_type[length]; if (index < 1) { - cout << "Delete:删除索引值" << index << "过小!" << endl; + // 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:删除长度" << length << "过小!" << endl; + cout << "Delete:Delete length value " << length << " is too small!" << endl; return data; } // 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个 @@ -361,7 +373,8 @@ element_type *LinkListWithHead::Delete(int index, int length) { start = this->GetNext(); // 如果链表没有任何数据 if (start == nullptr) { - cout << "Delete:链表为空!" << endl; + // cout << "Delete:链表为空!" << endl; + cout << "Delete:Link list is empty!" << endl; return data; } // 循环遍历到达指定索引号的单链表的结点 @@ -372,7 +385,8 @@ element_type *LinkListWithHead::Delete(int index, int length) { } // 如果此时i小于index-1,表示遍历完还没有到达对应的索引 if (i < index - 1) { - cout << "Delete:删除索引值" << index << "过大!" << endl; + // cout << "Delete:删除索引值" << index << "过大!" << endl; + cout << "Delete:Delete index value " << index << " is too large!" << endl; return data; } // 此时i==index-1,start到达,求end @@ -381,7 +395,8 @@ element_type *LinkListWithHead::Delete(int index, int length) { data[i] = end->GetData(); end = end->GetNext(); if (end == nullptr) { - cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length - 1 << "!" << endl; + // 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; } } @@ -438,7 +453,7 @@ element_type LinkListWithoutHead::GetData() const { LinkListWithoutHead::LinkListWithoutHead() { this->SetType(false); - this->SetData(NULL); + this->SetData(DEFAULTELEM); } bool LinkListWithoutHead::Print() { @@ -446,12 +461,14 @@ bool LinkListWithoutHead::Print() { if (this->GetLength() == 0) { return true; } - cout << "第" << i << "个元素值为" << this->GetData() << endl; + // 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 << "第" << i << "个元素值为" << node->GetData() << endl; + cout << "index: " << i << " -> value: " << node->GetData() << endl; node = node->GetNext(); } return true; @@ -459,7 +476,8 @@ bool LinkListWithoutHead::Print() { bool LinkListWithoutHead::Insert(int index, element_type elem) { if (index < 0) { - cout << "Insert:插入索引值" << index << "过小!" << endl; + // cout << "Insert:插入索引值" << index << "过小!" << endl; + cout << "Insert:Insert index value " << index << " is too small!" << endl; return false; } if (index == 0) { @@ -497,7 +515,8 @@ bool LinkListWithoutHead::Insert(int index, element_type elem) { } // 如果此时i小于index-1,表示遍历完还没有到达对应的索引 if (i < index - 1) { - cout << "Insert:插入索引值" << index << "过大!" << endl; + // cout << "Insert:插入索引值" << index << "过大!" << endl; + cout << "Insert:Insert index value" << index << " is too large!"; return false; } // 此时i==index-1 @@ -511,11 +530,14 @@ bool LinkListWithoutHead::Insert(int index, element_type elem) { element_type *LinkListWithoutHead::Delete(int index, int length) { auto *data = new element_type[length]; if (index < 0) { - cout << "Delete:删除索引值" << index << "过小!" << endl; + // cout << "Delete:删除索引值" << index << "过小!" << endl; + cout << "Delete:Delete index value " << index << " is too small!"; return data; } if (length < 1) { - cout << "Delete:删除长度" << length << "过小!" << endl; + // cout << "Delete:删除长度" << length << "过小!" << endl; + cout << "Delete:Delete length value " << length << " is too small!"; + return data; } // 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个 LinkListNode *start; @@ -526,8 +548,9 @@ element_type *LinkListWithoutHead::Delete(int index, int length) { // 将链表头结点的next指向start,为第1个结点 start = this->GetNext(); // 如果链表没有任何数据 - if (this->GetData() == NULL) { - cout << "Delete:链表为空!" << endl; + if (this->GetData() == DEFAULTELEM) { + // cout << "Delete:链表为空!" << endl; + cout << "Delete:Link list is empty!" << endl; return data; } data[0] = this->GetData(); @@ -539,7 +562,8 @@ element_type *LinkListWithoutHead::Delete(int index, int length) { } // 如果此时i小于index-1,表示遍历完还没有到达对应的索引 if (i < index - 1) { - cout << "Delete:删除索引值" << index << "过大!" << endl; + // cout << "Delete:删除索引值" << index << "过大!" << endl; + cout << "Delete:Delete index value " << index << " is too large!"; return data; } // 从1开始遍历 @@ -550,7 +574,8 @@ element_type *LinkListWithoutHead::Delete(int index, int length) { } end = end->GetNext(); if (end == nullptr) { - cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length - 1 << "!" << endl; + // 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; } } @@ -577,6 +602,6 @@ int LinkListWithoutHead::Locate(element_type elem) { } bool LinkListWithoutHead::Destroy() { - this->SetData(NULL); + this->SetData(DEFAULTELEM); return LinkList::Destroy(); } diff --git a/Code/CPP-Code/head/link_stack.h b/Code/CPP-Code/head/link_stack.h index 12c1137..bc53bb4 100644 --- a/Code/CPP-Code/head/link_stack.h +++ b/Code/CPP-Code/head/link_stack.h @@ -54,7 +54,7 @@ LinkStackNode *LinkStackNode::GetNext() { } LinkStackNode::LinkStackNode() { - this->SetData(NULL); + this->SetData(DEFAULTELEM); this->SetNext(nullptr); } @@ -69,7 +69,7 @@ LinkStackNode::LinkStackNode(element_type data, LinkStackNode *next) { } bool LinkStackNode::Destroy() { - this->SetData(NULL); + this->SetData(DEFAULTELEM); delete(this->GetNext()); this->SetNext(nullptr); return true; diff --git a/Code/CPP-Code/head/sequence_list.h b/Code/CPP-Code/head/sequence_list.h index 409c54b..4d914fd 100644 --- a/Code/CPP-Code/head/sequence_list.h +++ b/Code/CPP-Code/head/sequence_list.h @@ -103,7 +103,8 @@ SequenceList::SequenceList() { bool SequenceList::Print() const { for (int i = 0; i < this->GetLength(); i++) { - cout << "第" << i + 1 << "个元素值为" << this->GetData(i) << endl; + // cout << "第" << i << "个元素值为" << this->GetData(i) << endl; + cout << "index: " << i << " -> value: " << this->GetData(i) << endl; } return true; } @@ -112,7 +113,8 @@ 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]); if (!result) { - cout << "LoopInsert:循环插入失败!" << endl; + // cout << "LoopInsert:循环插入失败!" << endl; + cout << "LoopInsert:Loop insert failed!" << endl; return false; } } @@ -121,7 +123,8 @@ bool SequenceList::LoopInsert(element_type *elem, int index, int length) { element_type SequenceList::Delete(int index) { if (index >= this->GetLength() || index < 0) { - cout << "Delete:删除索引" << index << "超过索引范围!" << endl; + // cout << "Delete:删除索引" << index << "超过索引范围!" << endl; + cout << "Delete:Delete index value " << index << " is out of range!" << endl; return false; } for (int i = index; i < this->GetLength(); i++) { @@ -133,28 +136,26 @@ element_type SequenceList::Delete(int index) { element_type *SequenceList::LoopDelete(int index, int length) { if (index + length > this->GetLength() || index < 0) { - cout << "LoopDelete:删除索引" << index + length << "超过索引范围!" << endl; + // cout << "LoopDelete:删除索引" << index + length << "超过索引范围!" << endl; + cout << "LoopDelete:Loop Delete index value " << index + length << " is out of range!" << endl; return nullptr; } auto *elem = new element_type[length]; - if (elem) { - for (int i = index; i <= this->GetLength() - length; i++) { - if (i < index + length) { - elem[i - index] = this->GetData(i); - } - this->SetData(i, this->GetData(i + length)); + for (int i = index; i <= this->GetLength() - length; i++) { + if (i < index + length) { + elem[i - index] = this->GetData(i); } - this->SetLength(this->GetLength() - length); - } else { - cout << "LoopDelete:申请空间失败!" << endl; + this->SetData(i, this->GetData(i + length)); } + this->SetLength(this->GetLength() - length); return elem; } element_type SequenceList::GetElem(int index) const { if (index >= this->GetLength() || index < 0) { - cout << "GetElem:查找索引" << index << "超过索引范围!" << endl; - return NULL; + // cout << "GetElem:查找索引" << index << "超过索引范围!" << endl; + cout << "GetElem:The index " << index << " is out of range!" << endl; + return DEFAULTELEM; } return this->GetData(index); } @@ -166,7 +167,8 @@ int SequenceList::Locate(element_type elem) const { return i; } } - cout << "Locate:未能定位到对应值的元素!" << endl; + // cout << "Locate:未能定位到对应值的元素!" << endl; + cout << "Locate:Can't locate the element with value " << elem << " !" << endl; return -1; } @@ -198,12 +200,14 @@ StaticSequenceList::StaticSequenceList() : SequenceList() { bool StaticSequenceList::Insert(int index, element_type elem) { // 当静态顺序表已经满了就不能插入任何元素 if (this->GetLength() >= MAXSIZE) { - cout << "Insert:静态顺序表空间不足,插入失败!" << endl; + // cout << "Insert:静态顺序表空间不足,插入失败!" << endl; + cout << "Insert:The space size of " << MAXSIZE << " is not enough!" << endl; return false; } // 索引位置从0开始,所以可以插入的范围是0到list->length if (index > this->GetLength() || index < 0) { - cout << "Insert:插入索引" << index << "超过索引范围!" << endl; + // cout << "Insert:插入索引" << index << "超过索引范围!" << endl; + cout << "Insert:Insert index value " << index << " is out of range!" << endl; return false; } // 从最后一个元素开始交换后移,list->length是空的 @@ -255,41 +259,34 @@ DynamicSequenceList::DynamicSequenceList() : SequenceList() { this->SetMaxSize(0); // 申请一片连续的存储空间 auto *space = new element_type[MAXSIZE]; - if (space) { - this->SetData(space); - this->SetMaxSize(MAXSIZE); - } else { - cout << "SequenceList:分配空间失败!" << endl; - } + this->SetData(space); + this->SetMaxSize(MAXSIZE); } -bool DynamicSequenceList::OtherIncrease(int len) { - if (len <= 0) { - cout << "OtherIncrease:申请空间应该大于0!" << endl; +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() + len; + int new_length = this->GetMaxSize() + length; 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; + // 建立中间变量 + 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:申请空间应该大于0!" << endl; + cout << "ReIncrease:The length " << length << " should larger than 0!" << endl; return false; } // 申请一片连续的存储空间 @@ -309,17 +306,14 @@ bool DynamicSequenceList::ReIncrease(int length) { bool DynamicSequenceList::Insert(int index, element_type elem) { if (index > this->GetLength() || index < 0) { - cout << "Insert:插入索引" << index << "超过索引范围!" << endl; + // cout << "Insert:插入索引" << index << "超过索引范围!" << endl; + cout << "Insert:Insert index value " << index << " is out of range!" << endl; return false; } // 当动态顺序表已经满了,需要新增一个位置 // 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面 if (this->GetLength() >= MAXSIZE) { - bool result = this->ReIncrease(1); - if (!result) { - cout << "Insert:申请空间失败!" << endl; - return false; - } + this->ReIncrease(1); } for (int i = this->GetLength(); i > index; i--) { this->SetData(i, this->GetData(i - 1)); diff --git a/Code/CPP-Code/head/sequence_stack.h b/Code/CPP-Code/head/sequence_stack.h index a7ec1dc..580939e 100644 --- a/Code/CPP-Code/head/sequence_stack.h +++ b/Code/CPP-Code/head/sequence_stack.h @@ -111,6 +111,7 @@ int SequenceStack::Length() const { bool SequenceStack::Push(element_type elem) { if (this->Full()) { cout << "Push:栈满无法进栈!" << endl; + cout << "Push:The stack is full!" << endl; return false; } this->_data[this->SetTop()] = elem; @@ -120,7 +121,8 @@ bool SequenceStack::Push(element_type elem) { element_type SequenceStack::Pop() { if (this->Empty()) { cout << "Pop:栈空无法出栈!" << endl; - return NULL; + cout << "Pop:The stack is empty!" << endl; + return DEFAULTELEM; } return this->_data[this->SetTop(this->GetTop() - 1)]; } @@ -128,7 +130,8 @@ element_type SequenceStack::Pop() { element_type SequenceStack::Top() { if (this->Empty()) { cout << "Top:栈空无法读栈顶元素!" << endl; - return NULL; + cout << "Top:The stack is empty!" << endl; + return DEFAULTELEM; } return this->_data[this->GetTop() - 1]; } diff --git a/Code/CPP-Code/head/share_stack.h b/Code/CPP-Code/head/share_stack.h index 4b7cf88..4207569 100644 --- a/Code/CPP-Code/head/share_stack.h +++ b/Code/CPP-Code/head/share_stack.h @@ -158,7 +158,8 @@ int ShareStack::LengthRight() const { bool ShareStack::PushLeft(element_type elem) { if (this->Full()) { - cout << "PushLeft:栈满无法进栈!" << endl; + // cout << "PushLeft:栈满无法进栈!" << endl; + cout << "PushLeft:The stack is full!"; return false; } this->_data[this->SetTopLeft()] = elem; @@ -167,7 +168,8 @@ bool ShareStack::PushLeft(element_type elem) { bool ShareStack::PushRight(element_type elem) { if(this->Full()){ - cout << "PushRight:栈满无法进栈!" << endl; + // cout << "PushRight:栈满无法进栈!" << endl; + cout << "PushRight:The stack is full!" << endl; return false; } this->_data[this->SetTopRight()] = elem; @@ -176,32 +178,36 @@ bool ShareStack::PushRight(element_type elem) { element_type ShareStack::PopLeft() { if (this->EmptyLeft()) { - cout << "PopLeft:栈空无法出栈!" << endl; - return NULL; + // cout << "PopLeft:栈空无法出栈!" << endl; + cout << "PopLeft:The stack is empty!" << endl; + return DEFAULTELEM; } return this->_data[this->SetTopLeft(this->GetTopLeft() - 1)]; } element_type ShareStack::PopRight() { if (this->EmptyRight()) { - cout << "PopRight:栈空无法出栈!" << endl; - return NULL; + // cout << "PopRight:栈空无法出栈!" << endl; + cout << "PopRight:The stack is empty!" << endl; + return DEFAULTELEM; } return this->_data[this->SetTopRight(this->GetTopRight() + 1)]; } element_type ShareStack::TopLeft() { if (this->EmptyLeft()) { - cout << "TopLeft:栈空无法读栈顶元素!" << endl; - return NULL; + // cout << "TopLeft:栈空无法读栈顶元素!" << endl; + cout << "TopLeft:The stack is empty!" << endl; + return DEFAULTELEM; } return this->_data[this->GetTopLeft() - 1]; } element_type ShareStack::TopRight() { if (this->EmptyRight()) { - cout << "TopRight:栈空无法读栈顶元素!" << endl; - return NULL; + // cout << "TopRight:栈空无法读栈顶元素!" << endl; + cout << "TopRight:The stack is empty!" << endl; + return DEFAULTELEM; } return this->_data[this->GetTopRight() + 1]; } \ 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 830ed43..bb4940a 100644 --- a/Code/CPP-Code/head/static_link_list.h +++ b/Code/CPP-Code/head/static_link_list.h @@ -48,7 +48,7 @@ int StaticLinkListNode::GetNext() const { } StaticLinkListNode::StaticLinkListNode() { - this->SetData(NULL); + this->SetData(DEFAULTELEM); this->SetNext(-1); } @@ -63,7 +63,7 @@ StaticLinkListNode::StaticLinkListNode(element_type elem, int next) { } bool StaticLinkListNode::Destroy() { - this->SetData(NULL); + this->SetData(DEFAULTELEM); this->SetNext(NULL); return true; } diff --git a/Code/CPP-Code/source/main.cpp b/Code/CPP-Code/source/main.cpp index 9474a29..9e6e33b 100644 --- a/Code/CPP-Code/source/main.cpp +++ b/Code/CPP-Code/source/main.cpp @@ -1,4 +1,4 @@ -#include "test.cpp" +#include "test.cpp" int main() diff --git a/Code/Code/CMakeLists.txt b/Code/Code/CMakeLists.txt new file mode 100644 index 0000000..aa7b696 --- /dev/null +++ b/Code/Code/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20) +project(Code) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(Code source/main.cpp "head/link_list.h") diff --git a/Code/Code/CMakeSettings.json b/Code/Code/CMakeSettings.json new file mode 100644 index 0000000..9204f06 --- /dev/null +++ b/Code/Code/CMakeSettings.json @@ -0,0 +1,15 @@ +{ + "configurations": [ + { + "name": "x64-Debug", + "generator": "Ninja", + "configurationType": "Debug", + "inheritEnvironments": [ "msvc_x64_x64" ], + "buildRoot": "${projectDir}\\out\\build\\${name}", + "installRoot": "${projectDir}\\out\\install\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "", + "ctestCommandArgs": "" + } + ] +} \ No newline at end of file diff --git a/Code/Code/Code.sln b/Code/Code/Code.sln deleted file mode 100644 index 374aacd..0000000 --- a/Code/Code/Code.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31205.134 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Code", "Code.vcxproj", "{82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Debug|x64.ActiveCfg = Debug|x64 - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Debug|x64.Build.0 = Debug|x64 - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Debug|x86.ActiveCfg = Debug|Win32 - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Debug|x86.Build.0 = Debug|Win32 - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Release|x64.ActiveCfg = Release|x64 - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Release|x64.Build.0 = Release|x64 - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Release|x86.ActiveCfg = Release|Win32 - {82E38FA5-0EDC-487C-9E6F-8BFCE2D77216}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {5CDFB3EB-662D-456D-B96A-FC26C765E06A} - EndGlobalSection -EndGlobal diff --git a/Code/Code/Code.vcxproj b/Code/Code/Code.vcxproj deleted file mode 100644 index fd5c15e..0000000 --- a/Code/Code/Code.vcxproj +++ /dev/null @@ -1,168 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - Win32Proj - {82e38fa5-0edc-487c-9e6f-8bfce2d77216} - Code - 10.0 - - - - Application - true - v142 - Unicode - - - Application - false - v142 - true - Unicode - - - Application - true - v142 - Unicode - - - Application - false - v142 - true - Unicode - - - - - - - - - - - - - - - - - - - - - true - - - false - - - true - - - false - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - UninitializedLocalUsageCheck - - - Console - true - - - - - Level3 - true - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Code/Code/Code.vcxproj.filters b/Code/Code/Code.vcxproj.filters deleted file mode 100644 index 86ef3c5..0000000 --- a/Code/Code/Code.vcxproj.filters +++ /dev/null @@ -1,78 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - 头文件 - - - 源文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 源文件 - - - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - - - \ No newline at end of file diff --git a/Code/Code/Code.vcxproj.user b/Code/Code/Code.vcxproj.user deleted file mode 100644 index 88a5509..0000000 --- a/Code/Code/Code.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/Code/Code/double_link_list.h b/Code/Code/double_link_list.h deleted file mode 100644 index 1275bd7..0000000 --- a/Code/Code/double_link_list.h +++ /dev/null @@ -1,186 +0,0 @@ -#include -#include -#include "head.h" - -// 双链表结点 -typedef struct DoubleLinkNode{ - element_type data; - struct DoubleLinkNode* prior, * next; -} DoubleLinkNode, *DoubleLinkList; - - -// 初始化有头节点双链表 -int InitDoubleLinkListWithHead(DoubleLinkList list) { - list = (DoubleLinkNode*)malloc(sizeof(DoubleLinkNode)); - // 分配内存失败 - if (list == NULL) { - printf("InitDoubleLinkListWithHead:初始化分配内存失败!"); - return 1; - } - // 头结点的前驱结点始终为NULL - list->prior = NULL; - list->next = NULL; - return 0; -} - -// 判断有头节点双链表是否为空 -int IsDoubleLinkListEmptyWithHead(DoubleLinkList list) { - if (list->next == NULL) { - return 1; - } - else { - return 0; - } -} - -// 后插入双链表元素 -int InsertNextDoubleLinkNode(DoubleLinkNode* node, element_type elem) { - if (node == NULL) { - printf("InsertNextDoubleLinkNode:插入结点为空!"); - return 1; - } - DoubleLinkNode* s = (DoubleLinkNode*)malloc(sizeof(DoubleLinkNode)); - // 如果分配空间失败 - if (s == NULL) { - printf("InsertNextDoubleLinkNode:分配内存失败!\n"); - return 1; - } - s->data = elem; - // 将新建结点s插入结点node后 - s->next = node->next; - // 如果有后继结点,将p原来的后继给s结点 - if (node->next->prior) { - node->next->prior = s; - } - // 交换s的前驱和后继 - s->prior = node; - node->next = s; - return 0; -} - -// 前插入双链表元素 -int InsertPriorDoubleLinkNode(DoubleLinkNode* node, element_type elem) { - if (node == NULL) { - printf("InsertPriorDoubleLinkNode:插入结点为空!"); - return 1; - } - DoubleLinkNode* s = (DoubleLinkNode*)malloc(sizeof(DoubleLinkNode)); - // 如果分配空间失败 - if (s == NULL) { - printf("InsertPriorDoubleLinkNode:分配内存失败!\n"); - return 1; - } - s->next = node->next; - node->next = s; - s->data = node->data; - node->data = elem; - return 0; -} - -// 删除双链表后续结点 -int DeleteNextDoubleLinkListNode(DoubleLinkNode* node) { - if (node == NULL) { - printf("DeleteDoubleLinkListNode:删除结点为空!"); - return 1; - } - DoubleLinkNode* p = node->next; - if (p == NULL) { - printf("DeleteDoubleLinkListNode:删除结点后续结点为空!"); - return 1; - } - node->next = p->next; - // 如果p结点为最后一个结点 - if (p->next != NULL) { - p->next->prior = node; - } - free(p); - return 0; -} - -// 销毁双链表 -int DestroyDoubleLinkList(DoubleLinkList list) { - // 循环删除各个结点 - while (list->next != NULL) { - DeleteNextDoubleLinkListNode(list); - } - // 释放头结点 - free(list); - list = NULL; - return 0; -} - -// 初始化有头节点循环双链表 -int InitCircularDoubleLinkListWithHead(DoubleLinkList list) { - list = (DoubleLinkNode*)malloc(sizeof(DoubleLinkNode)); - if (list == NULL) { - printf("InitCircularDoubleLinkListWithHead:初始化分配内存失败!"); - return 1; - } - // 前后指针都指向其本身 - list->next = list; - list->prior = list; - return 0; -} - -// 判断有头节点循环双链表是否为空 -int IsCircularDoubleLinkListEmptyWithHead(DoubleLinkList list) { - if (list->next == list) { - return 1; - } - else { - return 0; - } -} - -// 判断结点是否尾有头节点循环双链表的尾结点 -int IsCircularDoubleLinkListEndWithHead(DoubleLinkList list, DoubleLinkNode* node) { - if (node->next == list) { - return 1; - } - else { - return 0; - } -} - -// 后插入循环双链表元素 -// 与双链表一样,唯一区别就是不用判别后一个元素是否为NULL,因为是连接在一起的 -int InsertNextCircularDoubleLinkNode(DoubleLinkNode* node, element_type elem) { - if (node == NULL) { - printf("InsertNextDoubleLinkNode:插入结点为空!"); - return 1; - } - DoubleLinkNode* s = (DoubleLinkNode*)malloc(sizeof(DoubleLinkNode)); - // 如果分配空间失败 - if (s == NULL) { - printf("InsertNextDoubleLinkNode:分配内存失败!\n"); - return 1; - } - s->data = elem; - // 将新建结点s插入结点node后 - s->next = node->next; - // 不同之处,一定有后继结点,将p原来的后继给s结点 - node->next->prior = s; - // 交换s的前驱和后继 - s->prior = node; - node->next = s; - return 0; -} - -// 删除循环双链表后续结点 -// 与双链表一样,唯一区别就是不用判别后一个元素是否为NULL,因为是连接在一起的 -int DeleteNextCircularDoubleLinkListNode(DoubleLinkNode* node) { - if (node == NULL) { - printf("DeleteDoubleLinkListNode:删除结点为空!"); - return 1; - } - DoubleLinkNode* p = node->next; - if (p == NULL) { - printf("DeleteDoubleLinkListNode:删除结点后续结点为空!"); - return 1; - } - node->next = p->next; - // 不同之处 - p->next->prior = node; - free(p); - return 0; -} \ No newline at end of file diff --git a/Code/Code/graph.h b/Code/Code/graph.h deleted file mode 100644 index d42da18..0000000 --- a/Code/Code/graph.h +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include -#include "head.h" - -// ڽӾͼ -typedef struct { - // - element_type vex[MAXSIZE]; - // ڽӾ󣬱߱ - int edge[MAXSIZE][MAXSIZE]; - // ͼǰĶͱ/ - int vex_num, arc_num; -} AdjacencyMatrixGraph; - -// ڽӱ -typedef struct AdjacencyListNode { - // Ϣ - element_type vex; - // һ - AdjacencyListNode* first; -} AdjacencyListNode, AdjacencyList[MAXSIZE]; - -// ڽӱ -typedef struct { - AdjacencyList data; - int vex_num, arc_num; -} AdjacencyListGraph; diff --git a/Code/Code/head.h b/Code/Code/head.h deleted file mode 100644 index 3ea11c5..0000000 --- a/Code/Code/head.h +++ /dev/null @@ -1,8 +0,0 @@ -// ʼ󳤶 -#define MAXSIZE 5 -// Ĭֵ -#define DEFAULTELEM '0' -// ֵ -#define INFINITY 32767 -// Ĭ -typedef char element_type; diff --git a/Code/Code/head/double_link_list.h b/Code/Code/head/double_link_list.h new file mode 100644 index 0000000..a3420f3 --- /dev/null +++ b/Code/Code/head/double_link_list.h @@ -0,0 +1,30 @@ +#include +#include +#include "head.h" + +// ˫ +typedef struct DoubleLinkListNode{ + // + element_type data; + // ͷβָ + struct DoubleLinkListNode *prior, *next; +} DoubleLinkListNode, *DoubleLinkList; + +// ʼ +DoubleLinkList InitDoubleLinkList(){ + auto list = (DoubleLinkList) malloc(sizeof(DoubleLinkList)); + list->data = DEFAULTELEM; + list->prior = nullptr; + list->next = nullptr; + return list; +} + +// +bool DestroyDoubleLinkList(DoubleLinkList &list){ + list->data = DEFAULTELEM; + delete(list->prior); + delete(list->next); + list->prior = nullptr; + list->next = nullptr; + return true; +} diff --git a/Code/Code/head/head.h b/Code/Code/head/head.h new file mode 100644 index 0000000..be262b5 --- /dev/null +++ b/Code/Code/head/head.h @@ -0,0 +1,6 @@ +// ʼ󳤶 +#define MAXSIZE 5 +// Ĭֵ +#define DEFAULTELEM '\0' +// Ĭ +typedef char element_type; \ No newline at end of file diff --git a/Code/Code/head/link_list.h b/Code/Code/head/link_list.h new file mode 100644 index 0000000..98ba927 --- /dev/null +++ b/Code/Code/head/link_list.h @@ -0,0 +1,325 @@ +#include +#include +#include "head.h" + +// +typedef struct LinkListNode { + element_type data; + struct LinkListNode *next; +} LinkListNode, *LinkList; + +// ʼ +LinkList InitLinkList() { + auto list = (LinkList) malloc(sizeof(LinkList)); + list->data = DEFAULTELEM; + list->next = nullptr; + return list; +} + +// п +bool EmptyLinkList(LinkList list) { + return list->next == nullptr && list->data == DEFAULTELEM; +} + +// ж +bool TypeLinkList(LinkList list) { + if (EmptyLinkList(list)) { + return NULL; + } else { + // һΪվʹͷڵ + if (list->data == DEFAULTELEM) { + return true; + } else { + return false; + } + } +} + +// ӡ +bool PrintLinkList(LinkList list) { + int i = 0; + if (EmptyLinkList(list)) { + return true; + } + // ǰָ + LinkListNode *node = list; + while (!TypeLinkList(list) && node != nullptr && node->data != DEFAULTELEM || TypeLinkList(list) && node != nullptr) { + printf("%dԪֵΪ%c\n", i, node->data); + node = node->next; + i++; + } + return true; +} + +// ͷڵ +bool InsertLinkListWithHead(LinkList &list, int index, element_type elem) { + //if (!TypeLinkList(list)) { + // printf("InsertLinkListWithHead:Ӧͷڵͣ\n"); + // return false; + //} + if (index < 1) { + printf("InsertLinkListWithHead:ֵ%dС\n", index); + return false; + } + // һָpָǰɨ赽Ľ + LinkListNode *p; + // һiʾǰɨ赽Ľ + int i = 0; + // ͷָpΪ0 + p = list; + // ѭָŵĵĽ + // ǵǰһΪŵĽһǿս + while (p->next != nullptr && i < index - 1) { + p = p->next; + i++; + } + // ʱiСindex-1ʾ껹ûеӦ + if (i < index - 1) { + printf("InsertLinkListWithHead:ֵ\n"); + return false; + } + // ʱi==index-1 + auto *s = new LinkListNode(); + s->data = elem; + // pԭĺ̸µĽ + s->next = p->next; + p->next = s; + return true; +} + +// ͷڵ +bool InsertLinkListWithoutHead(LinkList &list, int index, element_type elem) { + //if (TypeLinkList(list)) { + // printf("InsertLinkListWithoutHead:Ӧͷڵͣ\n"); + // return false; + //} + if (index < 0) { + printf("InsertLinkListWithoutHead:ֵ%dС\n", index); + return false; + } + auto *s = new LinkListNode(); + if (index == 0) { + s->data = elem; + // sĺΪlistָ + s->next = list; + // listָΪsָ + list = s; + return true; + } + // һָpָǰɨ赽Ľ + LinkListNode *p; + // һiʾǰɨ赽Ľ + int i = 0; + // ͷָpΪ0 + p = list; + // ѭָŵĵĽ + // ǵǰһΪŵĽһǿս + while (p->next != nullptr && i < index - 1) { + p = p->next; + i++; + } + // ʱiСindex-1ʾ껹ûеӦ + if (i < index - 1) { + printf("InsertLinkListWithoutHead:ֵ\n"); + return false; + } + // ʱi==index-1 + s->data = elem; + // pԭĺ̸µĽ + s->next = p->next; + p->next = s; + return true; +} + +// +bool NextInsertLinkList(LinkList &list, element_type *elem, int start, int length) { + if (!TypeLinkList(list)) { + for (int i = 0; i < length; i++) { + bool result = InsertLinkListWithoutHead(list, i, elem[i + start]); + if (!result) { + printf("NextInsertLinkList:ѭʧܣֵΪ%d\n", i + start); + return false; + } + } + } else { + for (int i = 0; i < length; i++) { + bool result = InsertLinkListWithHead(list, i + 1, elem[i + start]); + if (!result) { + printf("NextInsertLinkList:ѭʧܣֵΪ%d\n", i + start); + return false; + } + } + } + return true; +} + +// ǰ +bool PriorInsertLinkList(LinkList &list, element_type *elem, int start, int length) { + if (!TypeLinkList(list)) { + for (int i = 0; i < length; i++) { + bool result = InsertLinkListWithoutHead(list, 0, elem[i + start]); + if (!result) { + printf("PriorInsertLinkList:ѭʧܣֵΪ%d\n", i + start); + return false; + } + } + return true; + } else { + for (int i = 0; i < length; i++) { + bool result = InsertLinkListWithHead(list, 1, elem[i + start]); + if (!result) { + printf("PriorInsertLinkList:ѭʧܣֵΪ%d\n", i + start); + return false; + } + } + return true; + } +} + +// ɾ +element_type *DeleteLinkListWithHead(LinkList &list, int index, int length) { + auto *data = (element_type *) malloc(length * sizeof(element_type)); + if (index < 1) { + printf("DeleteLinkListWithHead:ɾֵ%dС\n", index); + return data; + } + if (length < 1) { + printf("DeleteLinkListWithHead:ɾ%dС\n", length); + } + // һָstartָǰɨ赽Ľ㣬ҪɾһԪصǰһ + LinkListNode *start; + // һָstartָǰɨ赽Ľ㣬ҪɾԪ + LinkListNode *end; + // һiʾǰɨ赽Ľ + int i = 1; + // ͷnextָstartΪ1 + start = list->next; + // ûκ + if (start == nullptr) { + printf("DeleteLinkListWithHead:Ϊգ\n"); + return data; + } + // ѭָŵĵĽ + // ǵǰһΪŵĽһǿս + while (start->next != nullptr && i < index - 1) { + start = start->next; + i++; + } + // ʱiСindex-1ʾ껹ûеӦ + if (i < index - 1) { + printf("DeleteLinkListWithHead:ɾֵ%d\n", index); + return data; + } + // ʱi==index-1startend + end = start; + for (int i = 0; i < length; i++) { + data[i] = end->data; + end = end->next; + if (end == nullptr) { + printf("DeleteLinkListWithHead:ɾֵ%d%d\n", index + length - 1, length - 1); + return data; + } + } + if (index == 1) { + list->next = end; + } else { + start->next = end->next; + } + return data; +} + +element_type *DeleteLinkListWithoutHead(LinkList &list, int index, int length) { + auto *data = (element_type *) malloc(length * sizeof(element_type)); + if (index < 0) { + printf("DeleteLinkListWithoutHead:ɾֵС\n"); + return data; + } + if (length < 1) { + printf("DeleteLinkListWithoutHead:ɾ%dС\n", length); + } + // һָstartָǰɨ赽Ľ㣬ҪɾһԪصǰһ + LinkListNode *start; + // һָstartָǰɨ赽Ľ㣬ҪɾԪ + LinkListNode *end; + // iʾǰָǵڼ + int i = 0; + // ͷnextָstartΪ0 + start = list; + // ûκ + if (EmptyLinkList(list)) { + printf("DeleteLinkListWithoutHead:Ϊգ\n"); + return data; + } + // ѭָŵĵĽ + // ǵǰһΪŵĽһǿս + while (start->next != nullptr && i < index - 1) { + start = start->next; + i++; + } + // ʱiСindex-1ʾ껹ûеӦ + if (i < index - 1) { + printf("DeleteLinkListWithoutHead:ɾֵ%d\n", index); + return data; + } + // λ + end = start; + for (int i = 0; i < length; i++) { + end = end->next; + data[i] = end->data; + if (end->next == nullptr) { + printf("DeleteLinkListWithoutHead:ɾֵ%d%d\n", index + length - 1, length - 1); + return data; + } + } + // ɾһ0Ž + if (index == 0) { + start->data = end->next->data; + start->next = end->next->next; + } + start->next = end->next; + return data; +} + +// +int GetLengthLinkList(LinkList list) { + int length = 0; + LinkListNode *node = list; + while (node->next != nullptr) { + length++; + node = node->next; + } + return length; +} + +// λ +element_type GetLinkListElem(LinkList list, int index) { + if (index >= GetLengthLinkList(list) || index < 0) { + printf("GetLinkListElem:%dΧ\n", index); + return DEFAULTELEM; + } + LinkListNode *node = list; + for (int i = 0; i < index; i++) { + node = node->next; + } + return node->data; +} + +// ֵ +int LocateLinkList(LinkList list, element_type elem){ + LinkListNode *node = list; + for (int i = 0; i < GetLengthLinkList(list); i++) { + if (node->data == elem) { + return i; + } + } + printf("LocateLinkList:δܶλӦֵ%cԪأ\n", elem); + return -1; +} + +// +bool DestroyLinkList(LinkList &list) { + list->data = DEFAULTELEM; + delete(list); + return true; +} + diff --git a/Code/Code/head/sequence_list.h b/Code/Code/head/sequence_list.h new file mode 100644 index 0000000..ee0fe58 --- /dev/null +++ b/Code/Code/head/sequence_list.h @@ -0,0 +1,178 @@ +#include +#include +#include "head.h" + +// ̬˳ +typedef struct { + element_type data[MAXSIZE]; + int length; +} StaticSequenceList; + +// ̬˳ +typedef struct { + element_type *data; + int max_size, length; +} DynamicSequenceList; + +// ʼ +bool InitSequenceList(StaticSequenceList &list) { + list.length = 0; + return true; +} + +bool InitSequenceList(DynamicSequenceList &list) { + list.length = 0; + list.data = new element_type[MAXSIZE]; + list.max_size = MAXSIZE; + return true; +} + +// ӡ +template +bool PrintSequenceList(List list) { + for (int i = 0; i < list.length; i++) { + printf("%dԪֵΪ%c\n", i + 1, list.data[i]); + } + return true; +} + +// 鳤 +bool IncreaseSequenceList(DynamicSequenceList &list, int length) { + if (length <= 0) { + printf("IncreaseSequenceList:ռӦô0\n"); + return false; + } + // һƬĴ洢ռ + int new_length = list.max_size + length; + void *pointer = realloc(list.data, new_length * sizeof(element_type)); + if (pointer == nullptr) { + printf("IncreaseSequenceList:ʧܣ\n"); + return false; + } + return true; +} + +// п +template +bool EmptySequenceList(List list) { + return list.length == 0; +} + +// +bool InsertSequenceList(StaticSequenceList &list, int index, element_type elem) { + // ̬˳Ѿ˾ͲܲκԪ + if (list.length >= MAXSIZE) { + printf("InsertSequenceList:̬˳ռ䲻㣬ʧܣ\n"); + return false; + } + // λô0ʼԿԲķΧ0list->length + if (index > list.length || index < 0) { + printf("InsertSequenceList:%dΧ\n", index); + return false; + } + // һԪؿʼƣlist->lengthǿյ + for (int i = list.length; i > index; i--) { + list.data[i] = list.data[i - 1]; + } + list.data[index] = elem; + list.length++; + return true; +} + +bool InsertSequenceList(DynamicSequenceList &list, int index, element_type elem) { + if (index > list.length || index < 0) { + printf("InsertDynamicSequenceList:%dΧ\n", index); + return false; + } + // ̬˳ѾˣҪһλ + // Ϊ˱Чһռ䣬Էڼֵĺ + if (list.length >= MAXSIZE) { + bool result = IncreaseSequenceList(list, 1); + if (!result) { + printf("InsertDynamicSequenceList:ռʧܣ\n"); + return false; + } + } + for (int i = list.length; i > index; i--) { + list.data[i] = list.data[i - 1]; + } + list.data[index] = elem; + list.length++; + return true; +} + +// ѭ +template +bool LoopInsertSequenceList(List &list, element_type *elem, int start, int end) { + for (int i = 0; i < end; i++) { + bool result = InsertSequenceList(list, i, elem[i + start]); + if (!result) { + printf("LoopInsertSequenceList:ѭʧܣ\n"); + return false; + } + } + return true; +} + +// ɾ +template +bool DeleteSequenceList(List &list, int index, element_type &elem) { + if (index >= list.length || index < 0) { + printf("DeleteStaticSequenceList:ɾΧ\n"); + return false; + } + elem = list.data[index]; + for (int i = index; i < list.length; i++) { + list.data[i] = list.data[i + 1]; + } + list.length--; + return true; +} + +// ɾԪ +template +int MultiDeleteSequenceList(List &list, int index, int length, element_type *elem) { + if (index + length >= list.length || index < 0) { + printf("MultiDeleteSequenceList:ɾΧ\n"); + return 1; + } + for (int i = index; i < list.length - length; i++) { + if (i < index + length) { + elem[i - index] = list.data[i]; + } + list.data[i] = list.data[i + length]; + } + list.length -= length; + return 0; +} + +// λ˳Ԫ +template +element_type GetSequenceListElem(List list, int index) { + if (index >= list.length || index < 0) { + printf("GetSequenceListElement:%dΧ\n", index); + return DEFAULTELEM; + } + return list.data[index]; +} + +// ֵ˳ +template +int LocateSequenceList(List list, element_type elem) { + for (int i = 0; i < list.length; i++) { + if (list.data[i] == elem) { + return i; + } + } + printf("LocateSequenceListElement:δܶλӦֵ%cԪأ\n", elem); + return -1; +} + +// ٶ̬˳ +int DestroyDynamicSequenceList(DynamicSequenceList &list) { + delete(list.data); + return 0; +} + + + diff --git a/Code/Code/head/sequence_stack.h b/Code/Code/head/sequence_stack.h new file mode 100644 index 0000000..9464e23 --- /dev/null +++ b/Code/Code/head/sequence_stack.h @@ -0,0 +1,65 @@ +#include +#include +#include "head.h" + +// ˳ջ +typedef struct { + // ջԪ + element_type data[MAXSIZE]; + // ջָ + int top; + // + int max_size; +} SequenceStack; + +// ʼ +bool InitSequenceStack(SequenceStack &stack) { + stack.top = -1; + stack.max_size = MAXSIZE; + return true; +} + +// п +bool EmptySequenceStack(SequenceStack stack) { + return stack.top == -1; +} + +// +bool FullSequenceStack(SequenceStack stack) { + return stack.top == stack.max_size - 1; +} + +// ջ +int LengthSequenceStack(SequenceStack stack) { + return stack.top + 1; +} + +// ջ +bool PushSequenceStack(SequenceStack &stack, element_type elem){ + if(FullSequenceStack(stack)){ + printf("PushSequenceStack:ջ޷ջ\n"); + return false; + } + // Լٽջ + stack.data[++stack.top] = elem; + return true; +} + +// ջ +element_type PopSequenceStack(SequenceStack &stack){ + if(EmptySequenceStack(stack)){ + printf("PopSequenceStack:ջ޷ջ\n"); + return DEFAULTELEM; + } + // ȳջԼ + return stack.data[stack.top--]; +} + +// ջ +element_type TopSequenceStack(SequenceStack stack){ + if(EmptySequenceStack(stack)){ + printf("TopSequenceStack:ջ޷ջԪأ\n"); + return DEFAULTELEM; + } + return stack.data[stack.top]; +} \ No newline at end of file diff --git a/Code/Code/head/static_link_list.h b/Code/Code/head/static_link_list.h new file mode 100644 index 0000000..096d05c --- /dev/null +++ b/Code/Code/head/static_link_list.h @@ -0,0 +1,11 @@ +#include +#include +#include "head.h" + +// ̬ +typedef struct { + // Ԫ + element_type data; + // ± + int next; +} StaticLinkList[MAXSIZE]; \ No newline at end of file diff --git a/Code/Code/link_list.h b/Code/Code/link_list.h deleted file mode 100644 index 8987157..0000000 --- a/Code/Code/link_list.h +++ /dev/null @@ -1,406 +0,0 @@ -#include -#include -#include "head.h" - -// 单链表结点 -typedef struct LinkListNode { - element_type data; - struct LinkListNode* next; -} LinkListNode, *LinkList; - -// 初始化有头节点单链表 -int InitLinkListWithHead(LinkList list) { - list = (LinkListNode*)malloc(sizeof(LinkListNode)); - if (list == NULL) { - printf("InitLinkListWithHead:初始化分配内存失败!"); - return 1; - } - list->data = NULL; - list->next = NULL; - return 0; -} - -// 初始化无头节点单链表 -int InitLinkListWithoutHead(LinkList list) { - list = NULL; - return 0; -} - -// 判断有头节点单链表是否为空 -int EmptyLinkListWithHead(LinkList list) { - if (list->next == NULL) { - return 1; - } - else { - return 0; - } -} - -// 判断无头节点单链表是否为空 -int EmptyIsLLinkListWithoutHead(LinkList list) { - if (list == NULL) { - return 1; - } - else { - return 0; - } -} - -// 插入有头节点单链表元素 -// 0号节点是头节点 -int InsertLinkListWithHead(LinkList list, int index, element_type elem) { - if (index < 1) { - printf("InsertLinkListWithHead:插入索引值过小!\n"); - return 1; - } - // 定义一个结点指针p指向当前扫描到的结点 - LinkListNode* p; - // 定义一个变量i表示当前扫描到的结点的索引号 - int i = 0; - // 将链表头结点指向p,为第0个结点 - p = list; - // 循环遍历到达指定索引号的单链表的结点 - // 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点 - while (p->next != NULL && i < index-1) { - p = p->next; - i++; - } - // 如果此时i小于index-1,表示遍历完还没有到达对应的索引 - if (i < index-1) { - printf("InsertLinkListWithHead:插入索引值过大!\n"); - return 1; - } - // 此时i==index-1 - LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode)); - if (s == NULL) { - printf("InsertLinkListWithHead:分配内存失败!\n"); - return 1; - } - s->data = elem; - // 将p原来的后继给新的结点 - s->next = p->next; - p->next = s; - return 0; -} - -// 插入无头节点单链表元素 -int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) { - if (index < 0) { - printf("InsertLinkListWithoutHead:插入索引值过小!\n"); - return 1; - } - if (index == 0) { - LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode)); - if (s == NULL) { - printf("InsertLinkListWithoutHead:分配内存失败!\n"); - return 1; - } - s->data = elem; - // 将s的后继设为list指针 - s->next = list; - // 将list指针设置为s指针 - list = s; - return 0; - } - // 定义一个结点指针p指向当前扫描到的结点 - LinkListNode* p; - // 定义一个变量i表示当前扫描到的结点的索引号 - int i = 0; - // 将链表头结点指向p,为第0个结点 - p = list; - // 循环遍历到达指定索引号的单链表的结点 - // 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点 - while (p->next != NULL && i < index - 1) { - p = p->next; - i++; - } - // 如果此时i小于index-1,表示遍历完还没有到达对应的索引 - if (i < index - 1) { - printf("InsertLinkListWithoutHead:插入索引值过大!\n"); - return 1; - } - // 此时i==index-1 - LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode)); - s->data = elem; - // 将p原来的后继给新的结点 - s->next = p->next; - p->next = s; - return 0; -} - -// 后插入单链表元素 -int InsertNextLinkListNode(LinkListNode* node, element_type elem) { - if (node == NULL) { - printf("InsertNextLinkListNode:插入结点为空!"); - return 1; - } - LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode)); - // 如果分配空间失败 - if (s == NULL) { - printf("InsertNextLinkListNode:分配内存失败!\n"); - return 1; - } - s->data = elem; - s->next = node->next; - node->next = s; - return 0; -} - -// 前插入单链表元素 -int InsertPriorLinkListNode(LinkListNode* node, element_type elem) { - if (node == NULL) { - printf("InsertPriorLinkListNode:插入结点为空!"); - return 1; - } - LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode)); - // 如果分配空间失败 - if (s == NULL) { - printf("InsertPriorLinkListNode:分配内存失败!\n"); - return 1; - } - s->next = node->next; - node->next = s; - s->data = node->data; - node->data = elem; - return 0; -} - -// 删除有头节点单链表元素 -int DeleteLinkListWithHead(LinkList list, int index, element_type *elem) { - if (index < 1) { - printf("DeleteLinkListWithHead:删除索引值过小!\n"); - return 1; - } - // p指向当前扫描的结点 - LinkListNode* p; - // i表示当前指向的是第几个结点 - int i = 0; - // 指向头结点 - p = list; - while (p != NULL && i < index - 1) { - p = p->next; - i++; - } - if (p == NULL || p->next == NULL) { - printf("DeleteLinkListWithHead:删除索引值过大!\n"); - return 1; - } - // q指向被删除的结点 - LinkListNode* q = p->next; - // 获取删除的元素数据 - *elem = q->data; - // 将q结点从链表中断开 - p->next = q->next; - free(q); - return 0; -} - -// 删除无头节点单链表元素 -int DeleteLinkListWithoutHead(LinkList list, int index, element_type* elem) { - if (index < 0) { - printf("DeleteLinkListWithHead:删除索引值过小!\n"); - return 1; - } - // p指向当前扫描的结点 - LinkListNode* p; - // i表示当前指向的是第几个结点 - int i = 0; - // 指向头结点 - p = list; - // 如果删除第一个第0号结点 - if (index == 0) { - list = p->next; - free(p); - return 0; - } - while (p != NULL && i < index - 1) { - p = p->next; - i++; - } - if (p == NULL || p->next == NULL) { - printf("DeleteLinkListWithHead:删除索引值过大!\n"); - return 1; - } - // q指向被删除的结点 - LinkListNode* q = p->next; - // 获取删除的元素数据 - *elem = q->data; - // 将q结点从链表中断开 - p->next = q->next; - free(q); - return 0; -} - -// 删除单链表元素 -int DeleteLinkListNode(LinkListNode* node) { - if (node == NULL) { - printf("DeleteLinkListNode:本结点是空结点无法删除!"); - return 1; - } - // 如果该结点为最后一个结点,无法找到前驱结点,无法操作 - if (node->next = NULL) { - printf("DeleteLinkListNode:后继结点为空无法操作!"); - return 1; - } - // 指向后继结点 - LinkListNode* p = node->next; - // 交换数据 - node->data = p->data; - // 断开结点 - node->next = p->next; - free(p); - return 0; -} - -// 按位查找单链表元素 -element_type GetLinkListElement(LinkList list, int index) { - if (index < 0) { - printf("GetLinkListElement:查找索引值过小!\n"); - return NULL; - } - // 定义一个结点指针p指向当前扫描到的结点 - LinkListNode* p; - // 定义一个变量i表示当前扫描到的结点的索引号 - int i = 0; - // 将链表头结点指向p,为第0个结点 - p = list; - // 循环遍历到达指定索引号的单链表的结点 - // 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点 - while (p->next != NULL && i < index) { - p = p->next; - i++; - } - // 如果查找索引大于当前扫描索引 - if (i < index) { - printf("GetLinkListElement:查找索引值过大!\n"); - return NULL; - } - return p->data; -} - -// 按位查找单链表结点 -LinkListNode* GetLinkListNode(LinkList list, int index) { - if (index < 0) { - printf("GetLinkListNode:查找索引值过小!\n"); - return NULL; - } - // 定义一个结点指针p指向当前扫描到的结点 - LinkListNode* p; - // 定义一个变量i表示当前扫描到的结点的索引号 - int i = 0; - // 将链表头结点指向p,为第0个结点 - p = list; - // 循环遍历到达指定索引号的单链表的结点 - // 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点 - while (p->next != NULL && i < index) { - p = p->next; - i++; - } - // 如果查找索引大于当前扫描索引 - if (i < index) { - printf("GetLinkListNode:查找索引值过大!\n"); - } - // 如果索引值过大,其p也会指向最后一个NULL,所以返回值都是一样为NULL,不需要单独处理 - return p; -} - - -// 按值查找单链表结点 -LinkListNode* LocateLinkListNode(LinkList list, element_type elem) { - LinkListNode* p = list; - while (p != NULL && p->data != elem) { - p = p->next; - } - return p; -} - -// 求链表长度 -int GetLength(LinkList list) { - int len = 0; - LinkListNode* p = list; - while (p->next != NULL) { - p = p->next; - len++; - } - return len; -} - -// 后插建立带头节点单链表 -LinkList TailBuildLinkListWithHead(LinkList list, int length) { - element_type elem; - list = (LinkList)malloc(sizeof(LinkListNode)); - // s指针为一个中间变量指针,r指针为尾指针(next指向最后一个元素) - LinkListNode* s, * r = list; - int i = 0; - element_type x; - if (length < 1) { - printf("TailBuildLinkListWithHead:输入的单链表长度过小!"); - return NULL; - } - while (i < length) { - scanf("%d", &x); - s = (LinkListNode*)malloc(sizeof(LinkListNode)); - s->data = x; - r->next = s; - r = s; - i++; - } - r->next = NULL; - return list; -} - -// 前插建立带头节点单链表 -LinkList HeadBuildLinkListWithHead(LinkList list, int length) { - element_type elem; - list = (LinkList)malloc(sizeof(LinkListNode)); - // 将单链表尾部设置为NULL - list->next = NULL; - // s指针为一个中间变量指针 - LinkListNode* s; - int i = 0; - element_type x; - if (length < 1) { - printf("HeadBuildLinkListWithHead:输入的单链表长度过小!"); - return NULL; - } - while (i < length) { - scanf("%d", &x); - s = (LinkListNode*)malloc(sizeof(LinkListNode)); - s->data = x; - s->next = list->next; - list->next = s; - i++; - } - return list; -} - -// 初始化有头节点循环单链表 -int InitCircularLinkListWithHead(LinkList list) { - list = (LinkListNode*)malloc(sizeof(LinkListNode)); - if (list == NULL) { - printf("InitCircularLinkListWithHead:初始化分配内存失败!"); - return 1; - } - list->next = list; - return 0; -} - -// 判断有头节点循环单链表是否为空 -int IsCircularLinkListEmptyWithHead(LinkList list) { - if (list->next == list) { - return 1; - } - else { - return 0; - } -} - -// 判断结点是否尾有头节点循环单链表的尾结点 -int IsCircularLinkListEndWithHead(LinkList list, LinkListNode* node) { - if (node->next == list) { - return 1; - } - else { - return 0; - } -} \ No newline at end of file diff --git a/Code/Code/link_queue.h b/Code/Code/link_queue.h deleted file mode 100644 index a14f176..0000000 --- a/Code/Code/link_queue.h +++ /dev/null @@ -1,154 +0,0 @@ -#include -#include -#include "head.h" - -// н -typedef struct LinkQueueNode { - element_type data; - struct LinkQueueNode* next; -} LinkQueueNode; - -// -typedef struct { - // еĶͷָͶβָ - LinkQueueNode* front, * rear; -} LinkQueue; - -// ʼͷڵ -int InitLinkQueueWithHead(LinkQueue* queue) { - // ʼʱͷָͶβָ붼ָͷ - queue->front = queue->rear = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); - if (queue->front) { - queue->front->next = NULL; - } - if (queue->front == NULL || queue->rear == NULL) { - printf("InitLinkQueueWithHead:ʼڴʧܣ"); - return 1; - } - return 0; -} - -// ʼͷڵ -int InitLinkQueueWithoutHead(LinkQueue* queue) { - // ʼʱͷβ㶼ָNULL - queue->front = NULL; - queue->rear = NULL; -} - -// жϴͷǷΪ -int IsLinkQueueEmptyWithHead(LinkQueue* queue) { - // ͷָͶβָָͬһط - if (queue->front == queue->rear) { - return 1; - } - else { - return 0; - } -} - -// жϲͷǷΪ -int IsLinkQueueEmptyWithoutHead(LinkQueue* queue) { - // ͷָͶβָһΪNULL - if (queue->front == NULL) { - return 1; - } - else { - return 0; - } -} - -// ͷڵ -int EnterLinkQueueWithHead(LinkQueue* queue, element_type elem) { - LinkQueueNode* p = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); - if (p) { - // ݸֵ - p->data = elem; - // nextָNULL - p->next = NULL; - // ½뵽rear֮ - queue->rear->next = p; - // ޸βָ - queue->rear = p; - return 0; - } - else { - printf("EnterLinkQueueWithHead:㽨ʧܣ"); - return 1; - } -} - -// ͷڵ -int EnterLinkQueueWithoutHead(LinkQueue* queue, element_type elem) { - LinkQueueNode* p = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); - if (p) { - // ݸֵ - p->data = elem; - // nextָNULL - p->next = NULL; - // ǿնвһԪ - if (queue->front == NULL) { - // ͷָ½Ԫ - queue->front = p; - } - else { - // ½뵽rear֮ - queue->rear->next = p; - } - // ޸βָ - queue->rear = p; - return 0; - } - else { - printf("EnterLinkQueueWithHead:㽨ʧܣ"); - return 1; - } -} - -// ͷӳ -int ExitLinkQueueWithHead(LinkQueue* queue, element_type* elem) { - // ǿն - if (queue->front == queue->rear) { - printf("ExitLinkQueueWithHead:ѿ޷ӣ"); - return 1; - } - // һֵΪͷ㣨ͷ㣩һ - LinkQueueNode* p = queue->front->next; - // صһԪ - *elem = p->data; - // ޸ͷnextָָ - // һ - queue->front->next = p->next; - // һ - if (queue->rear == p) { - queue->rear = queue->front; - } - // mallocĿռͷ - free(p); - return 0; -} - -// ͷӳ -int ExitLinkQueueWithoutHead(LinkQueue* queue, element_type* elem) { - // ǿն - if (queue->front == NULL) { - printf("ExitLinkQueueWithoutHead:ѿ޷ӣ"); - return 1; - } - // һֵΪͷ - LinkQueueNode* p = queue->front; - // صһԪ - *elem = p->data; - // ޸ͷָָ - // һ - queue->front = p->next; - // һ - if (queue->rear == p) { - // ͷβָ붼ֵΪNULL - queue->front = NULL; - queue->rear = NULL; - } - // mallocĿռͷ - free(p); - return 0; -} - diff --git a/Code/Code/link_stack.h b/Code/Code/link_stack.h deleted file mode 100644 index c3f725f..0000000 --- a/Code/Code/link_stack.h +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include -#include "head.h" - -// ջ -typedef struct LinkStackNode { - element_type data; - struct LinkStackNode* next; -} LinkStackNode, * LinkStack; diff --git a/Code/Code/link_string.h b/Code/Code/link_string.h deleted file mode 100644 index 2394ff9..0000000 --- a/Code/Code/link_string.h +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include "head.h" - -// -typedef struct LinkStringNode { - // ÿ洢һλ - char data; - // ÿ洢λ - //char data[4]; - struct LinkStringNode* next; -} LinkStringNode, *LinkString; \ No newline at end of file diff --git a/Code/Code/link_tree.h b/Code/Code/link_tree.h deleted file mode 100644 index ca57f2e..0000000 --- a/Code/Code/link_tree.h +++ /dev/null @@ -1,190 +0,0 @@ -#include -#include -#include "head.h" -#include "sequence_queue.h" - -// -typedef struct LinkTreeNode { - element_type data; - // Һӽ - struct LinkTreeNode* lchild, * rchild; -} LinkTreeNode, *LinkTree; - -// ʼ -int InitLinkTree(LinkTree tree, element_type elem) { - tree->data = elem; - tree->lchild = NULL; - tree->rchild = NULL; - return 0; -} - -// -int InsertLinkTree(LinkTree tree, element_type elem) { - LinkTreeNode* node = (LinkTreeNode*)malloc(sizeof(LinkTreeNode)); - if (node) { - node->data = elem; - node->lchild = NULL; - node->rchild = NULL; - tree->lchild = node; - } - else { - printf("InsertLinkTree:ڴռʧܣ"); - return 1; - } - return 0; -} - -// -int PreorderTraversalLinkTree(LinkTree tree, int(*visit)(LinkTree elem)) { - if (tree != NULL) { - // ʸԪ - int result = visit(tree); - if (result == 1) { - printf("PreorderTraversalLinkTree:Ԫزʧܣ"); - return 1; - } - // ݹ - PreorderTraversalLinkTree(tree->lchild, visit); - PreorderTraversalLinkTree(tree->rchild, visit); - } - return 0; -} - -// -int InorderTraversalLinkTree(LinkTree tree, int(*visit)(LinkTree elem)) { - if (tree != NULL) { - // ݹ - PreorderTraversalLinkTree(tree->lchild, visit); - // ʸԪ - int result = visit(tree); - if (result == 1) { - printf("InorderTraversalLinkTree:Ԫزʧܣ"); - return 1; - } - PreorderTraversalLinkTree(tree->rchild, visit); - } - return 0; -} - -// -int PostorderTraversalLinkTree(LinkTree tree, int(*visit)(LinkTree elem)) { - if (tree != NULL) { - // ݹú - PreorderTraversalLinkTree(tree->lchild, visit); - PreorderTraversalLinkTree(tree->rchild, visit); - // ʸԪ - int result = visit(tree); - if (result == 1) { - printf("PostorderTraversalLinkTree:Ԫزʧܣ"); - return 1; - } - } - return 0; -} - -// -int GetLinkTreeDeepth(LinkTree tree) { - if (tree == NULL) { - return 0; - } - else { - // ݹ - int l = GetLinkTreeDeepth(tree->lchild); - int r = GetLinkTreeDeepth(tree->rchild); - // ʸ - return l > r ? l + 1 : r + 1; - } -} - -// -int LevelorderTraversalLinkTree(LinkTree tree, int(*visit)(LinkTree elem)) { - // - SequenceQueue queue; - // ʼ - InitSequenceQueue(&queue); - // ָԪ - LinkTree p; - // - //EnterSequenceQueue(&queue, tree); - // вѭ - while (IsSequenceQueueEmpty(queue) == 0) { - // ͷ - //ExitSequenceQueue(&queue, &p); - //visit(p); - /*if (p->lchild != NULL) { - EnterSequenceQueue(&queue, p->lchild); - } - if (p->rchild != NULL) { - EnterSequenceQueue(&queue, p->rchild); - }*/ - } -} - -// -LinkTreeNode* TraversalSearchBST(LinkTree tree, element_type elem) { - while (tree != NULL && elem != tree->data) { - if (elem < tree->data) { - tree = tree->lchild; - } - else { - tree = tree->rchild; - } - } - return tree; -} - -// ݹ -LinkTreeNode* RecursiveSearchBST(LinkTree tree, element_type elem) { - if (tree == NULL) { - // Ϊ - return NULL; - } - if (elem = tree->data) { - return tree; - } - else if (elem < tree->data) { - return RecursiveSearchBST(tree->lchild, elem); - } - else { - return RecursiveSearchBST(tree->rchild, elem); - } -} - -// ݹ -int InsertBST(LinkTree tree, element_type elem) { - if (tree == NULL) { - tree = (LinkTree)malloc(sizeof(LinkTree)); - if (tree) { - tree->data = elem; - tree->lchild = tree->rchild = NULL; - return 0; - } - else { - printf("BSTInsert:ռʧܣ"); - return 1; - } - } - // ؼظ - else if (elem == tree->data) { - printf("BSTInsert:ؼظ"); - return 1; - } - else if (elem < tree->data) { - return InsertBST(tree->lchild, elem); - } - else { - return InsertBST(tree->rchild, elem); - } -} - -// ݹؼֽ -int CreateBST(LinkTree tree, element_type elem[], int n) { - tree = NULL; - int i = 0; - while (true) - { - InsertBST(tree, elem[i]); - i++; - } - return 0; -} \ No newline at end of file diff --git a/Code/Code/main.c b/Code/Code/main.c deleted file mode 100644 index c524add..0000000 --- a/Code/Code/main.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - - -int main() -{ - //SequenceListTest(); - //LinkListTest(); - return 0; -} \ No newline at end of file diff --git a/Code/Code/search.h b/Code/Code/search.h deleted file mode 100644 index 82c019f..0000000 --- a/Code/Code/search.h +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -#include "head.h" - -// ֿ -// ֿ -typedef struct { - // Ԫ - element_type max; - // 洢 - int low, high; -} BlockSearchIndex; - diff --git a/Code/Code/sequence_list.h b/Code/Code/sequence_list.h deleted file mode 100644 index 796d733..0000000 --- a/Code/Code/sequence_list.h +++ /dev/null @@ -1,322 +0,0 @@ -#include -#include -#include "head.h" - -#pragma warning(disable:6385) -#pragma warning(disable:6386) - -// 静态顺序表 -typedef struct { - element_type data[MAXSIZE]; - // 长度 - int length; -} StaticSequenceList; - -// 动态顺序表 -typedef struct { - // 给一个指针来分配动态数组 - element_type *data; - // 已分配的最大容量 - int max_size; - // 长度 - int length; -} DynamicSequenceList; - -// 初始化静态顺序表 -int InitStaticSequenceList(StaticSequenceList* list) { - if (list == NULL) { - printf("InitStaticSequenceList:指针指向为NULL!\n"); - return 1; - } - else { - // 初初始化静态顺序表长度为0 - list->length = 0; - return 0; - } -} - -// 初始化动态顺序表 -int InitDynamicSequenceList(DynamicSequenceList* list) { - if (list == NULL) { - printf("InitDynamicSequenceList:指针指向为NULL!\n"); - return 1; - } - else { - // 初初始化动态顺序表长度为0 - list->length = 0; - list->max_size = 0; - // 申请一片连续的存储空间 - element_type* space = (element_type*)malloc(MAXSIZE * sizeof(element_type)); - if (space != NULL) { - list->data = space; - list->max_size = MAXSIZE; - return 0; - } - else { - printf("InitDynamicSequenceList:分配空间失败!\n"); - return 1; - } - } -} - -// 打印静态顺序表 -int PrintfStaticSequenceList(StaticSequenceList list) { - for (int i = 0; i < list.length; i++) { - printf("第%d个元素值为%c\n", i + 1, list.data[i]); - } - return 0; -} - -// 打印动态顺序表 -int PrintfDynamicSequenceList(DynamicSequenceList list) { - for (int i = 0; i < list.length; i++) { - printf("第%d个元素值为%c\n", i + 1, list.data[i]); - } - return 0; -} - -// 分配其他地址增长动态顺序表的数据空间长度 -int OtherIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) { - if (len <= 0) { - printf("OtherIncreaseDynamicSequenceList:申请空间应该大于0!\n"); - return 1; - } - // 申请一片连续的存储空间 - int new_length = list->max_size + len; - element_type* space = (element_type*)malloc(new_length * sizeof(element_type)); - if (space != NULL) { - // 建立中间变量 - list->data = space; - element_type* temp = list->data; - for (int i = 0; i < list->length; i++) { - list->data[i] = temp[i]; - } - list->max_size = new_length; - free(temp); - return 0; - } - else { - printf("OtherIncreaseDynamicSequenceList:重新分配空间失败!\n"); - return 1; - } -} - -// 重新分配地址增长动态顺序表的数据空间长度 -int ReIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) { - if (len <= 0) { - printf("ReIncreaseDynamicSequenceList:申请空间应该大于0!\n"); - return 1; - } - // 申请一片连续的存储空间 - int new_length = list->max_size + len; - element_type* space = (element_type*)realloc(list->data, new_length * sizeof(element_type)); - if (space != NULL) { - list->data = space; - list->max_size += len; - return 0; - } - else { - list->max_size = 0; - list->length = 0; - printf("ReIncreaseDynamicSequenceList:分配其他地址空间失败!\n"); - return 1; - } -} - -// 插入静态顺序表 -int InsertStaticSequenceList(StaticSequenceList* list, int index, element_type elem) { - // 当静态顺序表已经满了就不能插入任何元素 - if (list->length >= MAXSIZE) { - printf("InsertStaticSequenceList:静态顺序表空间不足,插入失败!\n"); - return 1; - } - // 索引位置从0开始,所以可以插入的范围是0到list->length - if (index > list->length || index < 0) { - printf("InsertStaticSequenceList:插入索引%d超过索引范围!\n", index); - return 1; - } - // 从最后一个元素开始交换后移,list->length是空的 - for (int i = list->length; i > index; i--) { - list->data[i] = list->data[i - 1]; - } - list->data[index] = elem; - list->length++; - return 0; -} - -// 插入动态顺序表 -int InsertDynamicSequenceList(DynamicSequenceList* list, int index, element_type elem) { - if (index > list->length || index < 0) { - printf("InsertDynamicSequenceList:插入索引%d超过索引范围!\n", index); - return 1; - } - // 当动态顺序表已经满了,需要新增一个位置 - // 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面 - if (list->length >= MAXSIZE) { - int result = ReIncreaseDynamicSequenceList(list, 1); - if (result == 1) { - printf("InsertDynamicSequenceList:申请空间失败!\n"); - return 1; - } - } - for (int i = list->length; i > index; i--) { - list->data[i] = list->data[i - 1]; - } - list->data[index] = elem; - list->length++; - return 0; -} - -// 循环插入静态顺序表 -int LoopInsertStaticSequenceList(StaticSequenceList* list, element_type* elem, int start, int end) { - for (int i = 0; i < end; i++) { - int result = InsertStaticSequenceList(list, i, elem[i + start]); - if (result == 1) { - printf("LoopInsertStaticSequenceList:循环插入失败!\n"); - return 1; - } - } - return 0; -} - -// 循环插入动态顺序表 -int LoopInsertDynamicSequenceList(DynamicSequenceList* list, element_type* elem, int start, int end) { - for (int i = 0; i < end; i++) { - int result = InsertDynamicSequenceList(list, i, elem[i + start]); - if (result == 1) { - printf("LoopInsertDynamicSequenceList:循环插入失败!\n"); - return 1; - } - } - return 0; -} - -// 删除静态顺序表 -int DeleteStaticSequenceList(StaticSequenceList* list, int index, element_type *elem) { - if (index >= list->length || index < 0) { - printf("DeleteStaticSequenceList:删除索引超过索引范围!\n"); - return 1; - } - *elem = list->data[index]; - for (int i = index; i < list->length; i++) { - list->data[i] = list->data[i + 1]; - } - list->length--; - return 0; -} - -// 删除动态顺序表 -int DeleteDynamicSequenceList(DynamicSequenceList* list, int index, element_type *elem) { - if (index >= list->length || index < 0) { - printf("DeleteDynamicSequenceList:删除索引超过索引范围!\n"); - return 1; - } - *elem = list->data[index]; - for (int i = index; i < list->length; i++) { - list->data[i] = list->data[i + 1]; - } - list->length--; - return 0; -} - -// 删除多个静态顺序表 -int MultiDeleteStaticSequenceList(StaticSequenceList* list, int index, int len, element_type* elem) { - if (index + len >= list->length || index < 0) { - printf("MultiDeleteStaticSequenceList:删除索引超过索引范围!\n"); - return 1; - } - for (int i = index; i < list->length - len; i++) { - if (i < index + len) { - elem[i - index] = list->data[i]; - } - list->data[i] = list->data[i + len]; - } - list->length -= len; - return 0; -} - -// 删除多个动态顺序表 -int MultiDeleteDynamicSequenceList(DynamicSequenceList* list, int index, int len, element_type* elem) { - if (index + len >= list->length || index < 0) { - printf("MultiDeleteDynamicSequenceList:删除索引超过索引范围!\n"); - return 1; - } - for (int i = index; i < list->length - len; i++) { - if (i < index + len) { - elem[i - index] = list->data[i]; - } - list->data[i] = list->data[i + len]; - } - list->length -= len; - return 0; -} - -// 按位查找静态顺序表元素 -element_type GetStaticSequenceListElement(StaticSequenceList list, int index) { - if (index >= list.length || index < 0) { - printf("GetStaticSequenceListElement:查找索引超过索引范围!\n"); - return DEFAULTELEM; - } - return list.data[index]; -} - -// 按位查找动态顺序表元素 -element_type GetDynamicSequenceListElement(DynamicSequenceList list, int index) { - if (index >= list.length || index < 0) { - printf("GetDynamicSequenceListElement:查找索引超过索引范围!\n"); - return DEFAULTELEM; - } - return list.data[index]; -} - -// 按值查找静态顺序表索引 -int LocateStaticSequenceListElement(StaticSequenceList list, element_type elem) { - for (int i = 0; i < list.length; i++) { - if (list.data[i] == elem) { - return i; - } - } - printf("LocateStaticSequenceListElement:未能定位到对应值的元素!\n"); - return -1; -} - -// 按值查找动态顺序表索引 -int LocateDynamicSequenceListElement(DynamicSequenceList list, element_type elem) { - for (int i = 0; i < list.length; i++) { - if (list.data[i] == elem) { - return i; - } - } - printf("LocateDynamicSequenceListElement:未能定位到对应值的元素!\n"); - return -1; -} - -// 判空静态顺序表 -int EmptyStaticSequenceList(StaticSequenceList list) { - if (list.length == 0) { - return 1; - } - else { - return 0; - } -} - -// 判空动态顺序表 -int EmptyDynamicSequenceList(DynamicSequenceList list) { - if (list.length == 0) { - return 1; - } - else { - return 0; - } -} - -// 销毁动态顺序表 -int DestroyDynamicSequenceList(DynamicSequenceList* list) { - if (list != NULL) { - free(list->data); - } - list = NULL; - return 0; -} - diff --git a/Code/Code/sequence_queue.h b/Code/Code/sequence_queue.h deleted file mode 100644 index 673fb9a..0000000 --- a/Code/Code/sequence_queue.h +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include "head.h" - -// ˳ -typedef struct { - // ŶԪ - element_type data[MAXSIZE]; - // ͷָβָ룬ָǶӿʼ - int front, rear; -} SequenceQueue; - -// ʼ˳ -int InitSequenceQueue(SequenceQueue* queue) { - // ׶βָ0 - queue->rear = queue->front = 0; -} - -// ˳Ԫ -int EnterSequenceQueue(SequenceQueue* queue, element_type elem) { - // βָһǶͷָǾǶ - if ((queue->rear + 1) % MAXSIZE == queue->front) { - printf("EnterSequenceQueue:޷ӣ"); - return 1; - } - // ݶβָԪ - queue->data[queue->rear] = elem; - // Զβָȡ࣬ӶöβָָǰĿпռ - queue->rear = (queue->rear + 1) % MAXSIZE; - return 0; -} - -// ˳Ԫس -int ExitSequenceQueue(SequenceQueue* queue, element_type* elem) { - // βָǶͷָǾǶӿ - if (queue->rear == queue->front) { - printf("ExitSequenceQueue:ѿ޷ӣ"); - return 1; - } - // ݶͷָɾԪ - *elem = queue->data[queue->front]; - // Զͷָȡ࣬ӶöͷָָкĿпռ - queue->front = (queue->front + 1) % MAXSIZE; - return 0; -} - -// ȡ˳жͷԪ -int GetSequenceQueueHead(SequenceQueue* queue, element_type* elem) { - // βָǶͷָǾǶӿ - if (queue->rear == queue->front) { - printf("GetSequenceQueueHead:ѿ޷ȡԪأ"); - return 1; - } - // ݶͷָ븳ֵԪ - *elem = queue->data[queue->front]; - return 0; -} - -// ж˳ǷΪ -int IsSequenceQueueEmpty(SequenceQueue queue) { - if (queue.rear == queue.front) { - return 1; - } - return 0; -} \ No newline at end of file diff --git a/Code/Code/sequence_stack.h b/Code/Code/sequence_stack.h deleted file mode 100644 index f28ed90..0000000 --- a/Code/Code/sequence_stack.h +++ /dev/null @@ -1,99 +0,0 @@ -#include -#include -#include "head.h" - -// ˳ջ -typedef struct { - // ̬洢ջԪ - element_type data[MAXSIZE]; - // ջָ - int top; -} SequenceStack; - -// ʼ˳ջ -int InitSequenceStack(SequenceStack* stack) { - stack->top = -1; - return 0; -} - -// ж˳ջǷΪ -int IsSequenceStackEmpty(SequenceStack stack) { - if (stack.top == -1) { - return 1; - } - else { - return 0; - } -} - -// Ԫ˳ջ -int PushSequenceStack(SequenceStack* stack, element_type elem) { - // ջ޷Ԫ - if (stack->top == MAXSIZE - 1) { - printf("PushSequenceStack:ջ޷Ԫأ"); - return 1; - } - //// ָһ - //stack->top += 1; - //// Ԫջ - //stack->data[stack->top] = elem; - // һջ - stack->data[++stack->top] = elem; - return 0; -} - -// Ԫص˳ջ -int PopSequenceStack(SequenceStack* stack, element_type* elem) { - // ջ޷ɾԪ - if (stack->top == -1) { - printf("PopSequenceStack:ջ޷Ԫأ"); - return 1; - } - // ٳջ - *elem = stack->data[stack->top--]; - return 0; -} - -// ȡ˳ջջԪ -int GetSequenceStackTop(SequenceStack* stack, element_type* elem) { - // ջ޷ȡԪ - if (stack->top == -1) { - printf("PopSequenceStack:ջ޷ȡԪأ"); - return 1; - } - // ٳջ - *elem = stack->data[stack->top]; - return 0; -} - -// ˳ջʵƥ -int BracketCheck(char str[], int length) { - SequenceStack s; - // ʼջ - InitSequenceStack(&s); - for (int i = 0; i < length; i++) { - // ɨ赽 - if (str[i] == '{' || str[i] == '[' || str[i] == '(') { - PushSequenceStack(&s, str[i]); - } - else { - // ջ - if (IsSequenceStackEmpty(s)==1) { - return 1; - } - } - // һջԪر - char t; - PopSequenceStack(&s, &t); - if (str[i] == ')' && t != '(') { - return 1; - } - if (str[i] == ']' && t != '[') { - return 1; - } - if (str[i] == '}' && t != '{') { - return 1; - } - } - return 0; -} \ No newline at end of file diff --git a/Code/Code/sequence_string.h b/Code/Code/sequence_string.h deleted file mode 100644 index 669d341..0000000 --- a/Code/Code/sequence_string.h +++ /dev/null @@ -1,104 +0,0 @@ -#include -#include -#include "head.h" - -// ̬˳ -typedef struct { - char data[MAXSIZE]; - // - int length; -} StaticSequenceString; - -// ̬˳ -typedef struct { - char* data; - int length; -} DynamicSequenceString; - -// ̬˳Ӵ -int SubStaticSequenceString(StaticSequenceString string, StaticSequenceString *substring, int index, int length) { - // ӴԽ - if (index + length > string.length) { - printf("StaticSequenceString:ӴΧԽ磡"); - return 1; - } - for (int i = 0; i < length; i++) { - substring->data[i] = string.data[index + i]; - } - substring->length = length; - return 0; -} - -// ԱȾ̬˳ַ -int CompareStaticSequenceString(StaticSequenceString string1, StaticSequenceString string2) { - for (int i = 0; i <= string1.length && i <= string2.length; i++) { - if (string1.data[i] != string2.data[i]) { - return string1.data[i] - string2.data[i]; - } - } - // ɨַͬ򳤶ȳַ - return string1.length - string2.length; -} - -// ھ̬˳ַжλӴ -// дӴͬӴ򷵻еһγֵλã򷵻-1 -int LocateStaticSequenceString(StaticSequenceString mainstring, StaticSequenceString substring) { - int i = 0; - // ݴӴ - StaticSequenceString temp; - while (i < mainstring.length - substring.length + 1) { - // иӴͬȵӴ - SubStaticSequenceString(mainstring, &temp, i, substring.length); - // иӴӴȾͺһλиӴ - if (CompareStaticSequenceString(substring, temp) != 0) { - i++; - } - // 򷵻صǰ - else { - return i; - } - } - return -1; -} - -// ģʽnext -// 0ֵ -int GetNext(StaticSequenceString string, int *next[]) { - int i = 1, j = 0; - next[i] = 0; - while (i < string.length) { - if (j == 0 || string.data[i] == string.data[j]) { - ++i, ++j; - *next[i] = j; - } - else { - if (next[j]) { - // ģʽ - j = *next[j]; - } - } - } - return 0; -} - -// KMP㷨 -int KMP(StaticSequenceString string1, StaticSequenceString string2) { - int i = 1, j = 1; - int length = string2.length + 1; - int* next = (int*)malloc(length*sizeof(int)); - GetNext(string2, &next); - while (i <= string1.length && j <= string2.length) { - if (j == 0 || string1.data[i] == string2.data[j]) { - i++, j++; - } - else { - // ģʽַ - j = next[i]; - } - } - if (j > string2.length) { - return i - string2.length; - } - return -1; - free(next); -} \ No newline at end of file diff --git a/Code/Code/sequence_tree.h b/Code/Code/sequence_tree.h deleted file mode 100644 index 0be10ad..0000000 --- a/Code/Code/sequence_tree.h +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include "head.h" - -// ˫ -typedef struct ParentTreeNode { - element_type data; - // ˫λ - int parent; -} ParentTreeNode; - -typedef struct { - // ˫ - ParentTreeNode nodes[MAXSIZE]; - // - int n; -} ParentTree; - -// ӽ -typedef struct ChildTreeChildNode { - // ӽеλ - int child; - // һ - struct ChildTreeChildNode* next; -} ChildTreeChildNode; - -// -typedef struct { - element_type data; - // ָһ - ChildTreeChildNode* first_child; -} ChildTreeNode; - -// -typedef struct { - ChildTreeNode nodes[MAXSIZE]; - // λ - int n, r; -} ChildTree; - -// ֵ -typedef struct ChildSiblingTreeNode { - element_type data; - // һֵָ - struct ChildSiblingTreeNode* first_child, * next_sibling; -} ChildSiblingTreeNode, * ChildSiblingTree; diff --git a/Code/Code/sort.h b/Code/Code/sort.h deleted file mode 100644 index 09bc3cb..0000000 --- a/Code/Code/sort.h +++ /dev/null @@ -1,259 +0,0 @@ -#include -#include -#include "head.h" - -// ֱӲ -int DirectInsertSort(element_type data[], int length) { - int i, j; - element_type temp; - // ѭ - for (i = 1; i < length; i++) { - // data[i]ؼСǰһ - if (data[i] < data[i - 1]) { - // tempݴdata[i] - temp = data[i]; - // ǰѾźԪ - for (j = i - 1; j >= 0 && data[j] > temp; --j) { - // Ԫشtempһλ - data[j + 1] = data[j]; - } - // ֵλ - data[j + 1] = temp; - } - } - return 0; -} - -// ۰ -int BinaryInsertSort(element_type data[], int length) { - int i, j, low, high, mid; - // νdata[2]data[n-1]뵽ǰѾ - for (i = 2; i <= length; i++) { - // data[i]ݴ浽data[0] - data[0] = data[i]; - // ۰ҵķΧ - low = 1; - high = i - 1; - while (low <= high) { - // ȡм - mid = (low + high) / 2; - if (data[mid] > data[0]) { - // ӱ - high = mid - 1; - } - else { - // Ұӱ - low = mid + 1; - } - } - // ͳһԪأճλ - for (j = 1; j >= high + 1; --j) { - data[j + 1] = data[j]; - } - // Ԫ - data[high + 1] = data[0]; - } - return 0; -} - -// ϣ -int ShellSort(element_type data[], int length) { - // dǰֵ - int d, i, j; - // data[0]ֻݴݣj<=0ʱ͵˲λ - for (d = length / 2; d >= 1; d = d / 2) { - for (i = d + 1; i <= length; ++i) { - // Ҫdata[i]뵽ӱ - if (data[i] < data[i - d]) { - // ݴ浽data[0] - data[0] = data[i]; - for (j = i - d; j > 0 && data[0] < data[j]; j -= d) { - // ¼ƣѰҲλ - data[j + d] = data[j]; - } - // - data[j + d] = data[0]; - } - } - } - return 0; -} - -// ð -int BubbleSort(element_type data[], int length) { - for (int i = 0; i < length - 1; i++) { - // һ־ʾðǷ - int flag = 0; - element_type temp; - for (int j = length - 1; j > i; j--) { - if (data[j - 1] > data[j]) { - temp = data[j - 1]; - data[j - 1] = data[j]; - data[j] = temp; - flag = 1; - } - } - // αûзʹѾ - if (flag == 0) { - return 0; - } - } - return 0; -} - -// 򻮷 -int QuickPart(element_type data[], int low, int high) { - // ѡdata[low]Ϊ׼ - int pivot = data[low]; - // lowhigh׼λ - while (low < high) { - // ǰhighָԪشڻ׼ƶhighָ - while (low < high && data[high] >= pivot) { - --high; - } - // ȱ׼СԪƶlowָԪ - data[low] = data[high]; - // ƶָ - // ǰlowָԪСڻ׼ƶlowָ - while (low < high && data[low] <= pivot) { - ++low; - } - // ȱ׼ԪƶhighָԪ - data[high] = data[low]; - } - // lowhighָͬһԪʱͰѻ׼ŵλ - data[low] = pivot; - // شŻ׼Ԫصλ - return low; -} - -// -int QuickSort(element_type data[], int low, int high) { - // ݹ,low=highֻһԪ - if (low < high) { - // л - int pivot = QuickPart(data, low, high); - // Իֵӱд - QuickSort(data, low, pivot - 1); - // Իֵӱд - QuickSort(data, pivot + 1, high); - } - return 0; -} - -// ѡ -int SimpleSelectSort(element_type data[], int length) { - element_type temp; - // һn-1 - for (int i = 0; i < length - 1; i++) { - // ¼СԪصλ - int min = i; - // data[i,length-1]ѡСԪ - for (int j = i + 1; j < length; j++) { - // СԪλ - if (data[j] < data[min]) { - min = j; - } - } - // ǰСԪصֵڵǰָλþͽ - if (min != i) { - temp = i; - i = min; - min = i; - } - } -} - -// -int BuildMaxHeap(element_type data[], int length) { - for (int i = length / 2; i > 0; i--) { - MaxHeadAdjust(data, i, length); - } - return 0; -} - -// nodeΪΪ -int MaxHeadAdjust(element_type data[], int node, int length) { - // ʹdata[0]ݴ - data[0] = data[node]; - // keyϴӽɸѡ - for (int i = 2 * node; i <= length; i *= 2) { - // ȡkeyϴӽڵ± - if (i < length && data[i] < data[i + 1]) { - i++; - } - // ӽõ - if (data[0] >= data[i]) { - break; - } - else { - // data[i]ŵ - data[node] = data[i]; - // ޸nodeֵԼɸѡ - node = i; - } - } - // ɸѡֵŵλ - data[node] = data[0]; - return 0; -} - -// ѵĶ -int MaxHeapSort(element_type data[], int length) { - // ʼһ - BuildMaxHeap(data, length); - // һ - element_type temp; - // n-1˽ͽĹ - for (int i = length; i > 1; i--) { - // ѶԪѵԪػ - temp = data[i]; - data[i] = data[1]; - data[1] = temp; - // ʣĴԪصΪ - MaxHeadAdjust(data, i, i - 1); - } - return 0; -} - -// 鲢 -element_type* aid = (element_type*)malloc(MAXSIZE * sizeof(element_type)); - -// data[low,mid]data[mid+1,high]򣬽ֹ鲢 -int Merge(element_type data[], int low, int mid, int high) { - int i, j, k; - for (k - low; k <= high; k++) { - // dataԪظƵ - aid[k] = data[k]; - } - for (i = low, j = mid + 1, k = i; i <= mid && j <= high; k++) { - if (aid[i] <= aid[j]) { - data[k] = aid[i++]; - } - else { - data[k] = aid[j++]; - } - } - while (i <= mid) { - data[k++] = aid[i++]; - } - while (j <= high) { - data[k++] = aid[j++]; - } - return 0; -} - -// 鲢 -int MergeSort(element_type data[], int low, int high) { - if (low < high) { - // м仮 - int mid = (low + high) / 2; - // 벿ֹ鲢 - MergeSort(data, low, mid); - // Ұ벿ֹ鲢 - MergeSort(data, mid + 1, high); - // һι鲢ȫ - Merge(data, low, mid, high); - } - return 0; -} \ No newline at end of file diff --git a/Code/Code/source/main.cpp b/Code/Code/source/main.cpp new file mode 100644 index 0000000..932da7d --- /dev/null +++ b/Code/Code/source/main.cpp @@ -0,0 +1,8 @@ +#include "test.cpp" + +int main() +{ + //SequenceListTest(); + LinkListTest(); + return 0; +} \ No newline at end of file diff --git a/Code/Code/source/test.cpp b/Code/Code/source/test.cpp new file mode 100644 index 0000000..540f5d9 --- /dev/null +++ b/Code/Code/source/test.cpp @@ -0,0 +1,49 @@ +// ļ +#include +#include "../head/sequence_list.h" +#include "../head/link_list.h" +#include "../head/double_link_list.h" +#include "../head/static_link_list.h" +#include "../head/sequence_stack.h" + +using namespace std; + +bool SequenceListTest() { + DynamicSequenceList list; + InitSequenceList(list); + element_type a[6] = {'1','2','3','4','5','6'}; + LoopInsertSequenceList(list, a, 0, 6); + //printf("%d", list.length); + PrintSequenceList(list); + printf("\n"); + int len = 2; + element_type elem[2]; + MultiDeleteSequenceList(list, 0, len, elem); + PrintSequenceList(list); + for (int i = 0; i < len; i++) { + printf("%c\n", elem[i]); + } + cout << EmptySequenceList(list) << endl; + return true; +} + +bool LinkListTest() { + element_type a[6] = { '0', '1','2','3','4','5' }; + LinkList list = InitLinkList(); + cout << EmptyLinkList(list) << endl; + //InsertLinkListWithoutHead(list, 0, '2'); + InsertLinkListWithoutHead(list, 0, '4'); + //InsertLinkListWithHead(list, 1, '5'); + PriorInsertLinkList(list, a, 1, 5); + PrintLinkList(list); + int length = 2; + //element_type* data = DeleteLinkListWithHead(list, 2, length); + element_type* data = DeleteLinkListWithoutHead(list, 2, length); + PrintLinkList(list); + for (int i = 0; i < length; i++) { + printf("%dΪ%c\n", i, data[i]); + } + printf("Ϊ%d", GetLengthLinkList(list)); + printf("%cַ%d", '3', LocateLinkList(list, '3')); + return true; +} \ No newline at end of file diff --git a/Code/Code/static_link_list.h b/Code/Code/static_link_list.h deleted file mode 100644 index 0f64e6c..0000000 --- a/Code/Code/static_link_list.h +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -#include "head.h" - -// ̬ -typedef struct StaticLinkNode { - element_type data; - // һԪص± - int next; -} StaticLinkNode; -// ̬ -typedef StaticLinkNode StaticLinkList[MAXSIZE]; \ No newline at end of file diff --git a/Code/Code/test.c b/Code/Code/test.c deleted file mode 100644 index a3b0034..0000000 --- a/Code/Code/test.c +++ /dev/null @@ -1,40 +0,0 @@ -// ļ - -#include "sequence_list.h" -#include "link_list.h" - -int SequenceListTest() { - DynamicSequenceList list; - InitDynamicSequenceList(&list); - element_type a[6] = {'1','2','3','4','5','6'}; - LoopInsertDynamicSequenceList(&list, a, 0, 6); - element_type b[3] = { 9, 'a', 'e' }; - LoopInsertDynamicSequenceList(&list, b, 1, 2); - //printf("%d", list.length); - PrintfDynamicSequenceList(list); - printf("\n"); - int len = 2; - element_type elem[2]; - MultiDeleteDynamicSequenceList(&list, 0, len, elem); - PrintfDynamicSequenceList(list); - for (int i = 0; i < len; i++) { - printf("%c\n", elem[i]); - } - /*DynamicSequenceList dlist; - InitDynamicSequenceList(&dlist); - OtherIncreaseDynamicSequenceList(&dlist, 15); - printf("%d", dlist.max_size);*/ - /*int index = LocateDynamicSequenceListElement(list, '5'); - printf("%d", index); - DestroyDynamicSequenceList(&list);*/ - return 0; -} - -int LinkListTest() { - //LinkList list; - //InitLinkListWithHead(list); - //int empty = EmptyLinkListWithHead(list); - //printf("%d", empty); - return 0; -} - diff --git a/Code/Code/thread_tree.h b/Code/Code/thread_tree.h deleted file mode 100644 index a8e4a40..0000000 --- a/Code/Code/thread_tree.h +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include "head.h" - -// -typedef struct ThreadTreeNode { - element_type data; - struct ThreadTreeNode* lchild, * rchild; - int ltag, rtag; -} ThreadTreeNode, *ThreadTree; - -// ȫֱָǰʵǰ -ThreadTreeNode* pre = NULL; -// -int InorderThread(ThreadTreeNode* node) { - // Ϊգǰ - if (node->lchild == NULL) { - node->lchild = pre; - node->ltag = 1; - } - if (pre != NULL && pre->rchild==NULL) { - // ǰĺ - pre->rchild = node; - pre->rtag = 1; - } - pre = node; - return 0; -} - -// ҵnodeΪеһĽ -ThreadTreeNode* FristInOrderNode(ThreadTreeNode* node) { - //ѭҵ½ǽ㣬һҶӽ - while (node->ltag == 0) { - node = node->lchild; - } - return node; -} - -// ҵnodeĺ̽ -ThreadTreeNode* NextInOrderNode(ThreadTreeNode* node) { - // - if (node->rtag == 0) { - return FristInOrderNode(node->rchild); - } - else { - return node->rchild; - } -} - -// ʵַǵݹ -int InorderTraversalThreadTree(ThreadTree tree, int(*visit)(ThreadTreeNode* node)) { - for (ThreadTreeNode* p = FristInOrderNode(tree); p != NULL; p = NextInOrderNode(p)) { - visit(p); - } - return 0; -} - -// ҵnodeΪǰһĽ -ThreadTreeNode* LastInOrderNode(ThreadTreeNode* node) { - //ѭҵ½ǽ㣬һҶӽ - while (node->rtag == 0) { - node = node->rchild; - } - return node; -} - -// ҵnodeǰ -ThreadTreeNode* PreInOrderNode(ThreadTreeNode* node) { - // - if (node->ltag == 0) { - return LastInOrderNode(node->rchild); - } - else { - return node->lchild; - } -} - -// ʵַǵݹ -int ReverseInorderTraversalThreadTree(ThreadTree tree, int(*visit)(ThreadTreeNode* node)) { - for (ThreadTreeNode* p = LastInOrderNode(tree); p != NULL; p = PreInOrderNode(p)) { - visit(p); - } - return 0; -} \ No newline at end of file diff --git a/Data-Structrue/2-stack-ex.md b/Data-Structrue/2-stack-ex.md new file mode 100644 index 0000000..5a09bef --- /dev/null +++ b/Data-Structrue/2-stack-ex.md @@ -0,0 +1,45 @@ +# 栈习题 + +## 概念 + +**例题** 栈和队列具有相同的()。 + +$A.$抽象数据类型 + +$B.$逻辑结构 + +$C.$存储结构 + +$D.$运算 + +解:$B$。栈和队列的逻辑结构都是相同的,都属于线性结构,只是它们对数据的运算不同。 + +## 结构选择 + +**例题** 设链表不带头结点且所有操作均在表头进行,则下列最不适合作为链栈的是()。 + +$A.$只有表头结点指针,没有表尾指针的双向循环链表 + +$B.$只有表尾结点指针,没有表头指针的双向循环链表 + +$C.$只有表头结点指针,没有表尾指针的单向循环链表 + +$D.$只有表尾结点指针,没有表头指针的单向循环链表 + +解:$C$。对于双向循环链表,不管是表头指针还是表尾指针,都可以很方便地找到表头结点,方便在表头做插入或删除操作。而单循环链表通过尾指针的$next$指针可以很方便地找到表头结点,但通过头指针指向的就是头,对于头的操作则需要遍历一次链表到表尾。对于$C$,插入和删除结点后,找尾结点需要花费$O(n)$的时间。 + +## 出栈排列 + +如果有$n$个不同的元素进栈,出栈元素不同排列的个数为$\dfrac{1}{n+1}C_{2n}^n=\dfrac{1}{n+1}\dfrac{(2n)!}{n!\times n!}$,这就是卡特兰数。 + +**例题** $3$个不同元素依次进栈,能得到()种不同的出栈序列。 + +$A.4$ + +$B.5$ + +$C.6$ + +$D.7$ + +解:$B$。根据公式可得$\dfrac{6\times5\times4}{4\times3\times2\times1}=5$。 diff --git a/Data-Structrue/3-queue.md b/Data-Structrue/3-queue.md index ef1d7cc..45c819f 100644 --- a/Data-Structrue/3-queue.md +++ b/Data-Structrue/3-queue.md @@ -1,6 +1,6 @@ # 队列 -队列是只允许一端进行插入(入队),一端进行删除(出队)的线性表。即先进先出FIFO。 +队列是只允许一端进行插入(入队或进队),一端进行删除(出队或离队)的线性表。即先进先出$FIFO$。 队列允许插入的一端就是队尾,允许删除的一端就是队头。 @@ -16,17 +16,17 @@ 解决的方法就是使用模运算,将队尾指针不仅仅是加一,而是加一后再取整个静态数组大小MAXSIZE的模,这样如果队列尾指针超过了范围也仍能回到最开始插入数据。这时候物理结构虽然是线性的,而逻辑上已经变成了环型的了。 -所以与此同时,队列已满的条件也不再是队尾指针=MAXSIZE了,而是队尾指针的下一个指针是队头指针,这里最后一个存储单元是不能存储数据的,因为如果存储了数据那么头指针就等于尾指针,这在我们的定义中是空队列的意思,会混淆,所以必须牺牲一个存储单元。 +所以与此同时,队列已满的条件也不再是队尾指针$=MAXSIZE$了,而是队尾指针的下一个指针是队头指针,这里最后一个存储单元是不能存储数据的,因为如果存储了数据那么头指针就等于尾指针,这在我们的定义中是空队列的意思,会混淆,所以必须牺牲一个存储单元。 -如果我们最开始定义时,让队首指针指向-1,队尾指针指向0,则可以相等。 +如果我们最开始定义时,让队首指针指向$-1$,队尾指针指向$0$,则可以相等。 -从而队列元素个数=(rear+MAXSIZE-front)%MAXSIZE。 +从而队列元素个数$=(rear+MAXSIZE-front)\%MAXSIZE$。 #### 顺序队列删除 -当如果我们必须保证所有的存储空间被利用,可以定义一个size表明队列当前的长度,就可以完全利用所有空间。 +当如果我们必须保证所有的存储空间被利用,可以定义一个$size$表明队列当前的长度,就可以完全利用所有空间。 -同理我们可以定义一个int类型的tag,当进行删除操作就置tag为0,插入操作时置tag为1,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。 +同理我们可以定义一个$int$类型的$tag$,当进行删除操作就置$tag$为$0$,插入操作时置$tag$为$1$,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。 ## 链队 @@ -42,4 +42,4 @@ + 树的层次遍历。 + 图的广度优先遍历。 -+ 进程争用FCFS策略。 ++ 进程争用$FCFS$策略。