mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-06-17 07:27:12 +08:00
顺序表的单链表更新
This commit is contained in:
@@ -140,7 +140,11 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="head.h" />
|
||||
<ClCompile Include="linear_list.c" />
|
||||
<ClCompile Include="main.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="link_list.h" />
|
||||
<ClInclude Include="sequence_list.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -15,11 +15,19 @@
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="linear_list.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="head.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="link_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="sequence_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,3 +1,4 @@
|
||||
// 初始化最大长度
|
||||
#define MAXSIZE 25
|
||||
typedef int element_type;
|
||||
#define DEFAULTELEM 0
|
||||
typedef int element_type;
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 静态顺序表
|
||||
typedef struct {
|
||||
element_type data[MAXSIZE];
|
||||
int length;
|
||||
} StaticSequenceList;
|
||||
|
||||
// 动态顺序表
|
||||
typedef struct {
|
||||
element_type *data;
|
||||
// 已分配的最大容量
|
||||
int max_size;
|
||||
int length;
|
||||
} DynamicSequenceList;
|
||||
|
||||
// 初始化静态顺序表
|
||||
void InitStaticSequenceList(StaticSequenceList* list) {
|
||||
// 初初始化静态顺序表长度为0
|
||||
list->length = 0;
|
||||
}
|
||||
|
||||
// 初始化动态顺序表
|
||||
void InitDynamicSequenceList(DynamicSequenceList* list) {
|
||||
// 初初始化动态顺序表长度为0
|
||||
list->length = 0;
|
||||
// 申请一片连续的存储空间
|
||||
list->data = (element_type*)malloc(MAXSIZE * sizeof(element_type));
|
||||
list->max_size = MAXSIZE;
|
||||
}
|
||||
|
||||
// 分配其他地址增长动态顺序表的数据空间长度
|
||||
void OtherIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) {
|
||||
// 申请一片连续的存储空间
|
||||
int new_length = list->max_size + len;
|
||||
element_type* space = (element_type*)realloc(list->data, new_length);
|
||||
if (space != NULL) {
|
||||
list->data = space;
|
||||
list->max_size += len;
|
||||
}
|
||||
else {
|
||||
list->max_size = 0;
|
||||
list->length = 0;
|
||||
printf("分配空间失败!");
|
||||
}
|
||||
}
|
||||
|
||||
// 重新分配地址增长动态顺序表的数据空间长度
|
||||
void ReIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) {
|
||||
// 申请一片连续的存储空间
|
||||
int new_length = list->max_size + len;
|
||||
element_type* space = (element_type*)realloc(list->data, new_length);
|
||||
if (space != NULL) {
|
||||
list->data = space;
|
||||
list->max_size += len;
|
||||
}
|
||||
else {
|
||||
list->max_size = 0;
|
||||
list->length = 0;
|
||||
printf("分配空间失败!");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
161
Code/link_list.h
Normal file
161
Code/link_list.h
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 链表结点
|
||||
typedef struct {
|
||||
element_type data;
|
||||
struct LinkNode* next;
|
||||
} LinkNode, *LinkList;
|
||||
|
||||
// 初始化有头节点单链表
|
||||
int InitLinkListWithHead(LinkList list) {
|
||||
list = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
if (list == NULL) {
|
||||
return 1;
|
||||
}
|
||||
list->next = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 初始化无头节点单链表
|
||||
int InitLinkListWithoutHead(LinkList list) {
|
||||
list = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 判断有头节点单链表是否为空
|
||||
int IsLLinkListEmptyWithHead(LinkList list) {
|
||||
if (list->next == NULL) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断无头节点单链表是否为空
|
||||
int IsLLinkListEmptyWithoutHead(LinkList list) {
|
||||
if (list == NULL) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 插入有头节点单链表元素
|
||||
int InsertLinkListWithHead(LinkList list, int index, element_type elem) {
|
||||
if (index < 1) {
|
||||
printf("InsertLinkListWithHead:插入索引值过小!\n");
|
||||
return 1;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkNode* 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
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
if (s == NULL) {
|
||||
printf("InsertLinkListWithHead:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
s->data = elem;
|
||||
// 将p原来的后继给新的结点
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 插入无头节点单链表元素
|
||||
int InsertLinkListWithoutHead(LinkList list, int index, element_type elem) {
|
||||
if (index < 0) {
|
||||
printf("InsertLinkListWithoutHead:插入索引值过小!\n");
|
||||
return 1;
|
||||
}
|
||||
if (index == 0) {
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
if (s == NULL) {
|
||||
printf("InsertLinkListWithoutHead:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
s->data = elem;
|
||||
// 将s的后继设为list指针
|
||||
s->next = list;
|
||||
// 将list指针设置为s指针
|
||||
list = s;
|
||||
return 0;
|
||||
}
|
||||
// 定义一个结点指针p指向当前扫描到的结点
|
||||
LinkNode* 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
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
s->data = elem;
|
||||
// 将p原来的后继给新的结点
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 后插入单链表元素
|
||||
int InsertNextLinkNode(LinkNode* node, element_type elem) {
|
||||
if (node == NULL) {
|
||||
return 1;
|
||||
}
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
// 如果分配空间失败
|
||||
if (s == NULL) {
|
||||
printf("InsertNextLinkNode:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
s->data = elem;
|
||||
s->next = node->next;
|
||||
node->next = s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 前插入单链表元素
|
||||
int InsertPriorLinkNode(LinkNode* node, element_type elem) {
|
||||
if (node == NULL) {
|
||||
return 1;
|
||||
}
|
||||
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
|
||||
// 如果分配空间失败
|
||||
if (s == NULL) {
|
||||
printf("InsertPriorLinkNode:分配内存失败!\n");
|
||||
return 1;
|
||||
}
|
||||
s->next = node->next;
|
||||
node->next = s;
|
||||
s->data = node->data;
|
||||
node->data = elem;
|
||||
return 0;
|
||||
}
|
||||
30
Code/main.c
Normal file
30
Code/main.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#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);
|
||||
}
|
||||
213
Code/sequence_list.h
Normal file
213
Code/sequence_list.h
Normal file
@@ -0,0 +1,213 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
#pragma warning(disable:6385)
|
||||
#pragma warning(disable:6386)
|
||||
|
||||
// 静态顺序表
|
||||
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;
|
||||
}
|
||||
|
||||
// 打印静态顺序表
|
||||
int PrintfStaticSequenceList(StaticSequenceList list) {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
printf("第%d个元素值为%d\n", i + 1, list.data[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 打印动态顺序表
|
||||
int PrintfDynamicSequenceList(DynamicSequenceList list) {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
printf("第%d个元素值为%d\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) {
|
||||
printf("OtherIncreaseDynamicSequenceList:申请空间应该大于0!\n");
|
||||
return 1;
|
||||
}
|
||||
// 申请一片连续的存储空间
|
||||
int new_length = list->max_size + len;
|
||||
element_type* space = (element_type*)malloc(new_length * sizeof(element_type));
|
||||
if (space != NULL) {
|
||||
// 建立中间变量
|
||||
list->data = space;
|
||||
int* temp = list->data;
|
||||
for (int i = 0; i < list->length; i++) {
|
||||
list->data[i] = temp[i];
|
||||
}
|
||||
list->max_size = new_length;
|
||||
free(temp);
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("OtherIncreaseDynamicSequenceList:重新分配空间失败!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 重新分配地址增长动态顺序表的数据空间长度
|
||||
int ReIncreaseDynamicSequenceList(DynamicSequenceList* list, int len) {
|
||||
if (len <= 0) {
|
||||
printf("ReIncreaseDynamicSequenceList:申请空间应该大于0!\n");
|
||||
return 1;
|
||||
}
|
||||
// 申请一片连续的存储空间
|
||||
int new_length = list->max_size + len;
|
||||
element_type* space = (element_type*)realloc(list->data, new_length * sizeof(element_type));
|
||||
if (space != NULL) {
|
||||
list->data = space;
|
||||
list->max_size += len;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
list->max_size = 0;
|
||||
list->length = 0;
|
||||
printf("ReIncreaseDynamicSequenceList:分配其他地址空间失败!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 插入静态顺序表
|
||||
int InsertStaticSequenceList(StaticSequenceList* list, int index, element_type elem) {
|
||||
if (list->length == MAXSIZE) {
|
||||
printf("InsertStaticSequenceList:静态顺序表空间不足,插入失败!\n");
|
||||
return 1;
|
||||
}
|
||||
if (index > list->length || index < 0) {
|
||||
printf("InsertStaticSequenceList:插入索引超过索引范围!\n");
|
||||
return 1;
|
||||
}
|
||||
for (int i = list->length; i > index; i--) {
|
||||
list->data[i] = list->data[i - 1];
|
||||
}
|
||||
list->data[index] = elem;
|
||||
list->length++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 插入动态顺序表
|
||||
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");
|
||||
return 1;
|
||||
}
|
||||
for (int i = list->length; i > index; i--) {
|
||||
list->data[i] = list->data[i - 1];
|
||||
}
|
||||
list->data[index] = elem;
|
||||
list->length++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 删除静态顺序表
|
||||
int DeleteStaticSequenceList(StaticSequenceList* list, int index, element_type *elem) {
|
||||
if (index >= list->length || index < 0) {
|
||||
printf("DeleteStaticSequenceList:删除索引超过索引范围!\n");
|
||||
return 1;
|
||||
}
|
||||
*elem = list->data[index];
|
||||
for (int i = index; i < list->length; i++) {
|
||||
list->data[i] = list->data[i+1];
|
||||
}
|
||||
list->length--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 删除动态顺序表
|
||||
int DeleteDynamicSequenceList(DynamicSequenceList* list, int index, element_type *elem) {
|
||||
if (index >= list->length || index < 0) {
|
||||
printf("DeleteDynamicSequenceList:删除索引超过索引范围!\n");
|
||||
return 1;
|
||||
}
|
||||
*elem = list->data[index];
|
||||
for (int i = index; i < list->length; i++) {
|
||||
list->data[i] = list->data[i + 1];
|
||||
}
|
||||
list->length--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 按位获取静态顺序表元素
|
||||
element_type GetStaticSequenceListElement(StaticSequenceList list, int index) {
|
||||
if (index >= list.length || index < 0) {
|
||||
printf("GetStaticSequenceListElement:查找索引超过索引范围!\n");
|
||||
return 1;
|
||||
}
|
||||
return list.data[index];
|
||||
}
|
||||
|
||||
// 按位获取动态顺序表元素
|
||||
element_type GetDynamicSequenceListElement(DynamicSequenceList list, int index) {
|
||||
if (index >= list.length || index < 0) {
|
||||
printf("GetDynamicSequenceListElement:查找索引超过索引范围!\n");
|
||||
return 1;
|
||||
}
|
||||
return list.data[index];
|
||||
}
|
||||
|
||||
// 按值获取静态顺序表索引
|
||||
int LocateStaticSequenceListElement(StaticSequenceList list, element_type elem) {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
if (list.data[i] == elem) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
printf("LocateStaticSequenceListElement:未能定位到对应值的元素!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 按值获取动态顺序表索引
|
||||
int LocateDynamicSequenceListElement(DynamicSequenceList list, element_type elem) {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
if (list.data[i] == elem) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
printf("LocateDynamicSequenceListElement:未能定位到对应值的元素!\n");
|
||||
return -1;
|
||||
}
|
||||
Reference in New Issue
Block a user