mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-02-11 14:45:57 +08:00
更新代码
This commit is contained in:
11
Code/CPP-Code/CMakeLists.txt
Normal file
11
Code/CPP-Code/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
# CMakeList.txt: CPP-Code 的 CMake 项目,在此处包括源代码并定义
|
||||
# 项目特定的逻辑。
|
||||
#
|
||||
cmake_minimum_required (VERSION 3.8)
|
||||
|
||||
project ("CPP-Code")
|
||||
|
||||
# 将源代码添加到此项目的可执行文件。
|
||||
add_executable (CPP-Code "source/main.cpp")
|
||||
|
||||
# TODO: 如有需要,请添加测试并安装目标。
|
||||
15
Code/CPP-Code/CMakeSettings.json
Normal file
15
Code/CPP-Code/CMakeSettings.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "x64-Debug",
|
||||
"generator": "Ninja",
|
||||
"configurationType": "Debug",
|
||||
"inheritEnvironments": [ "msvc_x64_x64" ],
|
||||
"buildRoot": "${projectDir}\\out\\build\\${name}",
|
||||
"installRoot": "${projectDir}\\out\\install\\${name}",
|
||||
"cmakeCommandArgs": "",
|
||||
"buildCommandArgs": "",
|
||||
"ctestCommandArgs": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
8
Code/CPP-Code/head/head.h
Normal file
8
Code/CPP-Code/head/head.h
Normal file
@@ -0,0 +1,8 @@
|
||||
// 初始化最大长度
|
||||
#define MAXSIZE 5
|
||||
// 定义默认值
|
||||
#define DEFAULTELEM '0'
|
||||
// 定义最大值
|
||||
#define INFINITY 32767
|
||||
// 定义默认数据类型
|
||||
typedef char element_type;
|
||||
161
Code/CPP-Code/head/link_list.h
Normal file
161
Code/CPP-Code/head/link_list.h
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
// 单链表结点
|
||||
typedef struct LinkListNode {
|
||||
element_type data;
|
||||
struct LinkListNode* next;
|
||||
} LinkListNode, *LinkList;
|
||||
|
||||
// 由于C语言无法参数赋值,所以必须借助一个中间变量完成
|
||||
// 初始化有头节点单链表
|
||||
int InitLinkListWithHead(LinkList &list) {
|
||||
list = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (list) {
|
||||
list->data = NULL;
|
||||
list->next = nullptr;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InitLinkListWithHead:分配空间失败!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化无头节点单链表
|
||||
int InitLinkListWithoutHead(LinkList &list) {
|
||||
list = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 创建有头节点单链表
|
||||
LinkList CreateLinkListWithHead() {
|
||||
auto list = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (list) {
|
||||
list->data = NULL;
|
||||
list->next = nullptr;
|
||||
}
|
||||
else {
|
||||
printf("CreateLinkListWithHead:分配空间失败!");
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// 创建无头节点单链表
|
||||
LinkList CreateLinkListWithoutHead() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
// 判断有头节点单链表是否为空
|
||||
int EmptyLinkListWithHead(LinkList list) {
|
||||
if (list->next) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断无头节点单链表是否为空
|
||||
int EmptyLinkListWithoutHead(LinkList list) {
|
||||
if (list) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 插入有头节点单链表元素
|
||||
// 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;
|
||||
}
|
||||
if (index == 0) {
|
||||
LinkListNode* s = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (s) {
|
||||
s->data = elem;
|
||||
// 将s的后继设为list指针
|
||||
s->next = list;
|
||||
// 将list指针设置为s指针
|
||||
list = s;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InsertLinkListWithoutHead:分配内存失败!\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("InsertLinkListWithoutHead:插入索引值过大!\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("InsertLinkListWithoutHead:分配空间失败!\n");
|
||||
}
|
||||
}
|
||||
255
Code/CPP-Code/head/sequence_list.h
Normal file
255
Code/CPP-Code/head/sequence_list.h
Normal file
@@ -0,0 +1,255 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "head.h"
|
||||
|
||||
#pragma warning(disable:6385)
|
||||
#pragma warning(disable:6386)
|
||||
|
||||
// 顺序表
|
||||
class SequenceList {
|
||||
public:
|
||||
element_type *data{};
|
||||
int length{};
|
||||
// 插入函数
|
||||
virtual bool Insert(int index, element_type elem);
|
||||
// 打印函数
|
||||
void Printf();
|
||||
// 循环插入函数
|
||||
bool LoopInsert(element_type *elem, int start, int end);
|
||||
// 删除函数
|
||||
bool Delete(int index, element_type &elem);
|
||||
// 多个删除函数
|
||||
bool MultiDelete(int index, int len, element_type *elem);
|
||||
// 按位获取元素
|
||||
element_type GetElem(int index);
|
||||
// 按值获取元素
|
||||
int Locate(element_type elem);
|
||||
// 判空
|
||||
bool Empty();
|
||||
// 销毁
|
||||
bool Destroy();
|
||||
};
|
||||
|
||||
// 静态顺序表
|
||||
class StaticSequenceList: public SequenceList{
|
||||
public:
|
||||
element_type data[MAXSIZE]{};
|
||||
int length;
|
||||
// 构造函数
|
||||
StaticSequenceList();
|
||||
// 插入函数
|
||||
bool Insert(int index, element_type elem) override;
|
||||
};
|
||||
|
||||
// 动态顺序表
|
||||
class DynamicSequenceList: public SequenceList{
|
||||
public:
|
||||
// 给一个指针来分配动态数组
|
||||
element_type *data;
|
||||
int length;
|
||||
// 已分配的最大容量
|
||||
int max_size;
|
||||
// 构造函数
|
||||
DynamicSequenceList();
|
||||
// 插入函数
|
||||
bool Insert(int index, element_type elem) override;
|
||||
|
||||
private:
|
||||
// 分配其他地址增长动态顺序表的数据空间长度
|
||||
bool OtherIncrease(int len);
|
||||
// 重新分配地址增长动态顺序表的数据空间长度
|
||||
bool ReIncrease(int len);
|
||||
};
|
||||
|
||||
StaticSequenceList::StaticSequenceList() : SequenceList() {
|
||||
this->length=0;
|
||||
}
|
||||
|
||||
DynamicSequenceList::DynamicSequenceList() : SequenceList() {
|
||||
// 初初始化动态顺序表长度为0
|
||||
this->max_size=0;
|
||||
this->length = 0;
|
||||
// 申请一片连续的存储空间
|
||||
auto* space = (element_type*)malloc(MAXSIZE * sizeof(element_type));
|
||||
if (space) {
|
||||
this->data = space;
|
||||
this->max_size = MAXSIZE;
|
||||
}
|
||||
else {
|
||||
printf("InitSequenceList:分配空间失败!\n");
|
||||
}
|
||||
}
|
||||
|
||||
void SequenceList::Printf() {
|
||||
for (int i = 0; i < this->length; i++) {
|
||||
printf("第%d个元素值为%c\n", i + 1, this->data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicSequenceList::OtherIncrease(int len) {
|
||||
if (len <= 0) {
|
||||
printf("OtherIncrease:申请空间应该大于0!\n");
|
||||
return false;
|
||||
}
|
||||
// 申请一片连续的存储空间
|
||||
int new_length = this->max_size + 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->max_size = new_length;
|
||||
free(temp);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
printf("OtherIncrease:重新分配空间失败!\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicSequenceList::ReIncrease(int len) {
|
||||
if (len <= 0) {
|
||||
printf("ReIncrease:申请空间应该大于0!\n");
|
||||
return false;
|
||||
}
|
||||
// 申请一片连续的存储空间
|
||||
int new_length = this->max_size + len;
|
||||
auto* space = (element_type*)realloc(this->data, new_length * sizeof(element_type));
|
||||
if (space) {
|
||||
this->data = space;
|
||||
this->max_size += len;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
this->max_size = 0;
|
||||
this->length = 0;
|
||||
printf("ReIncrease:分配其他地址空间失败!\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool SequenceList::Insert(int index, element_type elem) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool StaticSequenceList::Insert(int index, element_type elem) {
|
||||
// 当静态顺序表已经满了就不能插入任何元素
|
||||
if (this->length >= MAXSIZE) {
|
||||
printf("Insert:静态顺序表空间不足,插入失败!\n");
|
||||
return false;
|
||||
}
|
||||
// 索引位置从0开始,所以可以插入的范围是0到list->length
|
||||
if (index > this->length || index < 0) {
|
||||
printf("Insert:插入索引%d超过索引范围!\n", index);
|
||||
return false;
|
||||
}
|
||||
// 从最后一个元素开始交换后移,list->length是空的
|
||||
for (int i = this->length; i > index; i--) {
|
||||
this->data[i] = this->data[i - 1];
|
||||
}
|
||||
this->data[index] = elem;
|
||||
this->length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DynamicSequenceList::Insert(int index, element_type elem) {
|
||||
if (index > this->length || index < 0) {
|
||||
printf("Insert:插入索引%d超过索引范围!\n", index);
|
||||
return false;
|
||||
}
|
||||
// 当动态顺序表已经满了,需要新增一个位置
|
||||
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
|
||||
if (this->length >= MAXSIZE) {
|
||||
bool result = this->ReIncrease(1);
|
||||
if (!result) {
|
||||
printf("Insert:申请空间失败!\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (int i = this->length; i > index; i--) {
|
||||
this->data[i] = this->data[i - 1];
|
||||
}
|
||||
this->data[index] = elem;
|
||||
this->length++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceList::LoopInsert(element_type *elem, int start, int end) {
|
||||
for (int i = 0; i < end; i++) {
|
||||
bool result = this->Insert(i, elem[i + start]);
|
||||
if (!result) {
|
||||
printf("LoopInsert:循环插入失败!\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceList::Delete(int index, element_type &elem) {
|
||||
if (index >= this->length || index < 0) {
|
||||
printf("Delete:删除索引超过索引范围!\n");
|
||||
return false;
|
||||
}
|
||||
elem = this->data[index];
|
||||
for (int i = index; i < this->length; i++) {
|
||||
this->data[i] = this->data[i + 1];
|
||||
}
|
||||
this->length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SequenceList::MultiDelete(int index, int len, element_type *elem) {
|
||||
if (index + len >= this->length || index < 0) {
|
||||
printf("MultiDelete:删除索引超过索引范围!\n");
|
||||
return false;
|
||||
}
|
||||
for (int i = index; i < this->length - len; i++) {
|
||||
if (i < index + len) {
|
||||
elem[i - index] = this->data[i];
|
||||
}
|
||||
this->data[i] = this->data[i + len];
|
||||
}
|
||||
this->length -= len;
|
||||
return true;
|
||||
}
|
||||
|
||||
element_type SequenceList::GetElem(int index) {
|
||||
if (index >= this->length || index < 0) {
|
||||
printf("GetElem:查找索引超过索引范围!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return this->data[index];
|
||||
}
|
||||
|
||||
|
||||
int SequenceList::Locate(element_type elem) {
|
||||
for (int i = 0; i < this->length; i++) {
|
||||
if (this->data[i] == elem) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
printf("Locate:未能定位到对应值的元素!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool SequenceList::Empty() {
|
||||
if (this->length == 0) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool SequenceList::Destroy() {
|
||||
if (this->data) {
|
||||
free(this->data);
|
||||
}
|
||||
delete this;
|
||||
return true;
|
||||
}
|
||||
|
||||
9
Code/CPP-Code/source/main.cpp
Normal file
9
Code/CPP-Code/source/main.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "test.cpp"
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
SequenceListTest();
|
||||
// LinkListTest();
|
||||
return 0;
|
||||
}
|
||||
46
Code/CPP-Code/source/test.cpp
Normal file
46
Code/CPP-Code/source/test.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
// ²âÊÔÎļþ
|
||||
|
||||
#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();
|
||||
//printf("%c", list.data[2]);
|
||||
/*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');
|
||||
//printf("%d", index);
|
||||
//list.Destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LinkListTest() {
|
||||
LinkList list = nullptr;
|
||||
InitLinkListWithoutHead(list);
|
||||
// InitLinkListWithHead(list);
|
||||
//LinkList list = CreateLinkListWithHead();
|
||||
// int empty = EmptyLinkListWithHead(list);
|
||||
// LinkList list = CreateLinkListWithoutHead();
|
||||
int empty = EmptyLinkListWithoutHead(list);
|
||||
printf("%d", empty);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user