1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-02-11 22:55:57 +08:00

更新链表

This commit is contained in:
Didnelpsun
2021-09-14 23:41:28 +08:00
parent 189f9250c3
commit 028cf20dde
6 changed files with 682 additions and 430 deletions

View File

@@ -0,0 +1,101 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 单链表结点
class DoubleLinkListNode {
private:
// 数据
element_type _data{};
// 指针
DoubleLinkListNode *_prior, *_next{};
public:
// 设置数据
bool SetData(element_type elem);
// 获取数据
element_type GetData() const;
// 设置prior
bool SetPrior(DoubleLinkListNode *prior)
// 获取prior
DoubleLinkList *GetPrior();
// 设置next
bool SetNext(DoubleLinkListNode *next);
// 获取next
DoubleLinkListNode *GetNext();
// 构造函数
DoubleLinkListNode();
explicit DoubleLinkListNode(element_type elem);
DoubleLinkListNode(element_type elem, DoubleLinkListNode *next);
DoubleLinkListNode(element_type elem, DoubleLinkListNode *prior, DoubleLinkListNode *next)
// 销毁
bool Destory();
};
bool DoubleLinkListNode::SetData(element_type elem) {
this._data = elem;
return true;
}
element_type DoubleLinkListNode::GetData() const {
return this._data;
}
bool DoubleLinkListNode::SetPrior(DoubleLinkListNode *prior) {
this._prior = prior;
return true;
}
DoubleLinkListNode *DoubleLinkListNode::GetPrior() const {
return this._prior;
}
bool DoubleLinkListNode::SetNext(DoubleLinkListNode *next) {
this._next=next;
return true;
}
DoubleLinkListNode *DoubleLinkListNode::GetNext() const {
return this._next;
}
DoubleLinkListNode::DoubleLinkListNode() {
this->SetPrior(nullptr);
this->SetNext(nullptr);
this->SetData(NULL);
}
DoubleLinkListNode::DoubleLinkListNode(element_type elem) {
this->SetPrior(nullptr);
this->SetNext(nullptr);
this->SetData(elem);
}
DoubleLinkListNode::DoubleLinkListNode(element_type elem, DoubleLinkListNode *next) {
this->SetPrior(nullptr);
this->SetNext(next);
this->SetData(elem);
}
DoubleLinkListNode::DoubleLinkListNode(element_type elem, DoubleLinkListNode *prior, DoubleLinkListNode *next) {
this->SetPrior(prior);
this->SetNext(next);
this->SetData(elem);
}
bool DoubleLinkListNode::Destroy{
free(this.GetPrior());
free(this.GetNext());
this->SetPrior(nullptr);
this->SetNext(nullptr);
this->SetData(NULL);
};

View File

@@ -1,7 +1,7 @@
// 初始化最大长度
#define MAXSIZE 5
// 定义默认值
#define DEFAULTDATA '0'
#define DEFAULTELEM '0'
// 定义最大值
//#define INFINITY 32767
// 定义默认数据类型

File diff suppressed because it is too large Load Diff

View File

@@ -65,9 +65,9 @@ private:
int _max_size{};
public:
// 设置最大容量
bool SetMaxSize(int length);
bool SetMaxSize(int max_size);
// 获取最大容量
int GetMaxSize();
int GetMaxSize() const;
// 构造函数
DynamicSequenceList();
// 插入函数
@@ -120,12 +120,12 @@ StaticSequenceList::StaticSequenceList() : SequenceList() {
this->SetData((element_type*)malloc(MAXSIZE * sizeof(element_type)));
}
bool DynamicSequenceList::SetMaxSize(int length) {
this->_max_size = length;
bool DynamicSequenceList::SetMaxSize(int max_size) {
this->_max_size = max_size;
return true;
}
int DynamicSequenceList::GetMaxSize() {
int DynamicSequenceList::GetMaxSize() const {
return this->_max_size;
}
@@ -264,7 +264,7 @@ 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;
return false;
return nullptr;
}
auto* elem = (element_type*)malloc(length * sizeof(element_type));
if (elem) {
@@ -285,7 +285,7 @@ element_type* SequenceList::LoopDelete(int index, int length) {
element_type SequenceList::GetElem(int index) const {
if (index >= this->GetLength() || index < 0) {
cout << "GetElem:查找索引" << index << "超过索引范围!" << endl;
return DEFAULTDATA;
return DEFAULTELEM;
}
return this->GetData(index);
}

View File

@@ -0,0 +1,64 @@
#include <cstdio>
#include <cstdlib>
#include "head.h"
class StaticLinkListNode {
private:
// 数据元素
element_type _data;
// 下一个元素的数组下标
int _next;
public:
// 设置元素
bool SetData(element_type elem);
// 获取元素
element_type GetData();
// 设置下标
bool SetNext(int index);
// 获取下标
int GetNext();
// 构造函数
StaticLinkListNode();
StaticLinkListNode(element_type elem);
StaticLinkListNode(element_type elem, int next);
// 销毁
bool Destroy();
};
class StaticLinkList {
private:
// 首元素地址
int _first;
// 数据长度
int _length;
// 数据当前最大长度
int _max_size;
public:
// 设置首元素地址
bool SetFirst(int index);
// 获取首元素地址
int GetFirst();
// 数据长度自加
bool SetLength();
// 设置数据长度
bool SetLength(int length);
// 获取数据长度
int GetLength();
// 设置数据当前最大长度
bool SetMaxSize(int max_size);
// 获取数据当前最大长度
int GetMaxSize();
// 数据
StaticLinkListNode *data;
};

View File

@@ -32,11 +32,12 @@ bool LinkListTest() {
auto* list = new LinkListWithoutHead();
list->NextInsert(a, 0 ,5);
list->Print();
int len = 3;
int len = 5;
element_type* b = list->Delete(2, len);
for (int i = 0; i < len; i++) {
cout << b[i] << endl;
}
list->Print();
cout << list->Locate('1') << endl;
return true;
}