mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-02-09 13:45:48 +08:00
更新栈
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user