mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-02-09 05:44:32 +08:00
更新进程管理
This commit is contained in:
@@ -22,6 +22,8 @@ public:
|
||||
LinkListNode();
|
||||
explicit LinkListNode(element_type data);
|
||||
LinkListNode(element_type data, LinkListNode* next);
|
||||
// 销毁
|
||||
bool Destory();
|
||||
};
|
||||
|
||||
class LinkList {
|
||||
@@ -60,6 +62,9 @@ public:
|
||||
bool PriorInsert(element_type* elem, int start, int length);
|
||||
// 后插入
|
||||
bool NextInsert(element_type* elem, int start, int length);
|
||||
// 删除
|
||||
virtual bool Delete(int index);
|
||||
virtual bool Delete(int index, int length) = 0;
|
||||
};
|
||||
|
||||
class LinkListWithHead : public LinkList {
|
||||
@@ -70,6 +75,8 @@ public:
|
||||
bool Print() override;
|
||||
// 插入
|
||||
bool Insert(int index, element_type data) override;
|
||||
// 删除
|
||||
bool Delete(int index, int length) override;
|
||||
};
|
||||
|
||||
class LinkListWithoutHead : public LinkList {
|
||||
@@ -87,6 +94,8 @@ public:
|
||||
bool Print() override;
|
||||
// 插入
|
||||
bool Insert(int index, element_type data) override;
|
||||
// 删除
|
||||
bool Delete(int index, int length) override;
|
||||
};
|
||||
|
||||
bool LinkListNode::SetData(element_type data) {
|
||||
@@ -122,6 +131,19 @@ LinkListNode::LinkListNode(element_type data, LinkListNode* next) {
|
||||
this->SetNext(next);
|
||||
}
|
||||
|
||||
bool LinkListNode::Destory() {
|
||||
// 循环删除next指向链表后续结点
|
||||
while(this->GetNext()!= nullptr){
|
||||
LinkListNode* node = this->GetNext();
|
||||
this->SetNext(node->GetNext());
|
||||
node->SetData(NULL);
|
||||
node->SetNext(nullptr);
|
||||
}
|
||||
this->SetData(NULL);
|
||||
this->SetNext(nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkList::SetNext(LinkListNode* next) {
|
||||
this->_next = next;
|
||||
return true;
|
||||
@@ -188,16 +210,16 @@ bool LinkList::Empty() const {
|
||||
|
||||
bool LinkListWithHead::Print() {
|
||||
int i = 1;
|
||||
cout << "第0个元素值为空" << endl;
|
||||
if (this->GetLength() == 0) {
|
||||
return true;
|
||||
}
|
||||
cout << "第0个元素值为空" << endl;
|
||||
// 当前遍历指针
|
||||
LinkListNode* p = this->GetNext();
|
||||
while (p != nullptr) {
|
||||
cout << "第" << i << "个元素值为" << p->GetData() << endl;
|
||||
LinkListNode* node = this->GetNext();
|
||||
while (node != nullptr) {
|
||||
cout << "第" << i << "个元素值为" << node->GetData() << endl;
|
||||
i++;
|
||||
p = p->GetNext();
|
||||
node = node->GetNext();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -209,91 +231,108 @@ bool LinkListWithoutHead::Print() {
|
||||
}
|
||||
cout << "第" << i << "个元素值为" << this->GetData() << endl;
|
||||
// 当前遍历指针
|
||||
LinkListNode* p = this->GetNext();
|
||||
while (p != nullptr) {
|
||||
LinkListNode* node = this->GetNext();
|
||||
while (node != nullptr) {
|
||||
i++;
|
||||
cout << "第" << i << "个元素值为" << p->GetData() << endl;
|
||||
p = p->GetNext();
|
||||
cout << "第" << i << "个元素值为" << node->GetData() << endl;
|
||||
node = node->GetNext();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkListWithHead::Insert(int index, element_type data) {
|
||||
if (index < 1) {
|
||||
cout << "Insert:插入索引值过小!" << endl;
|
||||
cout << "Insert:插入索引值" << index << "过小!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkListNode* p;
|
||||
LinkListNode* node;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 1;
|
||||
// 将链表头结点的next指向p,为第1个结点
|
||||
p = this->GetNext();
|
||||
LinkListNode* s = new LinkListNode(data);
|
||||
// 将链表头结点的next指向node,为第1个结点
|
||||
node = this->GetNext();
|
||||
// 设置一个新结点进行插入
|
||||
auto* new_node = new LinkListNode(data);
|
||||
// 如果该链表为空链表
|
||||
if (p == nullptr) {
|
||||
if (node == nullptr) {
|
||||
this->SetNext(new_node);
|
||||
this->SetLength();
|
||||
return true;
|
||||
}
|
||||
// 当插入的是第一个节点
|
||||
if (index == 1) {
|
||||
new_node->SetNext(node);
|
||||
this->SetNext(new_node);
|
||||
this->SetLength();
|
||||
this->SetNext(s);
|
||||
return true;
|
||||
}
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
while (p->GetNext() != nullptr && i < index - 1) {
|
||||
p = p->GetNext();
|
||||
while (node->GetNext() != nullptr && i < index - 1) {
|
||||
node = node->GetNext();
|
||||
i++;
|
||||
}
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
cout << "Insert:插入索引值过大!" << endl;
|
||||
cout << "Insert:插入索引值" << index << "过大!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 此时i==index-1
|
||||
// 将p原来的后继给新的结点
|
||||
s->SetNext(p->GetNext());
|
||||
p->SetNext(s);
|
||||
// 将node原来的后继给新的结点
|
||||
new_node->SetNext(node->GetNext());
|
||||
node->SetNext(new_node);
|
||||
this->SetLength();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkListWithoutHead::Insert(int index, element_type data) {
|
||||
if (index < 0) {
|
||||
cout << "Insert:插入索引值过小!" << endl;
|
||||
cout << "Insert:插入索引值" << index << "过小!" << endl;
|
||||
return false;
|
||||
}
|
||||
if (index == 0) {
|
||||
LinkListNode* node = new LinkListNode(this->GetData());
|
||||
this->SetData(data);
|
||||
this->SetLength();
|
||||
if (this->GetLength() == 0) {
|
||||
this->SetData(data);
|
||||
this->SetLength();
|
||||
}
|
||||
else {
|
||||
auto* node = new LinkListNode(this->GetData());
|
||||
node->SetNext(this->GetNext());
|
||||
this->SetData(data);
|
||||
this->SetNext(node);
|
||||
this->SetLength();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkListNode* p;
|
||||
// 定义一个结点指针node指向当前扫描到的结点
|
||||
LinkListNode* node;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 1;
|
||||
// 将链表头结点的next指向p,为第1个结点
|
||||
p = this->GetNext();
|
||||
LinkListNode* s = new LinkListNode(data);
|
||||
node = this->GetNext();
|
||||
// 设置一个新结点进行插入
|
||||
auto* new_node = new LinkListNode(data);
|
||||
// 如果该链表为空链表
|
||||
if (p == nullptr) {
|
||||
if (node == nullptr) {
|
||||
this->SetLength();
|
||||
this->SetNext(s);
|
||||
this->SetNext(new_node);
|
||||
return true;
|
||||
}
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
while (p->GetNext() != nullptr && i < index - 1) {
|
||||
p = p->GetNext();
|
||||
while (node->GetNext() != nullptr && i < index - 1) {
|
||||
node = node->GetNext();
|
||||
i++;
|
||||
}
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
cout << "Insert:插入索引值过大!" << endl;
|
||||
cout << "Insert:插入索引值" << index << "过大!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 此时i==index-1
|
||||
// 将p原来的后继给新的结点
|
||||
s->SetNext(p->GetNext());
|
||||
p->SetNext(s);
|
||||
new_node->SetNext(node->GetNext());
|
||||
node->SetNext(new_node);
|
||||
this->SetLength();
|
||||
return true;
|
||||
}
|
||||
@@ -303,7 +342,7 @@ 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:循环插入失败!" << endl;
|
||||
cout << "PriorInsert:循环插入失败!索引值为" << i + start << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -313,7 +352,7 @@ 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:循环插入失败!" << endl;
|
||||
cout << "PriorInsert:循环插入失败!索引值为" << i + start << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -326,7 +365,7 @@ 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:循环插入失败!" << endl;
|
||||
cout << "NextInsert:循环插入失败!索引值为" << i + start << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -336,10 +375,121 @@ 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:循环插入失败!" << endl;
|
||||
cout << "NextInsert:循环插入失败!索引值为" << i + start << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool LinkList::Delete(int index) {
|
||||
this->Delete(index, 1);
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
if (length < 1) {
|
||||
cout << "Delete:删除长度" << length << "过小!" << endl;
|
||||
}
|
||||
// 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个
|
||||
LinkListNode* start;
|
||||
// 定义一个结点指针start指向当前扫描到的结点,要删除最后的元素
|
||||
LinkListNode* end;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 1;
|
||||
// 将链表头结点的next指向start,为第1个结点
|
||||
start = this->GetNext();
|
||||
// 如果链表没有任何数据
|
||||
if(start == nullptr) {
|
||||
cout << "Delete:链表为空!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
while (start->GetNext() != nullptr && i < index - 1) {
|
||||
start = start->GetNext();
|
||||
i++;
|
||||
}
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
cout << "Delete:删除索引值" << index << "过大!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 此时i==index-1,start到达,求end
|
||||
end = start;
|
||||
for (int i = 0; i < length; i++) {
|
||||
end = end->GetNext();
|
||||
if (end == nullptr) {
|
||||
cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (index == 1) {
|
||||
this->SetNext(end);
|
||||
}
|
||||
else {
|
||||
start->SetNext(end->GetNext());
|
||||
}
|
||||
this->SetLength(this->GetLength() - length);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LinkListWithoutHead::Delete(int index, int length) {
|
||||
if (index < 0) {
|
||||
cout << "Delete:删除索引值" << index << "过小!" << endl;
|
||||
return false;
|
||||
}
|
||||
if (length < 1) {
|
||||
cout << "Delete:删除长度" << length << "过小!" << endl;
|
||||
}
|
||||
// 定义一个结点指针start指向当前扫描到的结点,即要删除第一的元素的前一个
|
||||
LinkListNode* start;
|
||||
// 定义一个结点指针start指向当前扫描到的结点,要删除最后的元素
|
||||
LinkListNode* end;
|
||||
// 定义一个变量i表示当前扫描到的结点的索引号
|
||||
int i = 1;
|
||||
// 将链表头结点的next指向start,为第1个结点
|
||||
start = this->GetNext();
|
||||
// 如果链表没有任何数据
|
||||
if (this->GetData() == NULL) {
|
||||
cout << "Delete:链表为空!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 循环遍历到达指定索引号的单链表的结点
|
||||
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
|
||||
while (start->GetNext() != nullptr && i < index - 1) {
|
||||
start = start->GetNext();
|
||||
i++;
|
||||
}
|
||||
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
|
||||
if (i < index - 1) {
|
||||
cout << "Delete:删除索引值" << index << "过大!" << endl;
|
||||
return false;
|
||||
}
|
||||
// 此时i==index-1,start到达,求end
|
||||
end = start;
|
||||
for (int i = 0; i < length; i++) {
|
||||
end = end->GetNext();
|
||||
if (end == nullptr) {
|
||||
cout << "Delete:删除索引最大值" << index + length - 1 << "大于链表最大索引" << length << endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (index == 0) {
|
||||
this->SetNext(end);
|
||||
}
|
||||
else {
|
||||
start->SetNext(end->GetNext());
|
||||
}
|
||||
this->SetLength(this->GetLength() - length);
|
||||
return true;
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
// 打印函数
|
||||
bool Print() const;
|
||||
// 循环插入函数
|
||||
bool LoopInsert(element_type *elem, int start, int length);
|
||||
bool LoopInsert(element_type *elem, int index, int length);
|
||||
// 删除函数
|
||||
bool Delete(int index, element_type &elem);
|
||||
// 多个删除函数
|
||||
@@ -238,9 +238,9 @@ bool DynamicSequenceList::Insert(int index, element_type elem) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceList::LoopInsert(element_type *elem, int start, int length) {
|
||||
bool SequenceList::LoopInsert(element_type *elem, int index, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
bool result = this->Insert(i, elem[i + start]);
|
||||
bool result = this->Insert(i, elem[i + index]);
|
||||
if (!result) {
|
||||
cout << "LoopInsert:循环插入失败!" << endl;
|
||||
return false;
|
||||
|
||||
@@ -22,12 +22,14 @@ int LinkListTest() {
|
||||
LinkListWithHead list;
|
||||
//cout << list.Empty() << endl;
|
||||
element_type a[6] = { '1','2','3','4','5','6' };
|
||||
list.PriorInsert(a, 2, 3);
|
||||
list.NextInsert(a, 0, 5);
|
||||
list.Print();
|
||||
cout << list.GetLength() << endl;
|
||||
list.Delete(2, 3);
|
||||
list.Print();
|
||||
/*cout << list.GetLength() << endl;
|
||||
LinkListWithoutHead list2;
|
||||
list2.PriorInsert(a, 2, 3);
|
||||
list2.Print();
|
||||
cout << list2.GetLength() << endl;
|
||||
cout << list2.GetLength() << endl;*/
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user