1
0
mirror of https://github.com/Didnelpsun/CS408.git synced 2026-10-31 04:14:52 +08:00

更新顺序表

This commit is contained in:
Didnelpsun
2021-08-09 23:14:08 +08:00
parent 58701736da
commit 4cdb5a75e7
11 changed files with 290 additions and 72 deletions

View File

@@ -141,6 +141,7 @@
<ItemGroup>
<ClCompile Include="head.h" />
<ClCompile Include="main.c" />
<ClCompile Include="test.c" />
<ClCompile Include="sequence_tree.h" />
<ClCompile Include="sequence_stack.h" />
<ClCompile Include="thread_tree.h" />

View File

@@ -24,12 +24,15 @@
<ClCompile Include="sequence_stack.h">
<Filter>头文件</Filter>
</ClCompile>
<ClCompile Include="sequence_tree.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="thread_tree.h">
<Filter>头文件</Filter>
</ClCompile>
<ClCompile Include="sequence_tree.h">
<Filter>头文件</Filter>
</ClCompile>
<ClCompile Include="test.c">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="link_list.h">
@@ -62,5 +65,14 @@
<ClInclude Include="sequence_string.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="graph.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="search.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="sort.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

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

View File

@@ -333,7 +333,7 @@ LinkList TailBuildLinkListWithHead(LinkList list, int length) {
element_type x;
if (length < 1) {
printf("TailBuildLinkListWithHead:输入的单链表长度过小!");
return 1;
return NULL;
}
while (i < length) {
scanf("%d", &x);
@@ -359,7 +359,7 @@ LinkList HeadBuildLinkListWithHead(LinkList list, int length) {
element_type x;
if (length < 1) {
printf("HeadBuildLinkListWithHead:输入的单链表长度过小!");
return 1;
return NULL;
}
while (i < length) {
scanf("%d", &x);

View File

@@ -1,30 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "head.h"
#include "sequence_list.h"
#include "link_list.h"
int main()
{
/*StaticSequenceList static_list;
InitStaticSequenceList(&static_list);
DynamicSequenceList dynamic_list;
InitDynamicSequenceList(&dynamic_list);
printf("%d\n", dynamic_list.max_size);
ReIncreaseDynamicSequenceList(&dynamic_list, 25);
printf("%d\n", dynamic_list.max_size);
InsertStaticSequenceList(&static_list, 0, 2);
InsertStaticSequenceList(&static_list, 1, 6);
InsertStaticSequenceList(&static_list, 2, 5);
PrintfStaticSequenceList(static_list);
element_type i = DEFAULTELEM;
DeleteStaticSequenceList(&static_list, 1, &i);
PrintfStaticSequenceList(static_list);
printf("%d", i);
int elem = 65;
int index = LocateStaticSequenceListElement(static_list, elem);
printf("²éÕÒÔªËØ%dÔÚÐòÁÐ%dλ\n", elem, index + 1);
return 0;*/
LinkList list;
InitLinkList(&list);
SequenceListTest();
return 0;
}

View File

@@ -8,28 +8,61 @@
// 静态顺序表
typedef struct {
element_type data[MAXSIZE];
// 长度
int length;
} StaticSequenceList;
// 动态顺序表
typedef struct {
// 给一个指针来分配动态数组
element_type *data;
// 已分配的最大容量
int max_size;
// 长度
int length;
} DynamicSequenceList;
// 初始化静态顺序表
int InitStaticSequenceList(StaticSequenceList* list) {
// 初初始化静态顺序表长度为0
list->length = 0;
return 0;
if (list == NULL) {
printf("InitStaticSequenceList:指针指向为NULL!\n");
return 1;
}
else {
// 初初始化静态顺序表长度为0
list->length = 0;
return 0;
}
}
// 初始化动态顺序表
int InitDynamicSequenceList(DynamicSequenceList* list) {
if (list == NULL) {
printf("InitDynamicSequenceList:指针指向为NULL!\n");
return 1;
}
else {
// 初初始化动态顺序表长度为0
list->length = 0;
list->max_size = 0;
// 申请一片连续的存储空间
element_type* space = (element_type*)malloc(MAXSIZE * sizeof(element_type));
if (space != NULL) {
list->data = space;
list->max_size = MAXSIZE;
return 0;
}
else {
printf("InitDynamicSequenceList:分配空间失败!\n");
return 1;
}
}
}
// 打印静态顺序表
int PrintfStaticSequenceList(StaticSequenceList list) {
for (int i = 0; i < list.length; i++) {
printf("第%d个元素值为%d\n", i + 1, list.data[i]);
printf("第%d个元素值为%c\n", i + 1, list.data[i]);
}
return 0;
}
@@ -37,30 +70,11 @@ int PrintfStaticSequenceList(StaticSequenceList list) {
// 打印动态顺序表
int PrintfDynamicSequenceList(DynamicSequenceList list) {
for (int i = 0; i < list.length; i++) {
printf("第%d个元素值为%d\n", i + 1, list.data[i]);
printf("第%d个元素值为%c\n", i + 1, list.data[i]);
}
return 0;
}
// 初始化动态顺序表
int InitDynamicSequenceList(DynamicSequenceList* list) {
// 初初始化动态顺序表长度为0
list->length = 0;
list->max_size = 0;
// 申请一片连续的存储空间
element_type* space = (element_type*)malloc(MAXSIZE * sizeof(element_type));
if (space != NULL) {
list->data = space;
list->max_size = MAXSIZE;
return 0;
}
else {
list->max_size = 0;
printf("InitDynamicSequenceList:初始化失败!\n");
return 1;
}
}
// 分配其他地址增长动态顺序表的数据空间长度
int OtherIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) {
if (len <= 0) {
@@ -73,7 +87,7 @@ int OtherIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) {
if (space != NULL) {
// 建立中间变量
list->data = space;
int* temp = list->data;
element_type* temp = list->data;
for (int i = 0; i < list->length; i++) {
list->data[i] = temp[i];
}
@@ -111,14 +125,17 @@ int ReIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) {
// 插入静态顺序表
int InsertStaticSequenceList(StaticSequenceList* list, int index, element_type elem) {
if (list->length == MAXSIZE) {
// 当静态顺序表已经满了就不能插入任何元素
if (list->length >= MAXSIZE) {
printf("InsertStaticSequenceList:静态顺序表空间不足,插入失败!\n");
return 1;
}
// 索引位置从0开始,所以可以插入的范围是0到list->length
if (index > list->length || index < 0) {
printf("InsertStaticSequenceList:插入索引超过索引范围!\n");
printf("InsertStaticSequenceList:插入索引%d超过索引范围!\n", index);
return 1;
}
// 从最后一个元素开始交换后移,list->length是空的
for (int i = list->length; i > index; i--) {
list->data[i] = list->data[i - 1];
}
@@ -129,13 +146,19 @@ int InsertStaticSequenceList(StaticSequenceList* list, int index, element_type e
// 插入动态顺序表
int InsertDynamicSequenceList(DynamicSequenceList* list, int index, element_type elem) {
if (list->length == MAXSIZE) {
ReIncreaseDynamicSequenceList(list, 1);
}
if (index > list->length || index < 0) {
printf("InsertDynamicSequenceList:插入索引超过索引范围!\n");
printf("InsertDynamicSequenceList:插入索引%d超过索引范围!\n", index);
return 1;
}
// 当动态顺序表已经满了,需要新增一个位置
// 为了避免索引无效而多增加一个空间,所以放在检查索引值的后面
if (list->length >= MAXSIZE) {
int result = ReIncreaseDynamicSequenceList(list, 1);
if (result == 1) {
printf("InsertDynamicSequenceList:申请空间失败!\n");
return 1;
}
}
for (int i = list->length; i > index; i--) {
list->data[i] = list->data[i - 1];
}
@@ -144,6 +167,30 @@ int InsertDynamicSequenceList(DynamicSequenceList* list, int index, element_type
return 0;
}
// 循环插入静态顺序表
int LoopInsertStaticSequenceList(StaticSequenceList* list, element_type* elem, int start, int end) {
for (int i = 0; i < end; i++) {
int result = InsertStaticSequenceList(list, i, elem[i + start]);
if (result == 1) {
printf("LoopInsertStaticSequenceList:循环插入失败!\n");
return 1;
}
}
return 0;
}
// 循环插入动态顺序表
int LoopInsertDynamicSequenceList(DynamicSequenceList* list, element_type* elem, int start, int end) {
for (int i = 0; i < end; i++) {
int result = InsertDynamicSequenceList(list, i, elem[i + start]);
if (result == 1) {
printf("LoopInsertDynamicSequenceList:循环插入失败!\n");
return 1;
}
}
return 0;
}
// 删除静态顺序表
int DeleteStaticSequenceList(StaticSequenceList* list, int index, element_type *elem) {
if (index >= list->length || index < 0) {
@@ -152,7 +199,7 @@ int DeleteStaticSequenceList(StaticSequenceList* list, int index, element_type *
}
*elem = list->data[index];
for (int i = index; i < list->length; i++) {
list->data[i] = list->data[i+1];
list->data[i] = list->data[i + 1];
}
list->length--;
return 0;
@@ -172,6 +219,47 @@ int DeleteDynamicSequenceList(DynamicSequenceList* list, int index, element_type
return 0;
}
// 删除多个静态顺序表
int MultiDeleteStaticSequenceList(StaticSequenceList* list, int index, int len, element_type* elem) {
if (index + len >= list->length || index < 0) {
printf("MultiDeleteStaticSequenceList:删除索引超过索引范围!\n");
return 1;
}
elem = (element_type*)malloc(len * sizeof(element_type));
if (elem == NULL) {
printf("MultiDeleteStaticSequenceList:分配空间失败!\n");
}
else {
for (int i = index; i < list->length - len; i++) {
elem[i - index] = list->data[i];
list->data[i] = list->data[i + len];
}
list->length -= len;
}
list->length -= len;
return 0;
}
// 删除多个动态顺序表
int MultiDeleteDynamicSequenceList(DynamicSequenceList* list, int index, int len, element_type* elem) {
if (index + len >= list->length || index < 0) {
printf("MultiDeleteDynamicSequenceList:删除索引超过索引范围!\n");
return 1;
}
elem = (element_type*)malloc(len * sizeof(element_type));
if (elem == NULL) {
printf("MultiDeleteDynamicSequenceList:分配空间失败!\n");
}
else {
for (int i = index; i < list->length - len; i++) {
elem[i - index] = list->data[i];
list->data[i] = list->data[i + len];
}
list->length -= len;
}
return 0;
}
// 按位查找静态顺序表元素
element_type GetStaticSequenceListElement(StaticSequenceList list, int index) {
if (index >= list.length || index < 0) {
@@ -210,4 +298,30 @@ int LocateDynamicSequenceListElement(DynamicSequenceList list, element_type elem
}
printf("LocateDynamicSequenceListElement:未能定位到对应值的元素!\n");
return -1;
}
}
// 判空静态顺序表
int EmptyStaticSequenceList(StaticSequenceList list) {
if (list.length == 0) {
return 1;
}
else {
return 0;
}
}
// 判空动态顺序表
int EmptyDynamicSequenceList(DynamicSequenceList list) {
if (list.length == 0) {
return 1;
}
else {
return 0;
}
}
// 销毁动态顺序表
int DestroyDynamicSequenceList(DynamicSequenceList* list) {
free(list);
}

27
Code/test.c Normal file
View File

@@ -0,0 +1,27 @@
// ²âÊÔÎļþ
#include "head.h"
#include "sequence_list.h"
int SequenceListTest() {
DynamicSequenceList list;
InitDynamicSequenceList(&list);
element_type a[6] = {'1','2','3','4','5','6'};
LoopInsertDynamicSequenceList(&list, a, 0, 6);
element_type b[3] = { 9, 'a', 'e' };
LoopInsertDynamicSequenceList(&list, b, 1, 2);
//printf("%d", list.length);
PrintfDynamicSequenceList(list);
printf("\n");
element_type elem;
MultiDeleteDynamicSequenceList(&list, 2, 2, &elem);
PrintfDynamicSequenceList(list);
/*DynamicSequenceList dlist;
InitDynamicSequenceList(&dlist);
OtherIncreaseDynamicSequenceList(&dlist, 15);
printf("%d", dlist.max_size);*/
int index = LocateDynamicSequenceListElement(list, '5');
printf("%d", index);
return 0;
}

View File

@@ -24,6 +24,7 @@ int InorderThread(ThreadTreeNode* node) {
pre->rtag = 1;
}
pre = node;
return 0;
}
// 找到以node为根结点的子树中第一个被中序遍历的结点
@@ -51,6 +52,7 @@ int InorderTraversalThreadTree(ThreadTree tree, int(*visit)(ThreadTreeNode* node
for (ThreadTreeNode* p = FristInOrderNode(tree); p != NULL; p = NextInOrderNode(p)) {
visit(p);
}
return 0;
}
// 找到以node为根结点的子树中前面最后一个被中序遍历的结点
@@ -78,4 +80,5 @@ int ReverseInorderTraversalThreadTree(ThreadTree tree, int(*visit)(ThreadTreeNod
for (ThreadTreeNode* p = LastInOrderNode(tree); p != NULL; p = PreInOrderNode(p)) {
visit(p);
}
return 0;
}