mirror of
https://github.com/kangjianwei/Data-Structure.git
synced 2026-09-28 15:48:55 +08:00
♻️ 线性表章节的源码重构
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqList.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="SqList-main.c" />
|
||||
<ClCompile Include="SqList.c" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{4DDF7C1A-9DF8-4983-B478-1B8928B8A239}</ProjectGuid>
|
||||
<RootNamespace>My0201_SqList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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>
|
||||
<ClInclude Include="SqList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="SqList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SqList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,170 @@
|
||||
#include <stdio.h>
|
||||
#include "SqList.h" //**02 线性表**//
|
||||
|
||||
// 判断data>e是否成立
|
||||
Status CmpGreater(ElemType data, ElemType e) {
|
||||
return data > e ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
SqList L; // 待操作的顺序表
|
||||
|
||||
int i;
|
||||
ElemType e;
|
||||
|
||||
printf("████████ InitList \n");
|
||||
{
|
||||
printf("█ 初始化顺序表 L ...\n");
|
||||
InitList(&L);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListEmpty \n");
|
||||
{
|
||||
if(ListEmpty(L) == TRUE) {
|
||||
printf("█ L 为空!!\n");
|
||||
} else {
|
||||
printf("█ L 不为空!\n");
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListInsert \n");
|
||||
{
|
||||
for(i = 1; i <= 8; i++) {
|
||||
printf("█ 作为示范,在 L 第 %d 个位置插入 \"%d\"...\n", i, 2 * i);
|
||||
ListInsert(&L, i, 2 * i);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListTraverse \n");
|
||||
{
|
||||
printf("█ L 中的元素为:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListLength \n");
|
||||
{
|
||||
i = ListLength(L);
|
||||
printf("█ L 的长度为 %d \n", i);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListDelete \n");
|
||||
{
|
||||
printf("█ 删除前的元素:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
|
||||
printf("█ 尝试删除 L 中第 6 个元素...\n");
|
||||
|
||||
if(ListDelete(&L, 6, &e) == OK) {
|
||||
printf("█ 删除成功,被删除元素是:\"%d\"\n", e);
|
||||
} else {
|
||||
printf("█ 删除失败,第 6 个元素不存在!\n");
|
||||
}
|
||||
|
||||
printf("█ 删除后的元素:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ GetElem \n");
|
||||
{
|
||||
GetElem(L, 4, &e);
|
||||
printf("█ L 中第 4 个位置的元素为 \"%d\" \n", e);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ LocateElem \n");
|
||||
{
|
||||
i = LocateElem(L, 7, CmpGreater);
|
||||
printf("█ L 中第一个元素值大于 \"7\" 的元素是 \"%d\" \n", L.elem[i - 1]);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ PriorElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(PriorElem(L, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的前驱为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的前驱不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ NextElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(NextElem(L, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的后继为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的后继不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ClearList \n");
|
||||
{
|
||||
printf("█ 清空 L 前:");
|
||||
if(ListEmpty(L) == TRUE) {
|
||||
printf(" L 为空!!\n");
|
||||
} else {
|
||||
printf(" L 不为空!\n");
|
||||
}
|
||||
|
||||
ClearList(&L);
|
||||
|
||||
printf("█ 清空 L 后:");
|
||||
if(ListEmpty(L) == TRUE) {
|
||||
printf(" L 为空!!\n");
|
||||
} else {
|
||||
printf(" L 不为空!\n");
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ DestroyList \n");
|
||||
{
|
||||
printf("█ 销毁 L 前:");
|
||||
if(L.elem != NULL) {
|
||||
printf(" L 存在!\n");
|
||||
} else {
|
||||
printf(" L 不存在!!\n");
|
||||
}
|
||||
|
||||
DestroyList(&L);
|
||||
|
||||
printf("█ 销毁 L 后:");
|
||||
if(L.elem != NULL) {
|
||||
printf(" L 存在!\n");
|
||||
} else {
|
||||
printf(" L 不存在!!\n");
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,149 @@
|
||||
/*=============================
|
||||
* 线性表的顺序存储结构(顺序表)
|
||||
*
|
||||
* 包含算法: 2.3、2.4、2.5、2.6
|
||||
=============================*/
|
||||
|
||||
#ifndef SEQUENCELIST_H
|
||||
#define SEQUENCELIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // 提供malloc、realloc、free、exit原型
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/* 宏定义 */
|
||||
#define LIST_INIT_SIZE 100 // 顺序表存储空间的初始分配量
|
||||
#define LISTINCREMENT 10 // 顺序表存储空间的分配增量
|
||||
|
||||
/* 顺序表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 顺序表结构
|
||||
*
|
||||
* 注:elem在使用前需要先为其分配内存,且元素从elem[0]处开始存储
|
||||
*/
|
||||
typedef struct {
|
||||
ElemType* elem; // 顺序表存储空间的基址(指向顺序表所占内存的起始位置)
|
||||
int length; // 当前顺序表长度(包含多少元素)
|
||||
int listsize; // 当前分配的存储容量(可以存储多少元素)
|
||||
} SqList;
|
||||
|
||||
|
||||
/*
|
||||
* ████████ 算法2.3 ████████
|
||||
*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(SqList* L);
|
||||
|
||||
/*
|
||||
* 销毁(结构)
|
||||
*
|
||||
* 释放顺序表所占内存。
|
||||
*/
|
||||
Status DestroyList(SqList* L);
|
||||
|
||||
/*
|
||||
* 置空(内容)
|
||||
*
|
||||
* 只是清理顺序表中存储的数据,不释放顺序表所占内存。
|
||||
*/
|
||||
Status ClearList(SqList* L);
|
||||
|
||||
/*
|
||||
* 判空
|
||||
*
|
||||
* 判断顺序表中是否包含有效数据。
|
||||
*
|
||||
* 返回值:
|
||||
* TRUE : 顺序表为空
|
||||
* FALSE: 顺序表不为空
|
||||
*/
|
||||
Status ListEmpty(SqList L);
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回顺序表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(SqList L);
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取顺序表中第i个元素,将其存储到e中。
|
||||
* 如果可以找到,返回OK,否则,返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。
|
||||
* 通常,i的含义应该指索引,即从0开始计数。
|
||||
*/
|
||||
Status GetElem(SqList L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.6 ████████
|
||||
*
|
||||
* 查找
|
||||
*
|
||||
* 返回顺序表中首个与e满足Compare关系的元素位序。
|
||||
* 如果不存在这样的元素,则返回0。
|
||||
*
|
||||
*【备注】
|
||||
* 元素e是Compare函数第二个形参
|
||||
*/
|
||||
int LocateElem(SqList L, ElemType e, Status(Compare)(ElemType, ElemType));
|
||||
|
||||
/*
|
||||
* 前驱
|
||||
*
|
||||
* 获取元素cur_e的前驱,
|
||||
* 如果存在,将其存储到pre_e中,返回OK,
|
||||
* 如果不存在,则返回ERROR。
|
||||
*/
|
||||
Status PriorElem(SqList L, ElemType cur_e, ElemType* pre_e);
|
||||
|
||||
/*
|
||||
* 后继
|
||||
*
|
||||
* 获取元素cur_e的后继,
|
||||
* 如果存在,将其存储到next_e中,返回OK,
|
||||
* 如果不存在,则返回ERROR。
|
||||
*/
|
||||
Status NextElem(SqList L, ElemType cur_e, ElemType* next_e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.4 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向顺序表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListInsert(SqList* L, int i, ElemType e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.5 ████████
|
||||
*
|
||||
* 删除
|
||||
*
|
||||
* 删除顺序表第i个位置上的元素,并将被删除元素存储到e中。
|
||||
* 删除成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListDelete(SqList* L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问顺序表L
|
||||
*/
|
||||
void ListTraverse(SqList L, void (Visit)(ElemType));
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,78 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqList.h" />
|
||||
<ClInclude Include="Union.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="SqList.c" />
|
||||
<ClCompile Include="Union-main.c" />
|
||||
<ClCompile Include="Union.c" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{5C53EFFE-EE74-42B8-875C-CFB7B2659917}</ProjectGuid>
|
||||
<RootNamespace>My0202_Union</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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>
|
||||
<ClInclude Include="SqList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Union.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="SqList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Union.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Union-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,172 @@
|
||||
/*=============================
|
||||
* 线性表的顺序存储结构(顺序表)
|
||||
*
|
||||
* 包含算法: 2.3、2.4、2.5、2.6
|
||||
=============================*/
|
||||
|
||||
#include "SqList.h"
|
||||
|
||||
/*
|
||||
* ████████ 算法2.3 ████████
|
||||
*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(SqList* L) {
|
||||
// 分配指定容量的内存,如果分配失败,则返回NULL
|
||||
(*L).elem = (ElemType*) malloc(LIST_INIT_SIZE * sizeof(ElemType));
|
||||
if((*L).elem == NULL) {
|
||||
// 存储内存失败
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
(*L).length = 0; // 初始化顺序表长度为0
|
||||
(*L).listsize = LIST_INIT_SIZE; // 顺序表初始内存分配量
|
||||
|
||||
return OK; // 初始化成功
|
||||
}
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回顺序表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(SqList L) {
|
||||
return L.length;
|
||||
}
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取顺序表中第i个元素,将其存储到e中。
|
||||
* 如果可以找到,返回OK,否则,返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。
|
||||
* 通常,i的含义应该指索引,即从0开始计数。
|
||||
*/
|
||||
Status GetElem(SqList L, int i, ElemType* e) {
|
||||
// 因为i的含义是位置,所以其合法范围是:[1, length]
|
||||
if(i < 1 || i > L.length) {
|
||||
return ERROR; //i值不合法
|
||||
}
|
||||
|
||||
*e = L.elem[i - 1];
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* ████████ 算法2.6 ████████
|
||||
*
|
||||
* 查找
|
||||
*
|
||||
* 返回顺序表中首个与e满足Compare关系的元素位序。
|
||||
* 如果不存在这样的元素,则返回0。
|
||||
*
|
||||
*【备注】
|
||||
* 元素e是Compare函数第二个形参
|
||||
*/
|
||||
int LocateElem(SqList L, ElemType e, Status(Compare)(ElemType, ElemType)) {
|
||||
int i;
|
||||
ElemType* p;
|
||||
|
||||
// 确保顺序表结构存在
|
||||
if(L.elem == NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/*
|
||||
* i的初值为第1个元素的位序
|
||||
*
|
||||
* 其实,更自然的写法是将i初始化为第1个元素的索引
|
||||
* 但由于教材中是按位序计数的,所以这里仍写作位序
|
||||
*/
|
||||
i = 1;
|
||||
|
||||
// p的初值为第1个元素的存储位置
|
||||
p = L.elem;
|
||||
|
||||
// 遍历顺序表
|
||||
while(i <= L.length && !Compare(*p++, e)) {
|
||||
++i;
|
||||
}
|
||||
|
||||
if(i <= L.length) {
|
||||
return i;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ████████ 算法2.4 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向顺序表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListInsert(SqList* L, int i, ElemType e) {
|
||||
ElemType* newbase;
|
||||
ElemType* p, * q;
|
||||
|
||||
// 确保顺序表结构存在
|
||||
if(L == NULL || (*L).elem == NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// i值越界
|
||||
if(i < 1 || i > (*L).length + 1) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 若存储空间已满,则增加新空间
|
||||
if((*L).length >= (*L).listsize) {
|
||||
// 基于现有空间扩容
|
||||
newbase = (ElemType*) realloc((*L).elem, ((*L).listsize + LISTINCREMENT) * sizeof(ElemType));
|
||||
if(newbase == NULL) {
|
||||
// 存储内存失败
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
// 新基址
|
||||
(*L).elem = newbase;
|
||||
// 存的存储空间
|
||||
(*L).listsize += LISTINCREMENT;
|
||||
}
|
||||
|
||||
// q为插入位置
|
||||
q = &(*L).elem[i - 1];
|
||||
|
||||
// 1.右移元素,腾出位置
|
||||
for(p = &(*L).elem[(*L).length - 1]; p >= q; --p) {
|
||||
*(p + 1) = *p;
|
||||
}
|
||||
|
||||
// 2.插入e
|
||||
*q = e;
|
||||
|
||||
// 3.表长增1
|
||||
(*L).length++;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问顺序表L
|
||||
*/
|
||||
void ListTraverse(SqList L, void(Visit)(ElemType)) {
|
||||
int i;
|
||||
|
||||
for(i = 0; i < L.length; i++) {
|
||||
Visit(L.elem[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*=============================
|
||||
* 线性表的顺序存储结构(顺序表)
|
||||
*
|
||||
* 包含算法: 2.3、2.4、2.5、2.6
|
||||
=============================*/
|
||||
|
||||
#ifndef SEQUENCELIST_H
|
||||
#define SEQUENCELIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // 提供malloc、realloc、free、exit原型
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/* 宏定义 */
|
||||
#define LIST_INIT_SIZE 100 // 顺序表存储空间的初始分配量
|
||||
#define LISTINCREMENT 10 // 顺序表存储空间的分配增量
|
||||
|
||||
/* 顺序表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 顺序表结构
|
||||
*
|
||||
* 注:elem在使用前需要先为其分配内存,且元素从elem[0]处开始存储
|
||||
*/
|
||||
typedef struct {
|
||||
ElemType* elem; // 顺序表存储空间的基址(指向顺序表所占内存的起始位置)
|
||||
int length; // 当前顺序表长度(包含多少元素)
|
||||
int listsize; // 当前分配的存储容量(可以存储多少元素)
|
||||
} SqList;
|
||||
|
||||
|
||||
/*
|
||||
* ████████ 算法2.3 ████████
|
||||
*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(SqList* L);
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回顺序表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(SqList L);
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取顺序表中第i个元素,将其存储到e中。
|
||||
* 如果可以找到,返回OK,否则,返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。
|
||||
* 通常,i的含义应该指索引,即从0开始计数。
|
||||
*/
|
||||
Status GetElem(SqList L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.6 ████████
|
||||
*
|
||||
* 查找
|
||||
*
|
||||
* 返回顺序表中首个与e满足Compare关系的元素位序。
|
||||
* 如果不存在这样的元素,则返回0。
|
||||
*
|
||||
*【备注】
|
||||
* 元素e是Compare函数第二个形参
|
||||
*/
|
||||
int LocateElem(SqList L, ElemType e, Status(Compare)(ElemType, ElemType));
|
||||
|
||||
/*
|
||||
* ████████ 算法2.4 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向顺序表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListInsert(SqList* L, int i, ElemType e);
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问顺序表L
|
||||
*/
|
||||
void ListTraverse(SqList L, void (Visit)(ElemType));
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include "Union.h" //**▲02 线性表**//
|
||||
#include "SqList.h" //**▲02 线性表**//
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
ElemType a[5] = {5, 2, 1, 3, 9};
|
||||
ElemType b[7] = {7, 2, 6, 9, 11, 3, 10};
|
||||
SqList La, Lb;
|
||||
int i;
|
||||
|
||||
// 初始化La
|
||||
InitList(&La);
|
||||
for(i = 1; i <= 5; i++) {
|
||||
ListInsert(&La, i, a[i - 1]);
|
||||
}
|
||||
|
||||
// 初始化Lb
|
||||
InitList(&Lb);
|
||||
for(i = 1; i <= 7; i++) {
|
||||
ListInsert(&Lb, i, b[i - 1]);
|
||||
}
|
||||
|
||||
// 输出La
|
||||
printf("La = ");
|
||||
ListTraverse(La, PrintElem);
|
||||
|
||||
// 输出Lb
|
||||
printf("Lb = ");
|
||||
ListTraverse(Lb, PrintElem);
|
||||
|
||||
// 输出新表La的内容
|
||||
printf("La = La∪Lb = ");
|
||||
Union(&La, Lb);
|
||||
ListTraverse(La, PrintElem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*==============
|
||||
* 求并集
|
||||
*
|
||||
* 包含算法: 2.1
|
||||
===============*/
|
||||
|
||||
#include "Union.h" //**▲02 线性表**//
|
||||
|
||||
/*
|
||||
* ████████ 算法2.1 ████████
|
||||
*
|
||||
* A=A∪B
|
||||
*
|
||||
* 计算La与Lb的并集并返回。
|
||||
* 由于生成的并集会拼接在La上,所以La的入参为指针类型。
|
||||
*/
|
||||
void Union(SqList* La, SqList Lb) {
|
||||
int La_len, Lb_len;
|
||||
int i;
|
||||
ElemType e;
|
||||
|
||||
// 求顺序表长度
|
||||
La_len = ListLength(*La);
|
||||
Lb_len = ListLength(Lb);
|
||||
|
||||
for(i = 1; i <= Lb_len; i++) {
|
||||
// 取Lb中第i个元素赋给e
|
||||
GetElem(Lb, i, &e);
|
||||
|
||||
// 若e不在La中则插入
|
||||
if(!LocateElem(*La, e, equal)) {
|
||||
ListInsert(La, ++La_len, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 判等
|
||||
*
|
||||
* 判断两元素是否相等。
|
||||
* 如果相等,则返回TRUE,否则,返回FALSE。
|
||||
*/
|
||||
Status equal(ElemType e1, ElemType e2) {
|
||||
return e1 == e2 ? TRUE : FALSE;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*==============
|
||||
* 求并集
|
||||
*
|
||||
* 包含算法: 2.1
|
||||
===============*/
|
||||
|
||||
#ifndef UNION_H
|
||||
#define UNION_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
#include "SqList.h" //**▲02 线性表**//
|
||||
|
||||
|
||||
/*
|
||||
* ████████ 算法2.1 ████████
|
||||
*
|
||||
* A=A∪B
|
||||
*
|
||||
* 计算La与Lb的并集并返回。
|
||||
* 由于生成的并集会拼接在La上,所以La的入参为指针类型。
|
||||
*/
|
||||
void Union(SqList* La, SqList Lb);
|
||||
|
||||
/*
|
||||
* 判等
|
||||
*
|
||||
* 判断两元素是否相等。
|
||||
* 如果相等,则返回TRUE,否则,返回FALSE。
|
||||
*/
|
||||
Status equal(ElemType e1, ElemType e2);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,78 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="MergeSqList-main.c" />
|
||||
<ClCompile Include="MergeSqList.c" />
|
||||
<ClCompile Include="SqList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="MergeSqList.h" />
|
||||
<ClInclude Include="SqList.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{D3309A60-3384-4EB0-9392-BEB5BF9934BF}</ProjectGuid>
|
||||
<RootNamespace>My0203_MergeSqList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="MergeSqList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="MergeSqList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SqList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="MergeSqList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SqList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include "SqList.h"
|
||||
#include "MergeSqList.h" //**▲02 线性表**//
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
ElemType a[4] = {3, 5, 8, 11};
|
||||
ElemType b[7] = {2, 6, 8, 9, 11, 15, 20};
|
||||
|
||||
SqList La, Lb, Lc1, Lc2;
|
||||
int i;
|
||||
|
||||
// 初始化La
|
||||
InitList(&La);
|
||||
for(i = 1; i <= 4; i++) {
|
||||
ListInsert(&La, i, a[i - 1]);
|
||||
}
|
||||
|
||||
// 初始化Lb
|
||||
InitList(&Lb);
|
||||
for(i = 1; i <= 7; i++) {
|
||||
ListInsert(&Lb, i, b[i - 1]);
|
||||
}
|
||||
|
||||
// 输出La
|
||||
printf("La = ");
|
||||
ListTraverse(La, PrintElem);
|
||||
|
||||
// 输出Lb
|
||||
printf("Lb = ");
|
||||
ListTraverse(Lb, PrintElem);
|
||||
|
||||
// 归并顺序表La和Lb,算法2.2
|
||||
MergeSqList_1(La, Lb, &Lc1);
|
||||
printf("归并La和Lb为Lc1 = ");
|
||||
ListTraverse(Lc1, PrintElem);
|
||||
|
||||
// 归并顺序表La和Lb,算法2.7
|
||||
MergeSqList_2(La, Lb, &Lc2);
|
||||
printf("归并La和Lb为Lc2 = ");
|
||||
ListTraverse(Lc2, PrintElem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*==================
|
||||
* 归并非降序顺序表
|
||||
*
|
||||
* 包含算法: 2.2、2.7
|
||||
===================*/
|
||||
|
||||
#include "MergeSqList.h" //**▲02 线性表**//
|
||||
|
||||
|
||||
/*
|
||||
* ████████ 算法2.2 ████████
|
||||
*
|
||||
* 非递减链表归并:C=A+B
|
||||
*
|
||||
* 归并顺序表La和Lb,生成新的顺序表Lc。
|
||||
* 其中,La、Lb、Lc均为非递减序列。
|
||||
*/
|
||||
void MergeSqList_1(SqList La, SqList Lb, SqList* Lc) {
|
||||
int La_len, Lb_len;
|
||||
int i, j, k;
|
||||
ElemType ai, bj;
|
||||
|
||||
i = j = 1;
|
||||
k = 0;
|
||||
|
||||
// 初始化Lc
|
||||
InitList(Lc);
|
||||
|
||||
// 获取La、Lb的长度
|
||||
La_len = ListLength(La);
|
||||
Lb_len = ListLength(Lb);
|
||||
|
||||
// 如果La及Lb均未遍历完
|
||||
while(i <= La_len && j <= Lb_len) {
|
||||
GetElem(La, i, &ai);
|
||||
GetElem(Lb, j, &bj);
|
||||
|
||||
// 比较遍历到的元素,先将比较小的元素加入顺序表Lc
|
||||
if(ai <= bj) {
|
||||
ListInsert(Lc, ++k, ai);
|
||||
i++;
|
||||
} else {
|
||||
ListInsert(Lc, ++k, bj);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果Lb已遍历完,但La还未遍历完,将La中剩余元素加入Lc
|
||||
while(i <= La_len) {
|
||||
GetElem(La, i++, &ai);
|
||||
ListInsert(Lc, ++k, ai);
|
||||
}
|
||||
|
||||
// 如果La已遍历完,但Lb还未遍历完,将Lb中剩余元素加入Lc
|
||||
while(j <= Lb_len) {
|
||||
GetElem(Lb, j++, &bj);
|
||||
ListInsert(Lc, ++k, bj);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ████████ 算法2.7 ████████
|
||||
*
|
||||
* 非递减链表归并:C=A+B
|
||||
*
|
||||
* 归并顺序表La和Lb,生成新的顺序表Lc。
|
||||
* 其中,La、Lb、Lc均为非递减序列。
|
||||
*/
|
||||
void MergeSqList_2(SqList La, SqList Lb, SqList* Lc) {
|
||||
ElemType* pa, * pb, * pc;
|
||||
ElemType* pa_last, * pb_last;
|
||||
|
||||
pa = La.elem; // 指向La第一个元素
|
||||
pb = Lb.elem; // 指向Lb第一个元素
|
||||
|
||||
// 没有使用InitList创建Lc
|
||||
(*Lc).listsize = (*Lc).length = La.length + Lb.length;
|
||||
pc = (*Lc).elem = (ElemType*) malloc((*Lc).listsize * sizeof(ElemType));
|
||||
if(pc == NULL) {
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
pa_last = La.elem + La.length - 1; // 指向La最后一个元素
|
||||
pb_last = Lb.elem + Lb.length - 1; // 指向Lb最后一个元素
|
||||
|
||||
// 如果La及Lb均未遍历完
|
||||
while(pa <= pa_last && pb <= pb_last) {
|
||||
if(*pa <= *pb) {
|
||||
*pc++ = *pa++;
|
||||
} else {
|
||||
*pc++ = *pb++;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果Lb已遍历完,但La还未遍历完,将La中剩余元素加入Lc
|
||||
while(pa <= pa_last) {
|
||||
*pc++ = *pa++;
|
||||
}
|
||||
|
||||
// 如果La已遍历完,但Lb还未遍历完,将Lb中剩余元素加入Lc
|
||||
while(pb <= pb_last) {
|
||||
*pc++ = *pb++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*==================
|
||||
* 归并非降序顺序表
|
||||
*
|
||||
* 包含算法: 2.2、2.7
|
||||
===================*/
|
||||
|
||||
#ifndef MERGESQLIST_H
|
||||
#define MERGESQLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "SqList.h" //**▲02 线性表**//
|
||||
|
||||
|
||||
/*
|
||||
* ████████ 算法2.2 ████████
|
||||
*
|
||||
* 非递减链表归并:C=A+B
|
||||
*
|
||||
* 归并顺序表La和Lb,生成新的顺序表Lc。
|
||||
* 其中,La、Lb、Lc均为非递减序列。
|
||||
*/
|
||||
void MergeSqList_1(SqList La, SqList Lb, SqList* Lc);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.7 ████████
|
||||
*
|
||||
* 非递减链表归并:C=A+B
|
||||
*
|
||||
* 归并顺序表La和Lb,生成新的顺序表Lc。
|
||||
* 其中,La、Lb、Lc均为非递减序列。
|
||||
*/
|
||||
void MergeSqList_2(SqList La, SqList Lb, SqList* Lc);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,129 @@
|
||||
/*=============================
|
||||
* 线性表的顺序存储结构(顺序表)
|
||||
*
|
||||
* 包含算法: 2.3、2.4、2.5、2.6
|
||||
=============================*/
|
||||
|
||||
#include "SqList.h"
|
||||
|
||||
/*
|
||||
* ████████ 算法2.3 ████████
|
||||
*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(SqList* L) {
|
||||
// 分配指定容量的内存,如果分配失败,则返回NULL
|
||||
(*L).elem = (ElemType*) malloc(LIST_INIT_SIZE * sizeof(ElemType));
|
||||
if((*L).elem == NULL) {
|
||||
// 存储内存失败
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
(*L).length = 0; // 初始化顺序表长度为0
|
||||
(*L).listsize = LIST_INIT_SIZE; // 顺序表初始内存分配量
|
||||
|
||||
return OK; // 初始化成功
|
||||
}
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回顺序表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(SqList L) {
|
||||
return L.length;
|
||||
}
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取顺序表中第i个元素,将其存储到e中。
|
||||
* 如果可以找到,返回OK,否则,返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。
|
||||
* 通常,i的含义应该指索引,即从0开始计数。
|
||||
*/
|
||||
Status GetElem(SqList L, int i, ElemType* e) {
|
||||
// 因为i的含义是位置,所以其合法范围是:[1, length]
|
||||
if(i < 1 || i > L.length) {
|
||||
return ERROR; //i值不合法
|
||||
}
|
||||
|
||||
*e = L.elem[i - 1];
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* ████████ 算法2.4 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向顺序表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListInsert(SqList* L, int i, ElemType e) {
|
||||
ElemType* newbase;
|
||||
ElemType* p, * q;
|
||||
|
||||
// 确保顺序表结构存在
|
||||
if(L == NULL || (*L).elem == NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// i值越界
|
||||
if(i < 1 || i > (*L).length + 1) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
// 若存储空间已满,则增加新空间
|
||||
if((*L).length >= (*L).listsize) {
|
||||
// 基于现有空间扩容
|
||||
newbase = (ElemType*) realloc((*L).elem, ((*L).listsize + LISTINCREMENT) * sizeof(ElemType));
|
||||
if(newbase == NULL) {
|
||||
// 存储内存失败
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
// 新基址
|
||||
(*L).elem = newbase;
|
||||
// 存的存储空间
|
||||
(*L).listsize += LISTINCREMENT;
|
||||
}
|
||||
|
||||
// q为插入位置
|
||||
q = &(*L).elem[i - 1];
|
||||
|
||||
// 1.右移元素,腾出位置
|
||||
for(p = &(*L).elem[(*L).length - 1]; p >= q; --p) {
|
||||
*(p + 1) = *p;
|
||||
}
|
||||
|
||||
// 2.插入e
|
||||
*q = e;
|
||||
|
||||
// 3.表长增1
|
||||
(*L).length++;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问顺序表L
|
||||
*/
|
||||
void ListTraverse(SqList L, void(Visit)(ElemType)) {
|
||||
int i;
|
||||
|
||||
for(i = 0; i < L.length; i++) {
|
||||
Visit(L.elem[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*=============================
|
||||
* 线性表的顺序存储结构(顺序表)
|
||||
*
|
||||
* 包含算法: 2.3、2.4、2.5、2.6
|
||||
=============================*/
|
||||
|
||||
#ifndef SEQUENCELIST_H
|
||||
#define SEQUENCELIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // 提供malloc、realloc、free、exit原型
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/* 宏定义 */
|
||||
#define LIST_INIT_SIZE 100 // 顺序表存储空间的初始分配量
|
||||
#define LISTINCREMENT 10 // 顺序表存储空间的分配增量
|
||||
|
||||
/* 顺序表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 顺序表结构
|
||||
*
|
||||
* 注:elem在使用前需要先为其分配内存,且元素从elem[0]处开始存储
|
||||
*/
|
||||
typedef struct {
|
||||
ElemType* elem; // 顺序表存储空间的基址(指向顺序表所占内存的起始位置)
|
||||
int length; // 当前顺序表长度(包含多少元素)
|
||||
int listsize; // 当前分配的存储容量(可以存储多少元素)
|
||||
} SqList;
|
||||
|
||||
|
||||
/*
|
||||
* ████████ 算法2.3 ████████
|
||||
*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(SqList* L);
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回顺序表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(SqList L);
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取顺序表中第i个元素,将其存储到e中。
|
||||
* 如果可以找到,返回OK,否则,返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。
|
||||
* 通常,i的含义应该指索引,即从0开始计数。
|
||||
*/
|
||||
Status GetElem(SqList L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.4 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向顺序表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListInsert(SqList* L, int i, ElemType e);
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问顺序表L
|
||||
*/
|
||||
void ListTraverse(SqList L, void (Visit)(ElemType));
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="LinkList-main.c" />
|
||||
<ClCompile Include="LinkList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_Head.txt" />
|
||||
<None Include="TestData_Tail.txt" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{8818D7C5-70C7-4649-9690-ACB30147C37B}</ProjectGuid>
|
||||
<RootNamespace>My0204_LinkList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="LinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="LinkList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_Head.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
<None Include="TestData_Tail.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,170 @@
|
||||
#include <stdio.h>
|
||||
#include "Status.h"
|
||||
#include "LinkList.h" //**▲02 线性表**//
|
||||
|
||||
// 判断data>e是否成立
|
||||
Status CmpGreater(ElemType data, ElemType e) {
|
||||
return data > e ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
LinkList L;
|
||||
int i;
|
||||
ElemType e;
|
||||
|
||||
printf("████████ InitList \n");
|
||||
{
|
||||
printf("█ 初始化单链表 L ...\n");
|
||||
InitList(&L);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListEmpty \n");
|
||||
{
|
||||
ListEmpty(L) ? printf("█ L 为空!!\n") : printf("█ L 不为空!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListInsert \n");
|
||||
{
|
||||
for(i = 1; i <= 8; i++) {
|
||||
printf("█ 在 L 第 %d 个位置插入 \"%d\" ...\n", i, 2 * i);
|
||||
ListInsert(L, i, 2 * i);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListTraverse \n");
|
||||
{
|
||||
printf("█ L 中的元素为:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListLength \n");
|
||||
{
|
||||
printf("█ L 的长度为 %d \n", ListLength(L));
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListDelete \n");
|
||||
{
|
||||
printf("█ 删除前的元素:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
|
||||
printf("█ 尝试删除 L 中第 6 个元素...\n");
|
||||
|
||||
if(ListDelete(L, 6, &e) == OK) {
|
||||
printf("█ 删除成功,被删除元素是:\"%d\"\n", e);
|
||||
} else {
|
||||
printf("█ 删除失败,第 6 个元素不存在!\n");
|
||||
}
|
||||
|
||||
printf("█ 删除后的元素:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ GetElem \n");
|
||||
{
|
||||
GetElem(L, 4, &e);
|
||||
printf("█ L 中第 4 个位置的元素为 \"%d\" \n", e);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ LocateElem \n");
|
||||
{
|
||||
i = LocateElem(L, 7, CmpGreater);
|
||||
GetElem(L, i, &e);
|
||||
printf("█ L 中第一个元素值大于 \"7\" 的元素是 \"%d\" \n", e);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ PriorElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(PriorElem(L, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的前驱为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的前驱不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ NextElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(NextElem(L, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的后继为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的后继不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ClearList \n");
|
||||
{
|
||||
printf("█ 清空 L 前:");
|
||||
ListEmpty(L) ? printf(" L 为空!!\n") : printf(" L 不为空!\n");
|
||||
|
||||
ClearList(L);
|
||||
|
||||
printf("█ 清空 L 后:");
|
||||
ListEmpty(L) ? printf(" L 为空!!\n") : printf(" L 不为空!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ DestroyList \n");
|
||||
{
|
||||
printf("█ 销毁 L 前:");
|
||||
L ? printf(" L 存在!\n") : printf(" L 不存在!!\n");
|
||||
|
||||
DestroyList(&L);
|
||||
|
||||
printf("█ 销毁 L 后:");
|
||||
L ? printf(" L 存在!\n") : printf(" L 不存在!!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ CreateList_Head \n");
|
||||
{
|
||||
LinkList L;
|
||||
printf("█ 头插法建立单链表 L = ");
|
||||
CreateList_Head("TestData_Head.txt", &L, 5);
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ CreateList_Tail \n");
|
||||
{
|
||||
LinkList L;
|
||||
printf("█ 尾插法建立单链表 L = ");
|
||||
CreateList_Tail("TestData_Tail.txt", &L, 5);
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,168 @@
|
||||
/*===============================
|
||||
* 线性表的链式存储结构(链表)
|
||||
*
|
||||
* 包含算法: 2.8、2.9、2.10、2.11
|
||||
================================*/
|
||||
|
||||
#ifndef LINKEDLIST_H
|
||||
#define LINKEDLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> //提供malloc、realloc、free、exit原型
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/* 单链表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 单链表结构
|
||||
*
|
||||
* 注:这里的单链表存在头结点
|
||||
*/
|
||||
typedef struct LNode {
|
||||
ElemType data; // 数据结点
|
||||
struct LNode* next; // 指向下一个结点的指针
|
||||
} LNode;
|
||||
|
||||
// 指向单链表结点的指针
|
||||
typedef LNode* LinkList;
|
||||
|
||||
|
||||
/*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(LinkList* L);
|
||||
|
||||
/*
|
||||
* 销毁(结构)
|
||||
*
|
||||
* 释放链表所占内存。
|
||||
*/
|
||||
Status DestroyList(LinkList* L);
|
||||
|
||||
/*
|
||||
* 置空(内容)
|
||||
*
|
||||
* 这里需要释放链表中非头结点处的空间。
|
||||
*/
|
||||
Status ClearList(LinkList L);
|
||||
|
||||
/*
|
||||
* 判空
|
||||
*
|
||||
* 判断链表中是否包含有效数据。
|
||||
*
|
||||
* 返回值:
|
||||
* TRUE : 链表为空
|
||||
* FALSE: 链表不为空
|
||||
*/
|
||||
Status ListEmpty(LinkList L);
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回链表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(LinkList L);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.8 ████████
|
||||
*
|
||||
* 取值
|
||||
*
|
||||
* 获取链表中第i个元素,将其存储到e中。
|
||||
* 如果可以找到,返回OK,否则,返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。
|
||||
* 通常,i的含义应该指索引,即从0开始计数。
|
||||
*/
|
||||
Status GetElem(LinkList L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* 查找
|
||||
*
|
||||
* 返回链表中首个与e满足Compare关系的元素位序。
|
||||
* 如果不存在这样的元素,则返回0。
|
||||
*
|
||||
*【备注】
|
||||
* 元素e是Compare函数第二个形参
|
||||
*/
|
||||
int LocateElem(LinkList L, ElemType e, Status(Compare)(ElemType, ElemType));
|
||||
|
||||
/*
|
||||
* 前驱
|
||||
*
|
||||
* 获取元素cur_e的前驱,
|
||||
* 如果存在,将其存储到pre_e中,返回OK,
|
||||
* 如果不存在,则返回ERROR。
|
||||
*/
|
||||
Status PriorElem(LinkList L, ElemType cur_e, ElemType* pre_e);
|
||||
|
||||
/*
|
||||
* 后继
|
||||
*
|
||||
* 获取元素cur_e的后继,
|
||||
* 如果存在,将其存储到next_e中,返回OK,
|
||||
* 如果不存在,则返回ERROR。
|
||||
*/
|
||||
Status NextElem(LinkList L, ElemType cur_e, ElemType* next_e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.9 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向链表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListInsert(LinkList L, int i, ElemType e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.10 ████████
|
||||
*
|
||||
* 删除
|
||||
*
|
||||
* 删除链表第i个位置上的元素,并将被删除元素存储到e中。
|
||||
* 删除成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListDelete(LinkList L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问链表L
|
||||
*/
|
||||
void ListTraverse(LinkList L, void(Visit)(ElemType));
|
||||
|
||||
/*
|
||||
* ████████ 算法2.11 ████████
|
||||
*
|
||||
* 头插法创建链表
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据。
|
||||
* 另教材中的算法采用的是头插法,所以此处的实现加了后缀_Head。
|
||||
*/
|
||||
Status CreateList_Head(char path[], LinkList* L, int n);
|
||||
|
||||
/*
|
||||
* 尾插法创建链表
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据
|
||||
*/
|
||||
Status CreateList_Tail(char path[], LinkList* L, int n);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1 @@
|
||||
降序:9 7 5 3 1
|
||||
@@ -0,0 +1 @@
|
||||
升序:2 4 6 8 10
|
||||
@@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="LinkList.c" />
|
||||
<ClCompile Include="MergeList-main.c" />
|
||||
<ClCompile Include="MergeList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h" />
|
||||
<ClInclude Include="MergeList.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_Head.txt" />
|
||||
<None Include="TestData_Tail.txt" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{DC3475CD-A081-423F-BCE5-14464B66F475}</ProjectGuid>
|
||||
<RootNamespace>My0205_MergeList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="LinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="MergeList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="MergeList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="MergeList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_Head.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
<None Include="TestData_Tail.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,138 @@
|
||||
/*===============================
|
||||
* 线性表的链式存储结构(链表)
|
||||
*
|
||||
* 包含算法: 2.8、2.9、2.10、2.11
|
||||
================================*/
|
||||
|
||||
#include "LinkList.h" //**▲02 线性表**//
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问链表L
|
||||
*/
|
||||
void ListTraverse(LinkList L, void(Visit)(ElemType)) {
|
||||
LinkList p;
|
||||
|
||||
// 确保链表存在
|
||||
if(L == NULL || L->next == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
p = L->next;
|
||||
|
||||
while(p != NULL) {
|
||||
Visit(p->data);
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* ████████ 算法2.11 ████████
|
||||
*
|
||||
* 头插法创建链表
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据。
|
||||
* 另教材中的算法采用的是头插法,所以此处的实现加了后缀_Head。
|
||||
*/
|
||||
Status CreateList_Head(char path[], LinkList* L, int n) {
|
||||
FILE* fp;
|
||||
int i;
|
||||
LinkList p;
|
||||
ElemType e;
|
||||
|
||||
// 建立头结点
|
||||
*L = (LinkList) malloc(sizeof(LNode));
|
||||
if(*L == NULL) {
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
(*L)->next = NULL;
|
||||
|
||||
// 打开文件,准备读取测试数据
|
||||
fp = fopen(path, "r");
|
||||
if(fp == NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
for(i = 1; i <= n; ++i) {
|
||||
// 如果成功读取到输入数据
|
||||
if(ReadData(fp, "%d", &e) == 1) {
|
||||
// 生成新结点
|
||||
p = (LinkList) malloc(sizeof(LNode));
|
||||
if(p == NULL) {
|
||||
fclose(fp);
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
// 填充数据,并插入到链表中
|
||||
p->data = e;
|
||||
p->next = (*L)->next;
|
||||
(*L)->next = p;
|
||||
} else {
|
||||
fclose(fp);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* 尾插法创建链表
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据。
|
||||
*/
|
||||
Status CreateList_Tail(char path[], LinkList* L, int n) {
|
||||
FILE* fp;
|
||||
int i;
|
||||
LinkList p, q;
|
||||
ElemType e;
|
||||
|
||||
// 建立头结点
|
||||
*L = (LinkList) malloc(sizeof(LNode));
|
||||
if(*L==NULL) {
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
(*L)->next = NULL;
|
||||
|
||||
// 打开文件,准备读取测试数据
|
||||
fp = fopen(path, "r");
|
||||
if(fp == NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
for(i = 1, q = *L; i <= n; ++i) {
|
||||
// 如果成功读取到输入数据
|
||||
if(ReadData(fp, "%d", &e) == 1) {
|
||||
p = (LinkList) malloc(sizeof(LNode));
|
||||
if(p==NULL) {
|
||||
fclose(fp);
|
||||
exit(OVERFLOW);
|
||||
}
|
||||
|
||||
// 填充数据,并插入到链表中
|
||||
p->data = e;
|
||||
q->next = p;
|
||||
q = q->next;
|
||||
} else {
|
||||
fclose(fp);
|
||||
return ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
q->next = NULL;
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return OK;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*===============================
|
||||
* 线性表的链式存储结构(链表)
|
||||
*
|
||||
* 包含算法: 2.8、2.9、2.10、2.11
|
||||
================================*/
|
||||
|
||||
#ifndef LINKEDLIST_H
|
||||
#define LINKEDLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> //提供malloc、realloc、free、exit原型
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/* 单链表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 单链表结构
|
||||
*
|
||||
* 注:这里的单链表存在头结点
|
||||
*/
|
||||
typedef struct LNode {
|
||||
ElemType data; // 数据结点
|
||||
struct LNode* next; // 指向下一个结点的指针
|
||||
} LNode;
|
||||
|
||||
// 指向单链表结点的指针
|
||||
typedef LNode* LinkList;
|
||||
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问链表L
|
||||
*/
|
||||
void ListTraverse(LinkList L, void(Visit)(ElemType));
|
||||
|
||||
/*
|
||||
* ████████ 算法2.11 ████████
|
||||
*
|
||||
* 头插法创建链表
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据。
|
||||
* 另教材中的算法采用的是头插法,所以此处的实现加了后缀_Head。
|
||||
*/
|
||||
Status CreateList_Head(char path[], LinkList* L, int n);
|
||||
|
||||
/*
|
||||
* 尾插法创建链表
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据
|
||||
*/
|
||||
Status CreateList_Tail(char path[], LinkList* L, int n);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include "MergeList.h" //**▲02 线性表**//
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
LinkList La, Lb, Lc;
|
||||
|
||||
int m = 5;
|
||||
|
||||
printf("作为示例,La长度设定为 %d ,Lb长度设定为 %d ,创建La和Lb...\n", m, m);
|
||||
|
||||
CreateList_Head("TestData_Head.txt", &La, m);
|
||||
printf("La = ");
|
||||
ListTraverse(La, PrintElem);
|
||||
|
||||
CreateList_Tail("TestData_Tail.txt", &Lb, m);
|
||||
printf("Lb = ");
|
||||
ListTraverse(Lb, PrintElem);
|
||||
|
||||
printf("归并La和Lb为Lc = ");
|
||||
MergeList(&La, &Lb, &Lc);
|
||||
ListTraverse(Lc, PrintElem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*===============
|
||||
* 归并非降序链表
|
||||
*
|
||||
* 包含算法: 2.12
|
||||
================*/
|
||||
|
||||
#include "MergeList.h" //**▲02 线性表**//
|
||||
|
||||
/*
|
||||
* ████████ 算法2.12 ████████
|
||||
*
|
||||
* 非递减链表归并:C=A+B
|
||||
*
|
||||
* 将链表A和B归并为C,且保持元素相对位置不变。
|
||||
* Lc利用La的头结点,Lb中结点均插入新链表Lc中。
|
||||
*/
|
||||
void MergeList(LinkList* La, LinkList* Lb, LinkList* Lc) {
|
||||
LinkList pa, pb, pc;
|
||||
|
||||
pa = (*La)->next;
|
||||
pb = (*Lb)->next;
|
||||
pc = *Lc = *La; // 用La的头结点作为Lc的头结点
|
||||
|
||||
// 遍历La和Lb
|
||||
while(pa && pb) {
|
||||
if(pa->data <= pb->data) {
|
||||
pc->next = pa;
|
||||
pc = pa;
|
||||
pa = pa->next;
|
||||
} else {
|
||||
pc->next = pb;
|
||||
pc = pb;
|
||||
pb = pb->next;
|
||||
}
|
||||
}
|
||||
|
||||
// 插入剩余段
|
||||
pc->next = pa ? pa : pb;
|
||||
|
||||
// 释放Lb的头结点所占内存
|
||||
free(*Lb);
|
||||
|
||||
*La = NULL;
|
||||
*Lb = NULL;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*===============
|
||||
* 归并非降序链表
|
||||
*
|
||||
* 包含算法: 2.12
|
||||
================*/
|
||||
|
||||
#ifndef MERGELIST_H
|
||||
#define MERGELIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> //提供malloc、realloc、free、exit原型
|
||||
#include "LinkList.h" //**▲02 线性表**//
|
||||
|
||||
/*
|
||||
* ████████ 算法2.12 ████████
|
||||
*
|
||||
* 非递减链表归并:C=A+B
|
||||
*
|
||||
* 将链表A和B归并为C,且保持元素相对位置不变。
|
||||
* Lc利用La的头结点,Lb中结点均插入新链表Lc中。
|
||||
*/
|
||||
void MergeList(LinkList* La, LinkList* Lb, LinkList* Lc);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1 @@
|
||||
降序:9 7 5 3 1
|
||||
@@ -0,0 +1 @@
|
||||
升序:2 4 6 8 10
|
||||
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{C0C969A9-926E-4AE3-87AC-F73CB04D0BC7}</ProjectGuid>
|
||||
<RootNamespace>My0206_SLinkList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="SLinkList-main.c" />
|
||||
<ClCompile Include="SLinkList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SLinkList.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="SLinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SLinkList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SLinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,152 @@
|
||||
#include <stdio.h>
|
||||
#include "Status.h"
|
||||
#include "SLinkList.h" //**▲02 线性表**//
|
||||
|
||||
// 判断data>e是否成立
|
||||
Status CmpGreater(ElemType data, ElemType e) {
|
||||
return data > e ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
SLinkList space; // 备用空间
|
||||
int S; // 静态链表头结点索引
|
||||
|
||||
int i;
|
||||
ElemType e;
|
||||
|
||||
printf("████████ InitList \n");
|
||||
{
|
||||
printf("█ 初始化单链表 S ...\n");
|
||||
InitList(space, &S);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListEmpty \n");
|
||||
{
|
||||
ListEmpty(space, S) ? printf("█ S 为空!!\n") : printf("█ S 不为空!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListInsert \n");
|
||||
{
|
||||
for(i = 1; i <= 8; i++) {
|
||||
printf("█ 在 S 第 %d 个位置插入 \"%d\" ...\n", i, 2 * i);
|
||||
ListInsert(space, S, i, 2 * i);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListTraverse \n");
|
||||
{
|
||||
printf("█ S 中的元素为:S = ");
|
||||
ListTraverse(space, S, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListLength \n");
|
||||
{
|
||||
printf("█ S 的长度为 %d \n", ListLength(space, S));
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListDelete \n");
|
||||
{
|
||||
printf("█ 删除前的元素:S = ");
|
||||
ListTraverse(space, S, PrintElem);
|
||||
|
||||
printf("█ 尝试删除 S 中第 6 个元素...\n");
|
||||
|
||||
if(ListDelete(space, S, 6, &e) == OK) {
|
||||
printf("█ 删除成功,被删除元素是:\"%d\"\n", e);
|
||||
} else {
|
||||
printf("█ 删除失败,第 6 个元素不存在!\n");
|
||||
}
|
||||
|
||||
printf("█ 删除后的元素:S = ");
|
||||
ListTraverse(space, S, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ GetElem \n");
|
||||
{
|
||||
GetElem(space, S, 4, &e);
|
||||
printf("█ S 中第 4 个位置的元素为 \"%d\" \n", e);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ LocateElem \n");
|
||||
{
|
||||
i = LocateElem(space, S, 7, CmpGreater);
|
||||
GetElem(space, S, i, &e);
|
||||
printf("█ S 中第一个元素值大于 \"7\" 的元素是 \"%d\" \n", e);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ PriorElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(PriorElem(space, S, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的前驱为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的前驱不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ NextElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(NextElem(space, S, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的后继为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的后继不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ClearList \n");
|
||||
{
|
||||
printf("█ 清空 S 前:");
|
||||
ListEmpty(space, S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
|
||||
|
||||
ClearList(space, S);
|
||||
|
||||
printf("█ 清空 S 后:");
|
||||
ListEmpty(space, S) ? printf(" S 为空!!\n") : printf(" S 不为空!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ DestroyList \n");
|
||||
{
|
||||
printf("█ 销毁 S 前:");
|
||||
S!=0 ? printf(" S 存在!\n") : printf(" S 不存在!!\n");
|
||||
|
||||
DestroyList(space, &S);
|
||||
|
||||
printf("█ 销毁 S 后:");
|
||||
S!=0 ? printf(" S 存在!\n") : printf(" S 不存在!!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,81 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{EB17D84C-751A-4261-BE9F-B02951BF8E51}</ProjectGuid>
|
||||
<RootNamespace>My0207_Difference</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Difference-main.c" />
|
||||
<ClCompile Include="Difference.c" />
|
||||
<ClCompile Include="SLinkList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Difference.h" />
|
||||
<ClInclude Include="SLinkList.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData.txt" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="Difference.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Difference-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SLinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Difference.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SLinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include "Difference.h" //**▲02 线性表**//
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
SLinkList space; // 备用空间
|
||||
int S; // 静态链表
|
||||
|
||||
printf("S = (A-B)∪(B-A) = ");
|
||||
difference("TestData.txt", space, &S);
|
||||
ListTraverse(space, S, PrintElem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*===============
|
||||
* 静态链表去重
|
||||
*
|
||||
* 包含算法: 2.17
|
||||
================*/
|
||||
|
||||
#include "Difference.h"
|
||||
|
||||
/*
|
||||
* ████████ 算法2.17 ████████
|
||||
*
|
||||
* S = (A-B)∪(B-A)
|
||||
*
|
||||
* 对集合A和集合B进行(A-B)∪(B-A)计算,计算结果存入静态链表S
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据。
|
||||
*/
|
||||
void difference(char path[], SLinkList space, int* S) {
|
||||
FILE* fp;
|
||||
int m, n; // 集合A和集合B中元素数量
|
||||
int j; // 循环计数器
|
||||
int R; // 指向静态链表最后一个结点
|
||||
int i, k, p;
|
||||
int b; // 临时存储从集合B中读到的数据
|
||||
|
||||
// 初始化备用空间
|
||||
InitSpace(space);
|
||||
|
||||
// 获取静态链表头结点
|
||||
*S = Malloc(space);
|
||||
|
||||
// 让R执行静态链表最后的结点
|
||||
R = *S;
|
||||
|
||||
// 打开文件,准备读取测试数据
|
||||
fp = fopen(path, "r");
|
||||
if(fp == NULL) {
|
||||
exit(ERROR);
|
||||
}
|
||||
|
||||
// 读取集合A和集合B的元素个数
|
||||
ReadData(fp, "%d%d", &m, &n);
|
||||
|
||||
// 录入集合A的数据
|
||||
for(j = 1; j <= m; ++j) {
|
||||
// 分配结点
|
||||
i = Malloc(space);
|
||||
|
||||
// 输入集合A的元素值
|
||||
ReadData(fp, "%d", &space[i].data);
|
||||
|
||||
// 将新结点插入到表尾
|
||||
space[R].cur = i;
|
||||
R = i;
|
||||
}
|
||||
|
||||
// 尾结点的指针置空
|
||||
space[R].cur = 0;
|
||||
|
||||
// 录入集合B的数据
|
||||
for(j = 1; j <= n; ++j) {
|
||||
// 输入集合B的元素值
|
||||
ReadData(fp, "%d", &b);
|
||||
|
||||
p = *S; // 指向静态链表头结点,后续总是指向k的前一个位置
|
||||
k = space[*S].cur; // 指向静态链表中的首个元素
|
||||
|
||||
// 在当前静态链表中查找是否存在b元素
|
||||
while(k != space[R].cur && space[k].data != b) {
|
||||
p = k;
|
||||
k = space[k].cur;
|
||||
}
|
||||
|
||||
// 如果该元素不存在,则加入静态链表
|
||||
if(k == space[R].cur) {
|
||||
i = Malloc(space);
|
||||
space[i].data = b;
|
||||
space[i].cur = space[R].cur;
|
||||
space[R].cur = i;
|
||||
|
||||
// 如果该元素已存在,则需要移除
|
||||
} else {
|
||||
space[p].cur = space[k].cur;
|
||||
Free(space, k);
|
||||
if(R == k) {
|
||||
R = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*===============
|
||||
* 静态链表去重
|
||||
*
|
||||
* 包含算法: 2.17
|
||||
================*/
|
||||
|
||||
#ifndef DIFFERENCE_H
|
||||
#define DIFFERENCE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
#include "SLinkList.h" //**▲02 线性表**//
|
||||
|
||||
/*
|
||||
* ████████ 算法2.17 ████████
|
||||
*
|
||||
* S = (A-B)∪(B-A)
|
||||
*
|
||||
* 对集合A和集合B进行(A-B)∪(B-A)计算,计算结果存入静态链表S
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据。
|
||||
*/
|
||||
void difference(char path[], SLinkList space, int* S);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
/*=================================
|
||||
* 静态链表
|
||||
*
|
||||
* 包含算法: 2.13、2.14、2.15、2.16
|
||||
==================================*/
|
||||
|
||||
#include "SLinkList.h"
|
||||
|
||||
/*
|
||||
* ██ 提示 ██
|
||||
*
|
||||
* 以下所有函数的形参中:
|
||||
* space:指示备用空间
|
||||
* S :指示静态链表头结点索引
|
||||
*/
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 备用空间操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* ████████ 算法2.14 ████████
|
||||
*
|
||||
* 初始化备用空间
|
||||
*
|
||||
* 备用空间为静态链表提供可用的内存。
|
||||
*/
|
||||
void InitSpace(SLinkList space) {
|
||||
int i;
|
||||
|
||||
for(i = 0; i < MAXSIZE - 1; ++i) {
|
||||
space[i].cur = i + 1;
|
||||
}
|
||||
|
||||
space[MAXSIZE - 1].cur = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* ████████ 算法2.15 ████████
|
||||
*
|
||||
* 申请空间
|
||||
*
|
||||
* 为静态链表从备用空间申请结点空间,
|
||||
* 如果申请成功,返回可用空间的索引,
|
||||
* 申请失败时,返回0。
|
||||
*/
|
||||
int Malloc(SLinkList space) {
|
||||
int i = space[0].cur;
|
||||
|
||||
if(i != 0) {
|
||||
// 将申请到的空间从备用空间中删去
|
||||
space[0].cur = space[i].cur;
|
||||
}
|
||||
|
||||
// 返回新申请结点下标
|
||||
return i;
|
||||
}
|
||||
|
||||
/*
|
||||
* ████████ 算法2.16 ████████
|
||||
*
|
||||
* 回收空间
|
||||
*
|
||||
* 回收索引k处的结点空间以供静态链表后续复用,
|
||||
* 回收方式就是将该结点空间从静态链表上移除,
|
||||
* 并将其添加到备用空间的列表当中。
|
||||
*/
|
||||
void Free(SLinkList space, int k) {
|
||||
space[k].cur = space[0].cur;
|
||||
space[0].cur = k;
|
||||
}
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 静态链表操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问静态链表S
|
||||
*/
|
||||
void ListTraverse(SLinkList space, int S, void(Visit)(ElemType)) {
|
||||
int p;
|
||||
|
||||
// 确保链表存在
|
||||
if(S == 0 || space[S].cur == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
p = space[S].cur;
|
||||
|
||||
while(p != 0) {
|
||||
Visit(space[p].data);
|
||||
p = space[p].cur;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 图形化输出 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
// 以图形化形式输出当前结构,仅限内部测试使用
|
||||
void PrintGraph(SLinkList space, int S) {
|
||||
int i = 0;
|
||||
|
||||
printf("==== 备用空间 ====\n");
|
||||
while(i < 20) {
|
||||
printf("%2d | %2d | %2d |\n", i, space[i].data, space[i].cur);
|
||||
i = space[i].cur;
|
||||
}
|
||||
|
||||
printf("==== 静态链表 ====\n");
|
||||
i = S;
|
||||
while(i>0 && i < 20) {
|
||||
printf("%2d | %2d | %2d |\n", i, space[i].data, space[i].cur);
|
||||
i = space[i].cur;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*=================================
|
||||
* 静态链表
|
||||
*
|
||||
* 包含算法: 2.13、2.14、2.15、2.16
|
||||
==================================*/
|
||||
|
||||
#ifndef SLINKLIST_H
|
||||
#define SLINKLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> //提供malloc、realloc、free、exit原型
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/* 宏定义 */
|
||||
#define MAXSIZE 1000 // 备用空间最大容量,近似于静态链表的最大长度
|
||||
|
||||
/* 静态链表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 静态链表结构
|
||||
*
|
||||
* 注:静态链表依托于一个数组,该数组包含了已占用空间和空闲空间
|
||||
*/
|
||||
typedef struct SLinkLNode {
|
||||
ElemType data;
|
||||
int cur; // cur是游标,做指针用,用来链接下一个结点(区别于数组下标)
|
||||
} SLinkList[MAXSIZE]; // 链表空间类型
|
||||
|
||||
|
||||
/*
|
||||
* ████ 提示 ████
|
||||
*
|
||||
* 以下所有函数的形参中:
|
||||
* space:指示备用空间
|
||||
* S :指示静态链表头结点索引
|
||||
*/
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 备用空间操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* ████████ 算法2.14 ████████
|
||||
*
|
||||
* 初始化备用空间
|
||||
*
|
||||
* 备用空间为静态链表提供可用的内存。
|
||||
*/
|
||||
void InitSpace(SLinkList space);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.15 ████████
|
||||
*
|
||||
* 申请空间
|
||||
*
|
||||
* 为静态链表从备用空间申请结点空间,
|
||||
* 如果申请成功,返回可用空间的索引,
|
||||
* 申请失败时,返回0。
|
||||
*/
|
||||
int Malloc(SLinkList space);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.16 ████████
|
||||
*
|
||||
* 回收空间
|
||||
*
|
||||
* 回收索引k处的结点空间以供静态链表后续复用,
|
||||
* 回收方式就是将该结点空间从静态链表上移除,
|
||||
* 并将其添加到备用空间的列表当中。
|
||||
*/
|
||||
void Free(SLinkList space, int k);
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 静态链表操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问静态链表S
|
||||
*/
|
||||
void ListTraverse(SLinkList space, int S, void(Visit)(ElemType));
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 图形化输出 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
// 以图形化形式输出当前结构,仅限内部测试使用
|
||||
void PrintGraph(SLinkList space, int S);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,4 @@
|
||||
集合A的长度:3
|
||||
集合B的长度:5
|
||||
集合A中元素:2 3 4
|
||||
集合B中元素:1 3 4 6 7
|
||||
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{6816DB4B-DC29-4BF4-8D93-F6B00444FD37}</ProjectGuid>
|
||||
<RootNamespace>My0208_DuLinkList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DuLinkList-main.c" />
|
||||
<ClCompile Include="DuLinkList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="DuLinkList.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="DuLinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="DuLinkList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="DuLinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,149 @@
|
||||
#include <stdio.h>
|
||||
#include "Status.h"
|
||||
#include "DuLinkList.h" //**▲02 线性表**//
|
||||
|
||||
// 判断data>e是否成立
|
||||
Status CmpGreater(ElemType data, ElemType e) {
|
||||
return data > e ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
DuLinkList L;
|
||||
int i;
|
||||
ElemType e;
|
||||
|
||||
printf("████████ InitList \n");
|
||||
{
|
||||
printf("█ 初始化双向循环链表 L ...\n");
|
||||
InitList(&L);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListEmpty \n");
|
||||
{
|
||||
ListEmpty(L) ? printf("█ L 为空!!\n") : printf("█ L 不为空!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListInsert \n");
|
||||
{
|
||||
for(i = 1; i <= 8; i++) {
|
||||
printf("█ 在 L 第 %d 个位置插入 \"%d\" ...\n", i, 2 * i);
|
||||
ListInsert(L, i, 2 * i);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListTraverse \n");
|
||||
{
|
||||
printf("█ L 中的元素为:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListLength \n");
|
||||
{
|
||||
printf("█ L 的长度为 %d \n", ListLength(L));
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ListDelete \n");
|
||||
{
|
||||
printf("█ 删除前的元素:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
|
||||
printf("█ 尝试删除 L 中第 6 个元素...\n");
|
||||
|
||||
if(ListDelete(L, 6, &e) == OK) {
|
||||
printf("█ 删除成功,被删除元素是:\"%d\"\n", e);
|
||||
} else {
|
||||
printf("█ 删除失败,第 6 个元素不存在!\n");
|
||||
}
|
||||
|
||||
printf("█ 删除后的元素:L = ");
|
||||
ListTraverse(L, PrintElem);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ GetElem \n");
|
||||
{
|
||||
GetElem(L, 4, &e);
|
||||
printf("█ L 中第 4 个位置的元素为 \"%d\" \n", e);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ LocateElem \n");
|
||||
{
|
||||
i = LocateElem(L, 7, CmpGreater);
|
||||
GetElem(L, i, &e);
|
||||
printf("█ L 中第一个元素值大于 \"7\" 的元素是 \"%d\" \n", e);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ PriorElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(PriorElem(L, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的前驱为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的前驱不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ NextElem \n");
|
||||
{
|
||||
ElemType cur_e = 6;
|
||||
|
||||
if(NextElem(L, cur_e, &e) == OK) {
|
||||
printf("█ 元素 \"%d\" 的后继为 \"%d\" \n", cur_e, e);
|
||||
} else {
|
||||
printf("█ 元素 \"%d\" 的后继不存在!\n", cur_e);
|
||||
}
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ ClearList \n");
|
||||
{
|
||||
printf("█ 清空 L 前:");
|
||||
ListEmpty(L) ? printf(" L 为空!!\n") : printf(" L 不为空!\n");
|
||||
|
||||
ClearList(L);
|
||||
|
||||
printf("█ 清空 L 后:");
|
||||
ListEmpty(L) ? printf(" L 为空!!\n") : printf(" L 不为空!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ DestroyList \n");
|
||||
{
|
||||
printf("█ 销毁 L 前:");
|
||||
L ? printf(" L 存在!\n") : printf(" L 不存在!!\n");
|
||||
|
||||
DestroyList(&L);
|
||||
|
||||
printf("█ 销毁 L 后:");
|
||||
L ? printf(" L 存在!\n") : printf(" L 不存在!!\n");
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
return 0;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,153 @@
|
||||
/*=====================
|
||||
* 双向循环链表
|
||||
*
|
||||
* 包含算法: 2.18、2.19
|
||||
======================*/
|
||||
|
||||
#ifndef DULINKLIST_H
|
||||
#define DULINKLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> //提供malloc、realloc、free、exit原型
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/* 双向循环链表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 双向循环链表结构
|
||||
*
|
||||
* 注:这里的双向循环链表存在头结点
|
||||
*/
|
||||
typedef struct DuLNode {
|
||||
ElemType data;
|
||||
struct DuLNode* prior; // 前驱
|
||||
struct DuLNode* next; // 后继
|
||||
} DuLNode;
|
||||
|
||||
// 指向双向循环链表结点的指针
|
||||
typedef DuLNode* DuLinkList;
|
||||
|
||||
|
||||
/*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(DuLinkList* L);
|
||||
|
||||
/*
|
||||
* 销毁(结构)
|
||||
*
|
||||
* 释放双向循环链表所占内存。
|
||||
*/
|
||||
Status DestroyList(DuLinkList* L);
|
||||
|
||||
/*
|
||||
* 置空(内容)
|
||||
*
|
||||
* 这里需要释放双向循环链表中非头结点处的空间。
|
||||
*/
|
||||
Status ClearList(DuLinkList L);
|
||||
|
||||
/*
|
||||
* 判空
|
||||
*
|
||||
* 判断双向循环链表中是否包含有效数据。
|
||||
*
|
||||
* 返回值:
|
||||
* TRUE : 双向循环链表为空
|
||||
* FALSE: 双向循环链表不为空
|
||||
*/
|
||||
Status ListEmpty(DuLinkList L);
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回双向循环链表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(DuLinkList L);
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取双向循环链表中第i个元素,将其存储到e中。
|
||||
* 如果可以找到,返回OK,否则,返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数,但这不符合编码的通用约定。
|
||||
* 通常,i的含义应该指索引,即从0开始计数。
|
||||
*/
|
||||
Status GetElem(DuLinkList L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* 查找
|
||||
*
|
||||
* 返回双向循环链表中首个与e满足Compare关系的元素位序。
|
||||
* 如果不存在这样的元素,则返回0。
|
||||
*
|
||||
*【备注】
|
||||
* 元素e是Compare函数第二个形参
|
||||
*/
|
||||
int LocateElem(DuLinkList L, ElemType e, Status(Compare)(ElemType, ElemType));
|
||||
|
||||
/*
|
||||
* 前驱
|
||||
*
|
||||
* 获取元素cur_e的前驱,
|
||||
* 如果存在,将其存储到pre_e中,返回OK,
|
||||
* 如果不存在,则返回ERROR。
|
||||
*/
|
||||
Status PriorElem(DuLinkList L, ElemType cur_e, ElemType* pre_e);
|
||||
|
||||
/*
|
||||
* 后继
|
||||
*
|
||||
* 获取元素cur_e的后继,
|
||||
* 如果存在,将其存储到next_e中,返回OK,
|
||||
* 如果不存在,则返回ERROR。
|
||||
*/
|
||||
Status NextElem(DuLinkList L, ElemType cur_e, ElemType* next_e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.18 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向双向循环链表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListInsert(DuLinkList L, int i, ElemType e);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.19 ████████
|
||||
*
|
||||
* 删除
|
||||
*
|
||||
* 删除双向循环链表第i个位置上的元素,并将被删除元素存储到e中。
|
||||
* 删除成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
*/
|
||||
Status ListDelete(DuLinkList L, int i, ElemType* e);
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问双向循环链表L
|
||||
*/
|
||||
void ListTraverse(DuLinkList L, void(Visit)(ElemType));
|
||||
|
||||
/*
|
||||
* 获取循环链表L上第i个元素的引用
|
||||
*
|
||||
* ▓▓▓▓ 注意 ▓▓▓▓
|
||||
* 1.加static的含义是当前函数只在DuLinkList中使用,不会被别的文件引用
|
||||
* 2.假设链表长度为len,且需要获取第len+1个元素的引用时,由于这里是循环链表,所以返回的是头结点
|
||||
*/
|
||||
static DuLinkList GetElemP(DuLinkList L, int i);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{2FE48840-F7BB-4DC7-B3D9-4AFF2B35206B}</ProjectGuid>
|
||||
<RootNamespace>My0209_ELinkList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="ELinkList-main.c" />
|
||||
<ClCompile Include="ELinkList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ELinkList.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="ELinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ELinkList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ELinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{58E663CD-DDC0-48B4-BE73-5AA11CEED026}</ProjectGuid>
|
||||
<RootNamespace>My0210_MergeEList</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="ELinkList.c" />
|
||||
<ClCompile Include="MergeEList-main.c" />
|
||||
<ClCompile Include="MergeEList.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ELinkList.h" />
|
||||
<ClInclude Include="MergeEList.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_La.txt" />
|
||||
<None Include="TestData_Lb.txt" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="ELinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="MergeEList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="MergeEList-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ELinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="MergeEList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_La.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
<None Include="TestData_Lb.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,155 @@
|
||||
/*=======================
|
||||
* 扩展的单链表(线性链表)
|
||||
*
|
||||
* 包含算法: 2.20
|
||||
========================*/
|
||||
|
||||
#ifndef ELINKLIST_H
|
||||
#define ELINKLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // 提供malloc、realloc、free、exit原型
|
||||
#include <limits.h> // 提供一些极限常量
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/*
|
||||
* ████ 注意 ████
|
||||
*
|
||||
* 教材中的线性链表命名为LinkList,
|
||||
* 这里为了与单链表区分,故将其命名为ELinkList。
|
||||
* 线性链表可以理解成对普通链表的一种扩展。
|
||||
*/
|
||||
|
||||
/* 线性链表元素类型定义 */
|
||||
typedef int ElemType;
|
||||
|
||||
/*
|
||||
* 线性链表结构
|
||||
*
|
||||
* 注:这里的线性链表存在头结点
|
||||
*/
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
} LNode, * Link, * Position;
|
||||
|
||||
/* 维护线性链表头尾指针及长度信息 */
|
||||
typedef struct {
|
||||
Link head, tail; // 分别指向线性链表中的头结点和尾结点
|
||||
int len; // 指示线性链表中数据元素的个数
|
||||
} ELinkList;
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 内存操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 内存分配
|
||||
*
|
||||
* 为线性链表申请一个结点,并存入指定的数据e。
|
||||
*
|
||||
*【备注】
|
||||
* static修饰的含义是该函数仅限当前文件内使用
|
||||
*/
|
||||
Status MakeNode(Link* p, ElemType e);
|
||||
|
||||
/*
|
||||
* 内存回收
|
||||
*
|
||||
* 释放线性链表中指定的结点。
|
||||
*
|
||||
*【备注】
|
||||
* static修饰的含义是该函数仅限当前文件内使用
|
||||
*/
|
||||
void FreeNode(Link* p);
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 链表常规操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(ELinkList* L);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.20 ████████
|
||||
*
|
||||
* 插入
|
||||
*
|
||||
* 向链表第i个位置上插入e,插入成功则返回OK,否则返回ERROR。
|
||||
*
|
||||
*【备注】
|
||||
* 教材中i的含义是元素位置,从1开始计数
|
||||
* 可以看做是算法2.9的改写
|
||||
*/
|
||||
Status ListInsert(ELinkList* L, int i, ElemType e);
|
||||
|
||||
/*
|
||||
* 遍历
|
||||
*
|
||||
* 用visit函数访问链表L
|
||||
*/
|
||||
void ListTraverse(ELinkList L, void(Visit)(ElemType));
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 链表扩展操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取结点p的元素值。
|
||||
*/
|
||||
ElemType GetCurElem(Link p);
|
||||
|
||||
/*
|
||||
* 头结点
|
||||
*
|
||||
* 获取头结点引用。
|
||||
*/
|
||||
Position GetHead(ELinkList L);
|
||||
|
||||
/*
|
||||
* 后继
|
||||
*
|
||||
* 获取结点p的后继,如果不存在,则返NULL。
|
||||
*/
|
||||
Position NextPos(ELinkList L, Link p);
|
||||
|
||||
/*
|
||||
* 查找
|
||||
*
|
||||
* 查找链表L中第i(允许为0)个结点,并将其引用存入p,且返回OK
|
||||
* 如果i值不合规,则返回ERROR
|
||||
* 特别注意,当i为0时,p存储的是头结点的引用
|
||||
*/
|
||||
Status LocatePos(ELinkList L, int i, Link* p);
|
||||
|
||||
/*
|
||||
* 插入
|
||||
*
|
||||
* 将s结点插入到h结点后面,成为h后面的第一个结点
|
||||
*
|
||||
*【备注】
|
||||
* 教材中对于该方法的描述有些问题,这里是修正过的版本
|
||||
*/
|
||||
Status InsFirst(ELinkList* L, Link h, Link s);
|
||||
|
||||
/*
|
||||
* 删除
|
||||
*
|
||||
* 删除h结点后的第一个结点,并用q存储被删除结点的引用
|
||||
*
|
||||
*【备注】
|
||||
* 教材中对于该方法的定义略显粗糙,这里是修正过的版本
|
||||
*/
|
||||
Status DelFirst(ELinkList* L, Link h, Link* q);
|
||||
|
||||
/*
|
||||
* 向尾部添加
|
||||
*
|
||||
* 将s所指的一串结点链接在链表L后面
|
||||
*/
|
||||
Status Append(ELinkList* L, Link s);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,71 @@
|
||||
#include <stdio.h>
|
||||
#include "MergeEList.h" //**▲02 线性表**//
|
||||
|
||||
// 测试函数,打印元素
|
||||
void PrintElem(ElemType e) {
|
||||
printf("%d ", e);
|
||||
}
|
||||
|
||||
/*
|
||||
* 比较
|
||||
*
|
||||
* 比较e1和e2的大小,返回值含义如下:
|
||||
* < 0:e1 < e2
|
||||
* = 0:e1 = e2
|
||||
* > 0:e1 > e2
|
||||
*/
|
||||
int Cmp(ElemType e1, ElemType e2) {
|
||||
return e1 - e2;
|
||||
}
|
||||
|
||||
/*
|
||||
* 尾插法创建链表
|
||||
*
|
||||
*【备注】
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件fp中读取测试数据。
|
||||
*/
|
||||
void CreateList_Tail(char path[], ELinkList* L) {
|
||||
FILE* fp;
|
||||
int i, n;
|
||||
ElemType e;
|
||||
|
||||
fp = fopen(path, "r");
|
||||
if(fp == NULL) {
|
||||
printf("文件打开失败...\n");
|
||||
exit(ERROR);
|
||||
}
|
||||
|
||||
// 读取指定集合中元素的数量
|
||||
ReadData(fp, "%d", &n);
|
||||
|
||||
InitList(L);
|
||||
|
||||
for(i = 1; i <= n; i++) {
|
||||
ReadData(fp, "%d", &e);
|
||||
ListInsert(L, i, e);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
ELinkList La, Lb, Lc;
|
||||
|
||||
printf("创建La和Lb以待测试...\n");
|
||||
|
||||
CreateList_Tail("TestData_La.txt", &La);
|
||||
printf("La= ");
|
||||
ListTraverse(La, PrintElem);
|
||||
|
||||
CreateList_Tail("TestData_Lb.txt", &Lb);
|
||||
printf("Lb= ");
|
||||
ListTraverse(Lb, PrintElem);
|
||||
|
||||
MergeEList(&La, &Lb, &Lc, Cmp);
|
||||
printf("合并La和Lb为 Lc = ");
|
||||
ListTraverse(Lc, PrintElem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*===============
|
||||
* 归并非降序链表
|
||||
*
|
||||
* 包含算法: 2.21
|
||||
================*/
|
||||
|
||||
#include "MergeEList.h"
|
||||
|
||||
Status MergeEList(ELinkList* La, ELinkList* Lb, ELinkList* Lc, int(Compare)(ElemType, ElemType)){
|
||||
Link ha, hb, pa, pb, q;
|
||||
ElemType a, b;
|
||||
|
||||
if(InitList(Lc) == ERROR) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
ha = GetHead(*La); // 指向La头结点
|
||||
hb = GetHead(*Lb); // 指向Lb头结点
|
||||
|
||||
// 保证La和Lb是有效的链表,不过有可能是空表
|
||||
if(ha == NULL || hb ==NULL) {
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
pa = NextPos(*La, ha); // 指向La第1个元素
|
||||
pb = NextPos(*La, hb); // 指向Lb第1个元素
|
||||
|
||||
// 遍历La和Lb,选出较小的元素,依次加入到Lc中
|
||||
while(pa !=NULL && pb!=NULL){
|
||||
a = GetCurElem(pa);
|
||||
b = GetCurElem(pb);
|
||||
|
||||
// 如果La<=Lb
|
||||
if(Compare(a, b)<=0) {
|
||||
// 摘下La中首个元素
|
||||
DelFirst(La, ha, &q);
|
||||
|
||||
// 将上述摘下的元素追加到Lc中
|
||||
Append(Lc, q);
|
||||
|
||||
// 依然是获取La中第1个元素
|
||||
pa = NextPos(*La, ha);
|
||||
} else {
|
||||
// 摘下Lb中首个元素
|
||||
DelFirst(Lb, hb, &q);
|
||||
|
||||
// 将上述摘下的元素追加到Lc中
|
||||
Append(Lc, q);
|
||||
|
||||
// 依然是获取La中第1个元素
|
||||
pb = NextPos(*Lb, hb);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果La还有剩余
|
||||
if(pa!=NULL) {
|
||||
Append(Lc, pa);
|
||||
} else {
|
||||
Append(Lc, pb);
|
||||
}
|
||||
|
||||
// 释放La的头结点。教材中这里是对ha操作,但是那样无法置空La的头指针,因而这里做了修改
|
||||
FreeNode(&(La->head));
|
||||
|
||||
// 释放Lb的头结点。教材中这里是对hb操作,但是那样无法置空Lb的头指针,因而这里做了修改
|
||||
FreeNode(&(Lb->head));
|
||||
|
||||
La->tail = NULL;
|
||||
Lb->tail = NULL;
|
||||
|
||||
La->len = 0;
|
||||
Lb->len = 0;
|
||||
|
||||
return OK;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*===============
|
||||
* 归并非降序链表
|
||||
*
|
||||
* 包含算法: 2.21
|
||||
================*/
|
||||
|
||||
#ifndef MERGEELIST_H
|
||||
#define MERGEELIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
#include "ELinkList.h" //**▲02 线性表**//
|
||||
|
||||
/*
|
||||
* ████ 注意 ████
|
||||
*
|
||||
* 为了与之前的归并算法名称区分,这里将算法名称命名为MergeEList(教材中的名称是MergeList_L)
|
||||
*/
|
||||
|
||||
/*
|
||||
* ████████ 算法2.21 ████████
|
||||
*
|
||||
* 非递减链表归并:C=A+B
|
||||
*
|
||||
* 将链表A和B归并为C,且保持元素相对位置不变。
|
||||
* 归并结束后,会销毁La和Lb。
|
||||
*/
|
||||
Status MergeEList(ELinkList* La, ELinkList* Lb, ELinkList* Lc, int(Compare)(ElemType, ElemType));
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
||||
┃(01)算法2.21:La与Lb均非递减排列,将其归并为一个非递减排列的单链表Lc。┃
|
||||
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,2 @@
|
||||
集合A的长度:6
|
||||
La = 1 2 3 5 7 8
|
||||
@@ -0,0 +1,2 @@
|
||||
集合B的长度:7
|
||||
Lb = 2 4 5 8 10 11 13
|
||||
@@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" 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>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{BA5E9196-08EF-4F25-9F82-D35296370F52}</ProjectGuid>
|
||||
<RootNamespace>My0211_Polynomial</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</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>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>$(SolutionDir)\..\Status;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>$(SolutionDir)\..\Status\Status.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="ELinkList.c" />
|
||||
<ClCompile Include="Polynomial-main.c" />
|
||||
<ClCompile Include="Polynomial.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ELinkList.h" />
|
||||
<ClInclude Include="Polynomial.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_Pa.txt" />
|
||||
<None Include="TestData_Pb.txt" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;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="ELinkList.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Polynomial.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Polynomial-main.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ELinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Polynomial.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="TestData_Pa.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
<None Include="TestData_Pb.txt">
|
||||
<Filter>资源文件</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,175 @@
|
||||
/*=======================
|
||||
* 扩展的单链表(线性链表)
|
||||
*
|
||||
* 包含算法: 2.20
|
||||
========================*/
|
||||
|
||||
#ifndef ELINKLIST_H
|
||||
#define ELINKLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // 提供malloc、realloc、free、exit原型
|
||||
#include <limits.h> // 提供一些极限常量
|
||||
#include "Status.h" //**▲01 绪论**//
|
||||
|
||||
/*
|
||||
* ████ 注意 ████
|
||||
*
|
||||
* 教材中的线性链表命名为LinkList,
|
||||
* 这里为了与单链表区分,故将其命名为ELinkList。
|
||||
* 线性链表可以理解成对普通链表的一种扩展。
|
||||
*/
|
||||
|
||||
/*
|
||||
* 线性链表元素类型定义
|
||||
* 这里用在多项式中,用到了结构体类型的元素
|
||||
*/
|
||||
typedef struct {
|
||||
float coef; // 多项式系数
|
||||
int expn; // 多项式指数
|
||||
} ElemType; // 重新定义扩展的线性表中元素类型
|
||||
|
||||
/*
|
||||
* 线性链表结构
|
||||
*
|
||||
* 注:这里的线性链表存在头结点
|
||||
*/
|
||||
typedef struct LNode {
|
||||
ElemType data;
|
||||
struct LNode* next;
|
||||
} LNode, * Link, * Position;
|
||||
|
||||
/* 维护线性链表头尾指针及长度信息 */
|
||||
typedef struct {
|
||||
Link head, tail; // 分别指向线性链表中的头结点和尾结点
|
||||
int len; // 指示线性链表中数据元素的个数
|
||||
} ELinkList;
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 内存操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 内存分配
|
||||
*
|
||||
* 为线性链表申请一个结点,并存入指定的数据e。
|
||||
*
|
||||
*【备注】
|
||||
* static修饰的含义是该函数仅限当前文件内使用
|
||||
*/
|
||||
Status MakeNode(Link* p, ElemType e);
|
||||
|
||||
/*
|
||||
* 内存回收
|
||||
*
|
||||
* 释放线性链表中指定的结点。
|
||||
*
|
||||
*【备注】
|
||||
* static修饰的含义是该函数仅限当前文件内使用
|
||||
*/
|
||||
void FreeNode(Link* p);
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 链表常规操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 初始化
|
||||
*
|
||||
* 初始化成功则返回OK,否则返回ERROR。
|
||||
*/
|
||||
Status InitList(ELinkList* L);
|
||||
|
||||
/*
|
||||
* 销毁(结构)
|
||||
*
|
||||
* 释放链表所占内存。
|
||||
*/
|
||||
Status DestroyList(ELinkList* L);
|
||||
|
||||
/*
|
||||
* 置空(内容)
|
||||
*
|
||||
* 这里需要释放链表中非头结点处的空间。
|
||||
*/
|
||||
Status ClearList(ELinkList* L);
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回链表包含的有效元素的数量。
|
||||
*/
|
||||
int ListLength(ELinkList L);
|
||||
|
||||
/*
|
||||
* 查找
|
||||
*
|
||||
* 这个查找函数与传统的版本不一样。
|
||||
* 这里首先会查找与元素e满足Compare()==0的元素,如果找到,使用q存储其引用,并返回True。
|
||||
* 如果不存在上述引用,则查找首个与元素e满足Compare()>0的元素的前驱,并返回FALSE。
|
||||
* 如果链表中的元素均大于e,q存储头结点引用。
|
||||
* 如果链表中的元素均小于e,q存储尾结点引用。
|
||||
* 其它情形下,会将q置空。
|
||||
*
|
||||
*【备注】
|
||||
* 1.元素e是Compare函数第二个形参
|
||||
*/
|
||||
Status LocateElem(ELinkList L, ElemType e, Position* q, Status(Compare)(ElemType, ElemType));
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 链表扩展操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 取值
|
||||
*
|
||||
* 获取结点p的元素值。
|
||||
*/
|
||||
ElemType GetCurElem(Link p);
|
||||
|
||||
/*
|
||||
* 设值
|
||||
*
|
||||
* 为结点p设置元素值。
|
||||
*/
|
||||
Status SetCurElem(Link p, ElemType e);
|
||||
|
||||
/*
|
||||
* 头结点
|
||||
*
|
||||
* 获取头结点引用。
|
||||
*/
|
||||
Position GetHead(ELinkList L);
|
||||
|
||||
/*
|
||||
* 后继
|
||||
*
|
||||
* 获取结点p的后继,如果不存在,则返NULL。
|
||||
*/
|
||||
Position NextPos(ELinkList L, Link p);
|
||||
|
||||
/*
|
||||
* 插入
|
||||
*
|
||||
* 将s结点插入到h结点后面,成为h后面的第一个结点
|
||||
*
|
||||
*【备注】
|
||||
* 教材中对于该方法的描述有些问题,这里是修正过的版本
|
||||
*/
|
||||
Status InsFirst(ELinkList* L, Link h, Link s);
|
||||
|
||||
/*
|
||||
* 删除
|
||||
*
|
||||
* 删除h结点后的第一个结点,并用q存储被删除结点的引用
|
||||
*
|
||||
*【备注】
|
||||
* 教材中对于该方法的定义略显粗糙,这里是修正过的版本
|
||||
*/
|
||||
Status DelFirst(ELinkList* L, Link h, Link* q);
|
||||
|
||||
/*
|
||||
* 向尾部添加
|
||||
*
|
||||
* 将s所指的一串结点链接在链表L后面
|
||||
*/
|
||||
Status Append(ELinkList* L, Link s);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "Polynomial.h" //**▲02 线性表**//
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Polynomial Pa, Pb;
|
||||
int m, n;
|
||||
|
||||
m = 5;
|
||||
n = 6;
|
||||
|
||||
printf("████████ CreatPolyn \n");
|
||||
{
|
||||
printf("█ 作为示范,创建项数为 %d 的多项式Pa...\n", m);
|
||||
CreatPolyn("TestData_Pa.txt", &Pa, m);
|
||||
|
||||
printf("█ 作为示范,创建项数为 %d 的多项式Pb...\n", n);
|
||||
CreatPolyn("TestData_Pb.txt", &Pb, n);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ PrintPolyn \n");
|
||||
{
|
||||
printf("█ 一元多项式 Pa = ");
|
||||
PrintPolyn(Pa);
|
||||
|
||||
printf("█ 一元多项式 Pb = ");
|
||||
PrintPolyn(Pb);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ PolynLength \n");
|
||||
{
|
||||
printf("█ La 的项数为 %d ,Lb 的项数为 %d\n", PolynLength(Pa), PolynLength(Pb));
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ AddPolyn \n");
|
||||
{
|
||||
CreatPolyn("TestData_Pa.txt", &Pa, m);
|
||||
CreatPolyn("TestData_Pb.txt", &Pb, n);
|
||||
|
||||
AddPolyn(&Pa, &Pb);
|
||||
|
||||
printf("█ Pa = Pa + Pb = ");
|
||||
PrintPolyn(Pa);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ SubtractPolyn \n");
|
||||
{
|
||||
CreatPolyn("TestData_Pa.txt", &Pa, m);
|
||||
CreatPolyn("TestData_Pb.txt", &Pb, n);
|
||||
|
||||
SubtractPolyn(&Pa, &Pb);
|
||||
|
||||
printf("█ Pa = Pa - Pb = ");
|
||||
PrintPolyn(Pa);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
|
||||
printf("████████ MultiplyPolyn \n");
|
||||
{
|
||||
CreatPolyn("TestData_Pa.txt", &Pa, m);
|
||||
CreatPolyn("TestData_Pb.txt", &Pb, n);
|
||||
|
||||
MultiplyPolyn(&Pa, &Pb);
|
||||
|
||||
printf("█ Pa = Pa * Pb = ");
|
||||
PrintPolyn(Pa);
|
||||
}
|
||||
PressEnterToContinue();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,91 @@
|
||||
/*========================
|
||||
* 一元多项式(基于线性链表)
|
||||
*
|
||||
* 包含算法: 2.22、2.23
|
||||
=========================*/
|
||||
|
||||
#ifndef POLYNOMIAL_H
|
||||
#define POLYNOMIAL_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> //提供malloc、realloc、free、exit原型
|
||||
#include "ELinkList.h" //**▲02 线性表**//
|
||||
|
||||
/* 一元多项式类型定义 */
|
||||
typedef ELinkList Polynomial;
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 一元多项式操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* ████████ 算法2.22 ████████
|
||||
*
|
||||
* 创建
|
||||
*
|
||||
* 根据输入的系数和指数,创建项数为m的一元多项式
|
||||
*
|
||||
*【备注】
|
||||
* 教材中默认从控制台读取数据。
|
||||
* 这里为了方便测试,避免每次运行都手动输入数据,
|
||||
* 因而选择了从预设的文件path中读取测试数据
|
||||
*/
|
||||
void CreatPolyn(char path[], Polynomial *P, int m);
|
||||
|
||||
/*
|
||||
* 销毁
|
||||
*
|
||||
* 销毁一元多项式。
|
||||
*/
|
||||
void DestroyPolyn(Polynomial *P);
|
||||
|
||||
/*
|
||||
* 计数
|
||||
*
|
||||
* 返回一元多项式的项数。
|
||||
*/
|
||||
int PolynLength(Polynomial P);
|
||||
|
||||
/*
|
||||
* ████████ 算法2.23 ████████
|
||||
*
|
||||
* 加法
|
||||
*
|
||||
* 一元多项式加法Pa=Pa+Pb,
|
||||
* 计算完成后,计算结果存到Pa中,并将Pb销毁。
|
||||
*/
|
||||
void AddPolyn(Polynomial *Pa, Polynomial *Pb);
|
||||
|
||||
/*
|
||||
* 减法
|
||||
*
|
||||
* 一元多项式减法Pa=Pa-Pb,
|
||||
* 计算完成后,计算结果存到Pa中,并将Pb销毁。
|
||||
*/
|
||||
void SubtractPolyn(Polynomial *Pa, Polynomial *Pb);
|
||||
|
||||
/*
|
||||
* 乘法
|
||||
*
|
||||
* 一元多项式乘法Pa=Pa*Pb,
|
||||
* 计算完成后,计算结果存到Pa中,并将Pb销毁。
|
||||
*/
|
||||
void MultiplyPolyn(Polynomial *Pa, Polynomial *Pb);
|
||||
|
||||
|
||||
/*━━━━━━━━━━━━━━━━━━━━━━ 基础操作 ━━━━━━━━━━━━━━━━━━━━━━*/
|
||||
|
||||
/*
|
||||
* 输出
|
||||
*
|
||||
* 打印输出一元多项式。
|
||||
*/
|
||||
void PrintPolyn(Polynomial P);
|
||||
|
||||
/*
|
||||
* 比较
|
||||
*
|
||||
* 比较c1项和c2项指数大小。
|
||||
*/
|
||||
int Cmp(ElemType c1, ElemType c2);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1 @@
|
||||
Pa = (2.1 , 1) (5.3 , 7) (4.0 , 3) (2.5 , 9) (-3.12 , 5)
|
||||
@@ -0,0 +1 @@
|
||||
Pb = (0.5 , 12) (-3.17 , 4) (3.4 , 2) (-2.5 , 9) (4.1 , 5) (1.8 , 8)
|
||||
Binary file not shown.
@@ -0,0 +1,80 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 11.00
|
||||
# Visual C++ Express 2010
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0201_SqList", "0201_SqList\0201_SqList.vcxproj", "{4DDF7C1A-9DF8-4983-B478-1B8928B8A239}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0202_Union", "0202_Union\0202_Union.vcxproj", "{5C53EFFE-EE74-42B8-875C-CFB7B2659917}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0203_MergeSqList", "0203_MergeSqList\0203_MergeSqList.vcxproj", "{D3309A60-3384-4EB0-9392-BEB5BF9934BF}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0204_LinkList", "0204_LinkList\0204_LinkList.vcxproj", "{8818D7C5-70C7-4649-9690-ACB30147C37B}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0205_MergeList", "0205_MergeList\0205_MergeList.vcxproj", "{DC3475CD-A081-423F-BCE5-14464B66F475}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0206_SLinkList", "0206_SLinkList\0206_SLinkList.vcxproj", "{C0C969A9-926E-4AE3-87AC-F73CB04D0BC7}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0207_Difference", "0207_Difference\0207_Difference.vcxproj", "{EB17D84C-751A-4261-BE9F-B02951BF8E51}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0208_DuLinkList", "0208_DuLinkList\0208_DuLinkList.vcxproj", "{6816DB4B-DC29-4BF4-8D93-F6B00444FD37}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0209_ELinkList", "0209_ELinkList\0209_ELinkList.vcxproj", "{2FE48840-F7BB-4DC7-B3D9-4AFF2B35206B}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0210_MergeEList", "0210_MergeEList\0210_MergeEList.vcxproj", "{58E663CD-DDC0-48B4-BE73-5AA11CEED026}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "0211_Polynomial", "0211_Polynomial\0211_Polynomial.vcxproj", "{BA5E9196-08EF-4F25-9F82-D35296370F52}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Release|Win32 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{4DDF7C1A-9DF8-4983-B478-1B8928B8A239}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{4DDF7C1A-9DF8-4983-B478-1B8928B8A239}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{4DDF7C1A-9DF8-4983-B478-1B8928B8A239}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{4DDF7C1A-9DF8-4983-B478-1B8928B8A239}.Release|Win32.Build.0 = Release|Win32
|
||||
{5C53EFFE-EE74-42B8-875C-CFB7B2659917}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{5C53EFFE-EE74-42B8-875C-CFB7B2659917}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{5C53EFFE-EE74-42B8-875C-CFB7B2659917}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{5C53EFFE-EE74-42B8-875C-CFB7B2659917}.Release|Win32.Build.0 = Release|Win32
|
||||
{D3309A60-3384-4EB0-9392-BEB5BF9934BF}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{D3309A60-3384-4EB0-9392-BEB5BF9934BF}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{D3309A60-3384-4EB0-9392-BEB5BF9934BF}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{D3309A60-3384-4EB0-9392-BEB5BF9934BF}.Release|Win32.Build.0 = Release|Win32
|
||||
{8818D7C5-70C7-4649-9690-ACB30147C37B}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{8818D7C5-70C7-4649-9690-ACB30147C37B}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{8818D7C5-70C7-4649-9690-ACB30147C37B}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{8818D7C5-70C7-4649-9690-ACB30147C37B}.Release|Win32.Build.0 = Release|Win32
|
||||
{DC3475CD-A081-423F-BCE5-14464B66F475}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{DC3475CD-A081-423F-BCE5-14464B66F475}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{DC3475CD-A081-423F-BCE5-14464B66F475}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{DC3475CD-A081-423F-BCE5-14464B66F475}.Release|Win32.Build.0 = Release|Win32
|
||||
{C0C969A9-926E-4AE3-87AC-F73CB04D0BC7}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{C0C969A9-926E-4AE3-87AC-F73CB04D0BC7}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{C0C969A9-926E-4AE3-87AC-F73CB04D0BC7}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{C0C969A9-926E-4AE3-87AC-F73CB04D0BC7}.Release|Win32.Build.0 = Release|Win32
|
||||
{EB17D84C-751A-4261-BE9F-B02951BF8E51}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{EB17D84C-751A-4261-BE9F-B02951BF8E51}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{EB17D84C-751A-4261-BE9F-B02951BF8E51}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{EB17D84C-751A-4261-BE9F-B02951BF8E51}.Release|Win32.Build.0 = Release|Win32
|
||||
{6816DB4B-DC29-4BF4-8D93-F6B00444FD37}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{6816DB4B-DC29-4BF4-8D93-F6B00444FD37}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{6816DB4B-DC29-4BF4-8D93-F6B00444FD37}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{6816DB4B-DC29-4BF4-8D93-F6B00444FD37}.Release|Win32.Build.0 = Release|Win32
|
||||
{2FE48840-F7BB-4DC7-B3D9-4AFF2B35206B}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{2FE48840-F7BB-4DC7-B3D9-4AFF2B35206B}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{2FE48840-F7BB-4DC7-B3D9-4AFF2B35206B}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{2FE48840-F7BB-4DC7-B3D9-4AFF2B35206B}.Release|Win32.Build.0 = Release|Win32
|
||||
{58E663CD-DDC0-48B4-BE73-5AA11CEED026}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{58E663CD-DDC0-48B4-BE73-5AA11CEED026}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{58E663CD-DDC0-48B4-BE73-5AA11CEED026}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{58E663CD-DDC0-48B4-BE73-5AA11CEED026}.Release|Win32.Build.0 = Release|Win32
|
||||
{BA5E9196-08EF-4F25-9F82-D35296370F52}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{BA5E9196-08EF-4F25-9F82-D35296370F52}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{BA5E9196-08EF-4F25-9F82-D35296370F52}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{BA5E9196-08EF-4F25-9F82-D35296370F52}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Binary file not shown.
Reference in New Issue
Block a user