mirror of
https://github.com/Didnelpsun/CS408.git
synced 2026-02-10 06:05:51 +08:00
更新代码
This commit is contained in:
31
Code/C-Code/C-Code.sln
Normal file
31
Code/C-Code/C-Code.sln
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.31605.320
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "C-Code", "C-Code.vcxproj", "{0097210B-6B4C-4528-90F7-28DC333772CF}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Debug|x64.Build.0 = Debug|x64
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Debug|x86.Build.0 = Debug|Win32
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Release|x64.ActiveCfg = Release|x64
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Release|x64.Build.0 = Release|x64
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Release|x86.ActiveCfg = Release|Win32
|
||||
{0097210B-6B4C-4528-90F7-28DC333772CF}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {D42A6D35-5080-4B03-A977-17AEC5B1ED3D}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
153
Code/C-Code/C-Code.vcxproj
Normal file
153
Code/C-Code/C-Code.vcxproj
Normal file
@@ -0,0 +1,153 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{0097210b-6b4c-4528-90f7-28dc333772cf}</ProjectGuid>
|
||||
<RootNamespace>CCode</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="source\main.c" />
|
||||
<ClCompile Include="source\test.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="head\head.h" />
|
||||
<ClInclude Include="head\link_list.h" />
|
||||
<ClInclude Include="head\sequence_list.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
36
Code/C-Code/C-Code.vcxproj.filters
Normal file
36
Code/C-Code/C-Code.vcxproj.filters
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="source\main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="source\test.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="head\head.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="head\link_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="head\sequence_list.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
4
Code/C-Code/C-Code.vcxproj.user
Normal file
4
Code/C-Code/C-Code.vcxproj.user
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
</Project>
|
||||
8
Code/C-Code/head/head.h
Normal file
8
Code/C-Code/head/head.h
Normal file
@@ -0,0 +1,8 @@
|
||||
// 初始化最大长度
|
||||
#define MAXSIZE 5
|
||||
// 定义默认值
|
||||
#define DEFAULTELEM '0'
|
||||
// 定义最大值
|
||||
#define INFINITY 32767
|
||||
// 定义默认数据类型
|
||||
typedef char element_type;
|
||||
114
Code/C-Code/head/link_list.h
Normal file
114
Code/C-Code/head/link_list.h
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
// 单链表结点
|
||||
typedef struct LinkListNode {
|
||||
element_type data;
|
||||
struct LinkListNode* next;
|
||||
} LinkListNode, *LinkList;
|
||||
|
||||
// 由于C语言无法参数赋值,所以必须借助一个中间变量完成
|
||||
// 初始化有头节点单链表
|
||||
int InitLinkListWithHead(LinkList list) {
|
||||
if (list) {
|
||||
list->data = NULL;
|
||||
list->next = NULL;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InitLinkListWithHead:指针为空无法初始化!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// C语言无法这样实现初始化,因为无法实现引用传递,不能赋值参数
|
||||
// 初始化无头节点单链表
|
||||
int InitLinkListWithoutHead(LinkList list) {
|
||||
list = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 创建有头节点单链表
|
||||
LinkList CreateLinkListWithHead() {
|
||||
LinkList list = (LinkListNode*)malloc(sizeof(LinkListNode));
|
||||
if (list) {
|
||||
list->data = NULL;
|
||||
list->next = NULL;
|
||||
}
|
||||
else {
|
||||
printf("CreateLinkListWithHead:分配空间失败!");
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// 创建无头节点单链表
|
||||
LinkList CreateLinkListWithoutHead() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// 判断有头节点单链表是否为空
|
||||
int EmptyLinkListWithHead(LinkList list) {
|
||||
if (list->next) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断无头节点单链表是否为空
|
||||
int EmptyLLinkListWithoutHead(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) {
|
||||
return 1;
|
||||
}
|
||||
322
Code/C-Code/head/sequence_list.h
Normal file
322
Code/C-Code/head/sequence_list.h
Normal file
@@ -0,0 +1,322 @@
|
||||
#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) {
|
||||
if (list) {
|
||||
// 初初始化静态顺序表长度为0
|
||||
list->length = 0;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InitStaticSequenceList:指针指向为NULL!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化动态顺序表
|
||||
int InitDynamicSequenceList(DynamicSequenceList* list) {
|
||||
if (list) {
|
||||
// 初初始化动态顺序表长度为0
|
||||
list->length = 0;
|
||||
list->max_size = 0;
|
||||
// 申请一片连续的存储空间
|
||||
element_type* space = (element_type*)malloc(MAXSIZE * sizeof(element_type));
|
||||
if (space) {
|
||||
list->data = space;
|
||||
list->max_size = MAXSIZE;
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
printf("InitDynamicSequenceList:分配空间失败!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("InitDynamicSequenceList:指针指向为NULL!\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 打印静态顺序表
|
||||
int PrintfStaticSequenceList(StaticSequenceList list) {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
printf("第%d个元素值为%c\n", i + 1, list.data[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 打印动态顺序表
|
||||
int PrintfDynamicSequenceList(DynamicSequenceList list) {
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
printf("第%d个元素值为%c\n", i + 1, list.data[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 分配其他地址增长动态顺序表的数据空间长度
|
||||
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) {
|
||||
// 建立中间变量
|
||||
list->data = space;
|
||||
element_type* 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) {
|
||||
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;
|
||||
}
|
||||
// 索引位置从0开始,所以可以插入的范围是0到list->length
|
||||
if (index > list->length || index < 0) {
|
||||
printf("InsertStaticSequenceList:插入索引%d超过索引范围!\n", index);
|
||||
return 1;
|
||||
}
|
||||
// 从最后一个元素开始交换后移,list->length是空的
|
||||
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 (index > list->length || index < 0) {
|
||||
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];
|
||||
}
|
||||
list->data[index] = elem;
|
||||
list->length++;
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
// 删除多个静态顺序表
|
||||
int MultiDeleteStaticSequenceList(StaticSequenceList* list, int index, int len, element_type* elem) {
|
||||
if (index + len >= list->length || index < 0) {
|
||||
printf("MultiDeleteStaticSequenceList:删除索引超过索引范围!\n");
|
||||
return 1;
|
||||
}
|
||||
for (int i = index; i < list->length - len; i++) {
|
||||
if (i < index + len) {
|
||||
elem[i - index] = list->data[i];
|
||||
}
|
||||
list->data[i] = list->data[i + 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;
|
||||
}
|
||||
for (int i = index; i < list->length - len; i++) {
|
||||
if (i < index + len) {
|
||||
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) {
|
||||
printf("GetStaticSequenceListElement:查找索引超过索引范围!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
return list.data[index];
|
||||
}
|
||||
|
||||
// 按位查找动态顺序表元素
|
||||
element_type GetDynamicSequenceListElement(DynamicSequenceList list, int index) {
|
||||
if (index >= list.length || index < 0) {
|
||||
printf("GetDynamicSequenceListElement:查找索引超过索引范围!\n");
|
||||
return DEFAULTELEM;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
// 判空静态顺序表
|
||||
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) {
|
||||
if (list != NULL) {
|
||||
free(list->data);
|
||||
}
|
||||
list = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
10
Code/C-Code/source/main.c
Normal file
10
Code/C-Code/source/main.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
//SequenceListTest();
|
||||
LinkListTest();
|
||||
return 0;
|
||||
}
|
||||
45
Code/C-Code/source/test.c
Normal file
45
Code/C-Code/source/test.c
Normal file
@@ -0,0 +1,45 @@
|
||||
// ²âÊÔÎļþ
|
||||
|
||||
#include "../Head/sequence_list.h"
|
||||
#include "../Head/link_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");
|
||||
int len = 2;
|
||||
element_type elem[2];
|
||||
MultiDeleteDynamicSequenceList(&list, 0, len, elem);
|
||||
PrintfDynamicSequenceList(list);
|
||||
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 = LocateDynamicSequenceListElement(list, '5');
|
||||
printf("%d", index);
|
||||
DestroyDynamicSequenceList(&list);*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LinkListTest() {
|
||||
/*LinkListNode node;
|
||||
InitLinkListWithHead(&node);
|
||||
LinkList list = &node;*/
|
||||
LinkList list = CreateLinkListWithHead();
|
||||
/*int empty = EmptyLinkListWithHead(list);*/
|
||||
/*LinkList list = CreateLinkListWithoutHead();
|
||||
int empty = EmptyLLinkListWithoutHead(list);
|
||||
printf("%d", empty);*/
|
||||
InsertLinkListWithHead(list, 1, '0');
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user