1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-11-28 23:16:40 +08:00

更新数据结构

This commit is contained in:
Didnelpsun
2021-08-20 23:54:10 +08:00
parent a13bacd22d
commit 3c864b55c1
5 changed files with 416 additions and 194 deletions

View File

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

View File

@@ -1,120 +1,282 @@
#include <cstdio>
#include <cstdio>
#include <cstdlib>
#include "head.h"
// 单链表结点
class LinkListNode {
private:
// 数据
element_type _data{};
// 指针
LinkListNode* _next{};
public:
// 数据
element_type data;
// 指针
LinkListNode* next;
// 构造函数
LinkListNode();
explicit LinkListNode(element_type data);
// 设置数据
bool SetData(element_type data);
// 获取数据
element_type GetData() const;
// 设置next
bool SetNext(LinkListNode* next);
// 获取next
LinkListNode* GetNext();
// 构造函数
LinkListNode();
explicit LinkListNode(element_type data);
LinkListNode(element_type data, LinkListNode* next);
};
class LinkList{
class LinkList {
private:
// 指针
LinkListNode* _next{};
// 链表长度
int _length{};
// 类型,真有头节点,假无头节点
bool _type{};
protected:
// 设置链表类型
bool SetType(bool type);
public:
// 指针
LinkListNode* next;
// 链表长度
int length;
// 构造函数
LinkList();
// 判空
bool Empty();
// 设置next
bool SetNext(LinkListNode* next);
// 获取next
LinkListNode* GetNext();
// 长度自加1
bool SetLength();
// 设置长度
bool SetLength(int length);
// 获取长度
int GetLength() const;
// 获取链表类型
bool GetType() const;
// 构造函数
LinkList();
// 打印
virtual bool Print() = 0;
// 判空
bool Empty() const;
// 插入
virtual bool Insert(int index, element_type data) = 0;
// 循环插入
bool LoopInsert(element_type* elem, int start, int length);
};
class LinkListWithHead: public LinkList{
class LinkListWithHead : public LinkList {
public:
// 构造函数
LinkListWithHead();
// 构造函数
LinkListWithHead();
// 打印
bool Print() override;
// 插入
bool Insert(int index, element_type data) override;
};
class LinkListWithoutHead: public LinkList{
class LinkListWithoutHead : public LinkList {
private:
// 数据
element_type _data{};
public:
// 数据
element_type data;
// 构造函数
LinkListWithoutHead();
// 设置数据
bool SetData(element_type data);
// 获取数据
element_type GetData() const;
// 构造函数
LinkListWithoutHead();
// 打印
bool Print() override;
// 插入
bool Insert(int index, element_type data) override;
};
bool LinkListNode::SetData(element_type data) {
this->_data = data;
return true;
}
element_type LinkListNode::GetData() const {
return this->_data;
}
bool LinkListNode::SetNext(LinkListNode* next) {
this->_next = next;
return true;
}
LinkListNode* LinkListNode::GetNext() {
return this->_next;
}
LinkListNode::LinkListNode() {
this->data = NULL;
this->next = nullptr;
this->SetData(NULL);
this->SetNext(nullptr);
}
LinkListNode::LinkListNode(element_type data) {
this->data = data;
this->next = nullptr;
this->SetData(data);
this->SetNext(nullptr);
}
LinkListNode::LinkListNode(element_type data, LinkListNode* next) {
this->SetData(data);
this->SetNext(next);
}
bool LinkList::SetNext(LinkListNode* next) {
this->_next = next;
return true;
}
LinkListNode* LinkList::GetNext() {
return this->_next;
}
bool LinkList::SetLength() {
this->_length++;
return true;
}
bool LinkList::SetLength(int length) {
this->_length = length;
return true;
}
int LinkList::GetLength() const {
return this->_length;
}
bool LinkList::SetType(bool type) {
this->_type = type;
return true;
}
bool LinkList::GetType() const {
return this->_type;
}
LinkList::LinkList() {
this->next= nullptr;
this->length =0;
this->SetNext(nullptr);
this->SetLength(0);
}
LinkListWithHead::LinkListWithHead() = default;
LinkListWithHead::LinkListWithHead() {
this->SetType(true);
};
bool LinkListWithoutHead::SetData(element_type data) {
this->_data = data;
return true;
}
element_type LinkListWithoutHead::GetData() const {
return this->_data;
}
LinkListWithoutHead::LinkListWithoutHead() {
this->data = NULL;
this->SetType(false);
this->SetData(NULL);
}
bool LinkList::Empty() {
if(this->length==0){
return true;
} else {
return false;
}
bool LinkList::Empty() const {
if (this->GetLength() == 0) {
return true;
}
else {
return false;
}
}
bool LinkListWithHead::Print() {
int i = 1;
if (this->GetLength() == 0) {
return true;
}
cout << "第0个元素值为空" << endl;
// 当前遍历指针
LinkListNode* p = this->GetNext();
if (p != nullptr) {
cout << "第" << i << "个元素值为" << p->GetData() << endl;
i++;
p = p->GetNext();
}
return true;
}
bool LinkListWithoutHead::Print() {
int i = 1;
if (this->GetLength() == 0) {
return true;
}
cout << "第" << i << "个元素值为" << this->GetData() << endl;
// 当前遍历指针
LinkListNode* p = this->GetNext();
if (p != nullptr) {
cout << "第" << i << "个元素值为" << p->GetData() << endl;
i++;
p = p->GetNext();
}
return true;
}
bool LinkListWithHead::Insert(int index, element_type data) {
if (index < 1) {
cout << "Insert:插入索引值过小!" << endl;
return false;
}
// 定义一个结点指针p指向当前扫描到的结点
LinkListNode* p;
// 定义一个变量i表示当前扫描到的结点的索引号
int i = 1;
// 将链表头结点的next指向p,为第1个结点
p = this->GetNext();
LinkListNode* s = new LinkListNode(data);
// 如果该链表为空链表
if (p == nullptr) {
this->SetLength();
this->SetNext(s);
return true;
}
// 循环遍历到达指定索引号的单链表的结点
// 条件是当前结点的下一个不为空且索引号到达,所到达的结点一定不是空结点
while (p->GetNext() != nullptr && i < index - 1) {
p = p->GetNext();
i++;
}
// 如果此时i小于index-1,表示遍历完还没有到达对应的索引
if (i < index - 1) {
cout << "Insert:插入索引值过大!" << endl;
return false;
}
cout << i << index << endl;
// 此时i==index-1
// 将p原来的后继给新的结点
s->SetNext(p->GetNext());
p->SetNext(s);
cout << p->GetNext() << endl;
this->SetLength();
return true;
}
bool LinkListWithoutHead::Insert(int index, element_type data) {
return true;
}
bool LinkList::LoopInsert(element_type* elem, int start, int length) {
for (int i = 1; i < length; i++) {
bool result = this->Insert(i, elem[i + start]);
if (!result) {
cout << "LoopInsert:循环插入失败!" << endl;
return false;
}
}
return true;
}
//// 插入有头节点单链表元素
//// 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) {
// s->data = elem;
// // 将p原来的后继给新的结点
// s->next = p->next;
// p->next = s;
// return 0;
// }
// else {
// printf("InsertLinkListWithHead:分配内存失败!\n");
// return 1;
// }
//}
//
//// 插入无头节点单链表元素
//// C语言也无法调用这个函数
//int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
// if (index < 0) {
// printf("InsertLinkListWithoutHead:插入索引值过小!\n");
// return 1;
// cout << "InsertLinkListWithoutHead:插入索引值过小!" << endl;
// return false;
// }
// if (index == 0) {
// LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
@@ -124,11 +286,11 @@ bool LinkList::Empty() {
// s->next = list;
// // 将list指针设置为s指针
// list = s;
// return 0;
// return true;
// }
// else {
// printf("InsertLinkListWithoutHead:分配内存失败!\n");
// return 1;
// cout << "InsertLinkListWithoutHead:分配内存失败!" << endl;
// return false;
// }
// }
// // 定义一个结点指针p指向当前扫描到的结点
@@ -145,8 +307,8 @@ bool LinkList::Empty() {
// }
// // 如果此时i小于index-1,表示遍历完还没有到达对应的索引
// if (i < index - 1) {
// printf("InsertLinkListWithoutHead:插入索引值过大!\n");
// return 1;
// cout << "InsertLinkListWithoutHead:插入索引值过大!" << endl;
// return false;
// }
// // 此时i==index-1
// LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
@@ -155,10 +317,10 @@ bool LinkList::Empty() {
// // 将p原来的后继给新的结点
// s->next = p->next;
// p->next = s;
// return 0;
// return true;
// }
// else {
// printf("InsertLinkListWithoutHead:分配空间失败!\n");
// return 1;
// cout << "InsertLinkListWithoutHead:分配空间失败!" << endl;
// return false;
// }
//}

View File

@@ -9,29 +9,44 @@ using namespace std;
// 顺序表
class SequenceList {
private:
// 数据
element_type *_data{};
// 长度
int _length{};
public:
element_type *data{};
int length{};
// 设置数据
bool SetData(element_type* elem);
bool SetData(int index, element_type elem);
// 获取数据
element_type* GetData() const;
element_type GetData(int index) const;
// 长度自加1
bool SetLength();
// 设置长度
bool SetLength(int length);
// 设置长度
int GetLength() const;
// 构造函数
SequenceList();
// 插入函数
virtual bool Insert(int index, element_type elem);
virtual bool Insert(int index, element_type elem) = 0;
// 打印函数
void Printf();
bool Print() const;
// 循环插入函数
bool LoopInsert(element_type *elem, int start, int end);
bool LoopInsert(element_type *elem, int start, int length);
// 删除函数
bool Delete(int index, element_type &elem);
// 多个删除函数
bool MultiDelete(int index, int len, element_type *elem);
bool LoopDelete(int index, int len, element_type *elem);
// 按位获取元素
element_type GetElem(int index);
element_type GetElem(int index) const;
// 按值获取元素
int Locate(element_type elem);
int Locate(element_type elem) const;
// 判空
bool Empty();
bool Empty() const;
// 销毁
bool Destroy();
bool Destroy() const;
};
// 静态顺序表
@@ -45,9 +60,14 @@ public:
// 动态顺序表
class DynamicSequenceList: public SequenceList{
public:
private:
// 已分配的最大容量
int max_size;
int _max_size{};
public:
// 设置最大容量
bool SetMaxSize(int length);
// 获取最大容量
int GetMaxSize();
// 构造函数
DynamicSequenceList();
// 插入函数
@@ -60,32 +80,74 @@ private:
bool ReIncrease(int len);
};
bool SequenceList::SetData(element_type* elem) {
this->_data = elem;
return true;
}
bool SequenceList::SetData(int index, element_type elem) {
this->_data[index] = elem;
return true;
}
element_type *SequenceList::GetData() const {
return this->_data;
}
element_type SequenceList::GetData(int index) const {
return this->_data[index];
}
bool SequenceList::SetLength() {
this->_length ++;
return true;
}
bool SequenceList::SetLength(int length) {
this->_length = length;
return true;
}
int SequenceList::GetLength() const {
return this->_length;
}
SequenceList::SequenceList() {
this->length = 0;
this->SetLength(0);
}
StaticSequenceList::StaticSequenceList() : SequenceList() {
this->data = (element_type*)malloc(MAXSIZE * sizeof(element_type));
this->SetData((element_type*)malloc(MAXSIZE * sizeof(element_type)));
}
bool DynamicSequenceList::SetMaxSize(int length) {
this->_max_size = length;
return true;
}
int DynamicSequenceList::GetMaxSize() {
return this->_max_size;
}
DynamicSequenceList::DynamicSequenceList() : SequenceList() {
// 初初始化动态顺序表长度为0
this->max_size=0;
this->SetMaxSize(0);
// 申请一片连续的存储空间
auto* space = (element_type*)malloc(MAXSIZE * sizeof(element_type));
if (space) {
this->data = space;
this->max_size = MAXSIZE;
this->SetData(space);
this->SetMaxSize(MAXSIZE);
}
else {
cout << "InitSequenceList:分配空间失败!" << endl;
}
}
void SequenceList::Printf() {
for (int i = 0; i < this->length; i++) {
cout << "第" << i + 1 << "个元素值为" << this->data[i] << endl;
bool SequenceList::Print() const {
for (int i = 0; i < this->GetLength(); i++) {
cout << "第" << i + 1 << "个元素值为" << this->GetData(i) << endl;
}
return true;
}
bool DynamicSequenceList::OtherIncrease(int len) {
@@ -94,16 +156,16 @@ bool DynamicSequenceList::OtherIncrease(int len) {
return false;
}
// 申请一片连续的存储空间
int new_length = this->max_size + len;
int new_length = this->GetMaxSize() + len;
auto* space = (element_type*)malloc(new_length * sizeof(element_type));
if (space) {
// 建立中间变量
this->data = space;
element_type* temp = this->data;
for (int i = 0; i < this->length; i++) {
this->data[i] = temp[i];
this->SetData(space);
element_type* temp = this->GetData();
for (int i = 0; i < this->GetLength(); i++) {
this->SetData(i, temp[i]);
}
this->max_size = new_length;
this->SetMaxSize(new_length);
free(temp);
return true;
}
@@ -113,75 +175,71 @@ bool DynamicSequenceList::OtherIncrease(int len) {
}
}
bool DynamicSequenceList::ReIncrease(int len) {
if (len <= 0) {
bool DynamicSequenceList::ReIncrease(int length) {
if (length <= 0) {
cout << "ReIncrease:申请空间应该大于0!" << endl;
return false;
}
// 申请一片连续的存储空间
int new_length = this->max_size + len;
auto* space = (element_type*)realloc(this->data, new_length * sizeof(element_type));
int new_length = this->GetMaxSize() + length;
auto* space = (element_type*)realloc(this->GetData(), new_length * sizeof(element_type));
if (space) {
this->data = space;
this->max_size += len;
this->SetData(space);
this->SetMaxSize(this->GetMaxSize()+length);
return true;
}
else {
this->max_size = 0;
this->length = 0;
this->SetMaxSize(0);
this->SetLength(0);
cout << "ReIncrease:分配其他地址空间失败!" << endl;
return false;
}
}
bool SequenceList::Insert(int index, element_type elem) {
return false;
}
bool StaticSequenceList::Insert(int index, element_type elem) {
// 当静态顺序表已经满了就不能插入任何元素
if (this->length >= MAXSIZE) {
if (this->GetLength() >= MAXSIZE) {
cout << "Insert:静态顺序表空间不足,插入失败!" << endl;
return false;
}
// 索引位置从0开始,所以可以插入的范围是0到list->length
if (index > this->length || index < 0) {
if (index > this->GetLength() || index < 0) {
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
return false;
}
// 从最后一个元素开始交换后移,list->length是空的
for (int i = this->length; i > index; i--) {
this->data[i] = this->data[i - 1];
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i-1));
}
this->data[index] = elem;
this->length++;
this->SetData(index, elem);
this->SetLength();
return true;
}
bool DynamicSequenceList::Insert(int index, element_type elem) {
if (index > this->length || index < 0) {
if (index > this->GetLength() || index < 0) {
cout << "Insert:插入索引" << index << "超过索引范围!" << endl;
return false;
}
// 当动态顺序表已经满了,需要新增一个位置
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
if (this->length >= MAXSIZE) {
if (this->GetLength() >= MAXSIZE) {
bool result = this->ReIncrease(1);
if (!result) {
cout << "Insert:申请空间失败!" << endl;
return false;
}
}
for (int i = this->length; i > index; i--) {
this->data[i] = this->data[i - 1];
for (int i = this->GetLength(); i > index; i--) {
this->SetData(i, this->GetData(i-1));
}
this->data[index] = elem;
this->length++;
this->SetData(index, elem);
this->SetLength();
return true;
}
bool SequenceList::LoopInsert(element_type *elem, int start, int end) {
for (int i = 0; i < end; i++) {
bool SequenceList::LoopInsert(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 << "LoopInsert:循环插入失败!" << endl;
@@ -192,45 +250,45 @@ bool SequenceList::LoopInsert(element_type *elem, int start, int end) {
}
bool SequenceList::Delete(int index, element_type &elem) {
if (index >= this->length || index < 0) {
if (index >= this->GetLength() || index < 0) {
cout << "Delete:删除索引" << index << "超过索引范围!" << endl;
return false;
}
elem = this->data[index];
for (int i = index; i < this->length; i++) {
this->data[i] = this->data[i + 1];
elem = this->GetData(index);
for (int i = index; i < this->GetLength(); i++) {
this->SetData(i, this->GetData(i+1));
}
this->length--;
this->SetLength(this->GetLength()-1);
return true;
}
bool SequenceList::MultiDelete(int index, int len, element_type *elem) {
if (index + len >= this->length || index < 0) {
cout << "MultiDelete:删除索引" << index << "超过索引范围!" << endl;
bool SequenceList::LoopDelete(int index, int length, element_type *elem) {
if (index + length > this->GetLength() || index < 0) {
cout << "LoopDelete:删除索引" << index + length << "超过索引范围!" << endl;
return false;
}
for (int i = index; i < this->length - len; i++) {
if (i < index + len) {
elem[i - index] = this->data[i];
for (int i = index; i <= this->GetLength() - length; i++) {
if (i < index + length) {
elem[i - index] = this->GetData(i);
}
this->data[i] = this->data[i + len];
this->SetData(i, this->GetData(i + length));
}
this->length -= len;
this->SetLength(this->GetLength()-length);
return true;
}
element_type SequenceList::GetElem(int index) {
if (index >= this->length || index < 0) {
element_type SequenceList::GetElem(int index) const {
if (index >= this->GetLength() || index < 0) {
cout << "GetElem:查找索引" << index << "超过索引范围!" << endl;
return DEFAULTDATA;
}
return this->data[index];
return this->GetData(index);
}
int SequenceList::Locate(element_type elem) {
for (int i = 0; i < this->length; i++) {
if (this->data[i] == elem) {
int SequenceList::Locate(element_type elem) const {
for (int i = 0; i < this->GetLength(); i++) {
if (this->GetData(i) == elem) {
return i;
}
}
@@ -238,8 +296,8 @@ int SequenceList::Locate(element_type elem) {
return -1;
}
bool SequenceList::Empty() {
if (this->length == 0) {
bool SequenceList::Empty() const {
if (this->GetLength() == 0) {
return false;
}
else {
@@ -247,9 +305,9 @@ bool SequenceList::Empty() {
}
}
bool SequenceList::Destroy() {
if (this->data) {
free(this->data);
bool SequenceList::Destroy() const {
if (this->GetData()) {
free(this->GetData());
}
return true;
}

View File

@@ -1,40 +1,32 @@
// ²âÊÔÎļþ
// 测试文件
#include "../head/sequence_list.h"
#include "../head/link_list.h"
int SequenceListTest() {
DynamicSequenceList list;
list.Insert(0, 'a');
element_type a[6] = {'1','2','3','4','5','6'};
list.LoopInsert(a, 0, 6);
//printf("%c", list.data[2]);
element_type b[3] = { '9', 'a', 'e' };
list.LoopInsert(b, 1, 2);
list.Printf();
/*list.Printf();
printf("\n");
int len = 2;
element_type elem[2];
list.MultiDelete(0, len, elem);*/
/*list.Printf();
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 = list.Locate('5');
index = list.GetElem(1);
cout << list.Empty() << endl;
list.Print();
element_type b[4];
list.LoopDelete(1, 3, b);
list.Print();
for (int i = 0; i < 3; i++) {
cout << b[i] << endl;
}
list.Destroy();
return 0;
}
int LinkListTest() {
LinkList list;
cout << list.Empty() << endl;
LinkListWithHead list;
//cout << list.Empty() << endl;
//element_type a[6] = { '1','2','3','4','5','6' };
//list.LoopInsert(a, 1, 6);
list.Insert(1, '3');
list.Print();
list.Insert(2, '4');
list.Print();
return 0;
}