1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-06-16 23:17:21 +08:00

更新内存管理

This commit is contained in:
Didnelpsun
2021-08-24 23:55:13 +08:00
parent c3d4acdf8f
commit 61a0b89de7
4 changed files with 250 additions and 97 deletions

View File

@@ -388,10 +388,6 @@ bool LinkList::Delete(int index) {
return true;
}
bool LinkList::Delete(int index, int length) {
return false;
}
bool LinkListWithHead::Delete(int index, int length) {
if (index < 1) {
cout << "Delete:删除索引值" << index << "过小!" << endl;
@@ -475,9 +471,9 @@ bool LinkListWithoutHead::Delete(int index, int length) {
cout << "Delete:删除索引值" << index << "过大!" << endl;
return false;
}
// 此时i==index-1start到达求end
end = start;
for (int i = 0; i < length; i++) {
// 从1开始遍历
end = this->GetNext();
for (int i = 1; i < index + length - 1; i++) {
end = end->GetNext();
if (end == nullptr) {
cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length << endl;
@@ -485,7 +481,11 @@ bool LinkListWithoutHead::Delete(int index, int length) {
}
}
if (index == 0) {
this->SetNext(end);
this->SetData(end->GetNext()->GetData());
this->SetNext(end->GetNext()->GetNext());
}
if (index == 1) {
this->SetNext(end->GetNext());
}
else {
start->SetNext(end->GetNext());

View File

@@ -19,17 +19,21 @@ int SequenceListTest() {
}
int LinkListTest() {
LinkListWithHead list;
//cout << list.Empty() << endl;
element_type a[6] = { '1','2','3','4','5','6' };
list.NextInsert(a, 0, 5);
list.Print();
list.Delete(2, 3);
list.Print();
element_type a[6] = {'0', '1','2','3','4','5' };
// LinkListWithHead list;
// list.NextInsert(a, 0, 5);
// list.Print();
// list.Delete(2, 4);
// list.Print();
/*cout << list.GetLength() << endl;
LinkListWithoutHead list2;
list2.PriorInsert(a, 2, 3);
list2.Print();
cout << list2.GetLength() << endl;*/
auto* list = new LinkListWithoutHead();
list->NextInsert(a, 0 ,5);
list->Print();
list->Delete(2,4);
list->Print();
return 0;
}