mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-02-09 05:44:32 +08:00
更新栈
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -9,4 +9,5 @@
|
||||
*Debug/
|
||||
*x64/
|
||||
*out/
|
||||
*cmake-build-debug/
|
||||
*cmake-build-debug/
|
||||
*cmake-build-debug-mingw/
|
||||
@@ -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}"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// 初始化最大长度
|
||||
#define MAXSIZE 5
|
||||
// 定义默认值
|
||||
#define DEFAULTELEM '0'
|
||||
#define DEFAULTELEM '\0'
|
||||
// 定义最大值
|
||||
//#define INFINITY 32767
|
||||
// 定义默认数据类型
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "test.cpp"
|
||||
#include "test.cpp"
|
||||
|
||||
|
||||
int main()
|
||||
|
||||
6
Code/Code/CMakeLists.txt
Normal file
6
Code/Code/CMakeLists.txt
Normal file
@@ -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")
|
||||
15
Code/Code/CMakeSettings.json
Normal file
15
Code/Code/CMakeSettings.json
Normal file
@@ -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": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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
|
||||
@@ -1,168 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{82e38fa5-0edc-487c-9e6f-8bfce2d77216}</ProjectGuid>
|
||||
<RootNamespace>Code</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<BasicRuntimeChecks>UninitializedLocalUsageCheck</BasicRuntimeChecks>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="head.h" />
|
||||
<ClCompile Include="main.c" />
|
||||
<ClCompile Include="test.c" />
|
||||
<ClCompile Include="sequence_tree.h" />
|
||||
<ClCompile Include="sequence_stack.h" />
|
||||
<ClCompile Include="thread_tree.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="double_link_list.h" />
|
||||
<ClInclude Include="graph.h" />
|
||||
<ClInclude Include="link_list.h" />
|
||||
<ClInclude Include="link_queue.h" />
|
||||
<ClInclude Include="link_stack.h" />
|
||||
<ClInclude Include="link_string.h" />
|
||||
<ClInclude Include="link_tree.h" />
|
||||
<ClInclude Include="search.h" />
|
||||
<ClInclude Include="sequence_list.h" />
|
||||
<ClInclude Include="sequence_string.h" />
|
||||
<ClInclude Include="sort.h" />
|
||||
<ClInclude Include="static_link_list.h" />
|
||||
<ClInclude Include="sequence_queue.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -1,78 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="head.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="sequence_stack.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="thread_tree.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="sequence_tree.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="test.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="link_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="sequence_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="double_link_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="link_stack.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="static_link_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="sequence_queue.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="link_queue.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="link_string.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="link_tree.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="sequence_string.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="graph.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="search.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="sort.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
</Project>
|
||||
@@ -1,186 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
@@ -1,8 +0,0 @@
|
||||
// 初始化最大长度
|
||||
#define MAXSIZE 5
|
||||
// 定义默认值
|
||||
#define DEFAULTELEM '0'
|
||||
// 定义最大值
|
||||
#define INFINITY 32767
|
||||
// 定义默认数据类型
|
||||
typedef char element_type;
|
||||
30
Code/Code/head/double_link_list.h
Normal file
30
Code/Code/head/double_link_list.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#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;
|
||||
}
|
||||
6
Code/Code/head/head.h
Normal file
6
Code/Code/head/head.h
Normal file
@@ -0,0 +1,6 @@
|
||||
// 初始化最大长度
|
||||
#define MAXSIZE 5
|
||||
// 定义默认值
|
||||
#define DEFAULTELEM '\0'
|
||||
// 定义默认数据类型
|
||||
typedef char element_type;
|
||||
325
Code/Code/head/link_list.h
Normal file
325
Code/Code/head/link_list.h
Normal file
@@ -0,0 +1,325 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#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-1,start到达,求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;
|
||||
}
|
||||
|
||||
178
Code/Code/head/sequence_list.h
Normal file
178
Code/Code/head/sequence_list.h
Normal file
@@ -0,0 +1,178 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#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<class List>
|
||||
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<class List>
|
||||
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开始,所以可以插入的范围是0到list->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<class List>
|
||||
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<class List>
|
||||
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<class List>
|
||||
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<class List>
|
||||
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<class List>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
65
Code/Code/head/sequence_stack.h
Normal file
65
Code/Code/head/sequence_stack.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#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];
|
||||
}
|
||||
11
Code/Code/head/static_link_list.h
Normal file
11
Code/Code/head/static_link_list.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 静态链表
|
||||
typedef struct {
|
||||
// 数据元素
|
||||
element_type data;
|
||||
// 下标
|
||||
int next;
|
||||
} StaticLinkList[MAXSIZE];
|
||||
@@ -1,406 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 链栈结点
|
||||
typedef struct LinkStackNode {
|
||||
element_type data;
|
||||
struct LinkStackNode* next;
|
||||
} LinkStackNode, * LinkStack;
|
||||
@@ -1,12 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 链串
|
||||
typedef struct LinkStringNode {
|
||||
// 每个结点存储一位数据
|
||||
char data;
|
||||
// 每个结点存储四位数据
|
||||
//char data[4];
|
||||
struct LinkStringNode* next;
|
||||
} LinkStringNode, *LinkString;
|
||||
@@ -1,190 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
//SequenceListTest();
|
||||
//LinkListTest();
|
||||
return 0;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 分块查找
|
||||
// 分块索引表
|
||||
typedef struct {
|
||||
// 块中最大元素
|
||||
element_type max;
|
||||
// 存储区间的最低索引和最高索引
|
||||
int low, high;
|
||||
} BlockSearchIndex;
|
||||
|
||||
@@ -1,322 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
259
Code/Code/sort.h
259
Code/Code/sort.h
@@ -1,259 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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];
|
||||
// 用low和high搜索基准的最终位置
|
||||
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];
|
||||
}
|
||||
// 当low和high指向同一个元素时就把基准放到这个位置
|
||||
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;
|
||||
}
|
||||
8
Code/Code/source/main.cpp
Normal file
8
Code/Code/source/main.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "test.cpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
//SequenceListTest();
|
||||
LinkListTest();
|
||||
return 0;
|
||||
}
|
||||
49
Code/Code/source/test.cpp
Normal file
49
Code/Code/source/test.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// 测试文件
|
||||
#include <iostream>
|
||||
#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;
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 静态链表结点
|
||||
typedef struct StaticLinkNode {
|
||||
element_type data;
|
||||
// 下一个元素的数组下标
|
||||
int next;
|
||||
} StaticLinkNode;
|
||||
// 静态链表数组
|
||||
typedef StaticLinkNode StaticLinkList[MAXSIZE];
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
45
Data-Structrue/2-stack-ex.md
Normal file
45
Data-Structrue/2-stack-ex.md
Normal file
@@ -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$。
|
||||
@@ -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$策略。
|
||||
|
||||
Reference in New Issue
Block a user