idk why these stuffs get stashed for so long and I didn't ever commit them

This commit is contained in:
2025-11-06 09:43:54 +08:00
parent 5dbdfef1a1
commit e01c041259
232 changed files with 22806 additions and 1256 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,41 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34701.34
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sequential_List", "Sequential_List\Sequential_List.vcxproj", "{B4E4A6F7-1B97-421C-98BB-79482B2743FE}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Linked_List", "Linked_List\Linked_List.vcxproj", "{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x64.ActiveCfg = Debug|x64
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x64.Build.0 = Debug|x64
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x86.ActiveCfg = Debug|Win32
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x86.Build.0 = Debug|Win32
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x64.ActiveCfg = Release|x64
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x64.Build.0 = Release|x64
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x86.ActiveCfg = Release|Win32
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x86.Build.0 = Release|Win32
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x64.ActiveCfg = Debug|x64
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x64.Build.0 = Debug|x64
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x86.ActiveCfg = Debug|Win32
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x86.Build.0 = Debug|Win32
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x64.ActiveCfg = Release|x64
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x64.Build.0 = Release|x64
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x86.ActiveCfg = Release|Win32
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {413D1D02-E71E-45AC-8680-BA6E93C42BA8}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,253 @@
template <class DT>
struct LNode //链表结点
{
DT data; //数据域,存储数据元素值
LNode* next; //指针域,指向下一个结点
};
//算法2.1
template <class DT>
bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱
{
int k;
k = LocateElem_e(L, e); // 1.获取e的位序k
if (k > 1) // 2.位序k大于1
{
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
return true;
}
else // 3.元素e无前驱
return false; // 返回false
}
//【算法2.14】 创建空单链表
template <class DT>
bool InitList(LNode<DT>*& L)
{
L = new LNode<DT>; // 1.创建头结点
if (!L) exit(1); // 2.创建失败,退出
L->next = NULL; // 3.创建成功
return true; // 返回true
}
//【算法2.15】 尾插法创建n的元素
template <class DT>
bool CreateList_1(LNode<DT>*& L, int n)
{
int i;
LNode<DT>* p, * s;
p = L; //1.工作指针初始化,指向尾结点
cout << "依次输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点
{
s = new LNode<DT>; // 2.1 新建一个结点s
if (!s) // 2.2 创建失败返回false
return false;
cin >> s->data; // 2.3 输入结点值
s->next = p->next; // 2.4 s 链在表尾
p->next = s;
p = s; // 2.5 工作指针指向 s
}
return true; // 3.创建成功返回true
}
//【算法2.16】 头插法创建n个元素
template <class DT>
bool CreateList_2(LNode<DT>* (&L), int n)
{
int i;
LNode<DT>* s;
cout << "逆序输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
{
s = new LNode<DT>; // 1.1 新建一个结点 s
if (!s) // 1.2 创建失败返回false
return false;
cin >> s->data; // 1.3 输入结点值
s->next = L->next; // 1.4 s 在头结点后
L->next = s;
}
return true; // 1.创建成功返回true
}
//【算法2.17】
template <class DT>
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
{
LNode<DT>* p;
while (L) // 1. 表非空,从头结点开始,依次释放结点
{
p = L; // 1.1 处理表头结点
L = L->next; // 1.2 头指针后移
delete p; // 1.3 释放表头结点所占内存
}
L = NULL; // 2.头指针指向空
}
//【算法2.18】 获取第i个元素
template<class DT>
bool GetElem_i(LNode<DT>* L, int i, DT& e)
{
LNode<DT>* p; // 1.初始化
p = L->next; // 1.1 设置工作指针,从首结点开始数结点
int j = 1; // 1.2 计数器初始化
while (p && j < i) // 2.定位到第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3 未找到返回false
return false;
else // 4. 找到
{
e = p->data; // 获取第i个元素值
return true; // 返回true
}
}
//【算法2.19】 查找值为e的元素位序
template<class DT>
int LocateElem_e(LNode<DT>* L, DT e)
{
LNode<DT>* p; // 1.初始化从首元开始查找
p = L->next; // 1.1从首元开始查找
int j = 1; // 1.2 计数器初值
while (p && p->data != e) // 2.顺序查找
{
p = p->next; // 2.1未找到指针后移
j++; // 2.2 计数器增1
}
if (p == NULL) // 3. 判断是否找到
return 0; // 3.1末找到返回0
else
return j; // 3.2 找到,返回位序
}
//【算法2.20】 插入第i个元素
template<class DT>
bool InsertElem_i(LNode<DT>*& L, int i, DT e)
{
int j = 0;
LNode<DT>* p; // 1.初始化
p = L; // 工作指针初始化
while (p && j < i - 1) // 2. 定位到插入点前驱
{
p = p->next;
j++;
}
if (!p || j > i - 1) // 3.判断定位是否成功:
return false; // 3.1 定位失败,不能插入
else // 3.2 定位成功
{
LNode<DT>* s;
s = new LNode<DT>; // 3.2.1建立新结点
s->data = e; // 3.2.2新结点赋值
s->next = p->next; // 3.2.3结点S链接到p结点之后
p->next = s;
return true; // 3.2.4 插入成功返回true
}
}
//【算法2.21】 删除第i个元素
template<class DT>
bool DeleElem_i(LNode<DT>* (&L), int i)
{
LNode<DT>* p, * q; //1.初始化:设置工作指针
p = L; //查找从头结点开始
int j = 0; //计数器初始化
while (p->next && j < i - 1) //2.p定位到删除点的前驱
{
p = p->next;
j++;
}
if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
return false; //返回false
else //4.删除操作
{
q = p->next; //4.1暂存删除结点位置
p->next = q->next; //4.2从链表中摘除删除结点
delete q;
return true; //4.3删除成功返回true
}
}
//【算法2.22】 修改第i个元素值
template<class DT>
bool PutElem_i(LNode<DT>* (&L), int i, DT e)
{
LNode<DT>* p; // 1.初始化:设置工作指针
p = L->next; // 从首结点开始,数结点
int j = 1; // 计数器初始化
while (p && j < i) // 2.查找第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3.元素不存在,返回false
return false;
else // 4.定位成功
{
p->data = e; // 修改元素值
return true; // 返回true
}
}
// 释放链表所占空间
template<class DT>
void ClearList(LNode<DT>* (&L))
{
LNode<DT>* p;
while (L->next) // 从首元结点开始,依次释放结点
{
p = L->next;
L->next = p->next;
delete p;
}
cout << endl << "表已清空!" << endl;
}
//【算法2.23】 测表长
template<class DT>
int ListLength(LNode<DT>* L)
{ // 1.初始化
int len = 0; // 1.1 结点计数器赋初值0
LNode<DT>* p; // 1.2设置工作指针
p = L; // 指向头结点
while (p->next) // 2.数结点个数。有后继结点,
{
len++; p = p->next; // 结点数增1指针后移
}
return len; // 3.返回表长
}
//
template<class DT>
bool ListEmpty(LNode<DT>* L) //测表空
{
if (L->next == NULL)
return true; //空表返回1
else
return false; //不空返回0
}
//【算法2.24】 遍历表
template <class DT>
void DispList(LNode<DT>* L) // 显示表内容
{
LNode<DT>* p; // 1. 设置工作指针
p = L; // 从首元结点开始遍历
while (p->next) // 2.依次输出各结点值
{
p = p->next; cout << p->data << "\t";
}
cout << endl;
}

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{b2eef80b-772e-44e2-856d-c8def3242ee3}</ProjectGuid>
<RootNamespace>LinkedList</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="LinkList.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,80 @@
#include <iostream>
using namespace std;
#include "LinkList.h"
template <class DT>
bool CreateListNoRepeat(LNode<DT>*& L, int n)
{
int i;
LNode<DT>* p, * s;
p = L;
cout << "依次输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++)
{
int temp;
cin >> temp;
if (LocateElem_e(L, temp)) i--;
else
{
s = new LNode<DT>;
if (!s) return false;
s->data = temp;
s->next = p->next;
p->next = s;
p = s;
}
}
return true;
}
template <class DT>
void Union(LNode<DT>*& A, LNode<DT>*& B, int length_a, int length_b)
{
for (int i = 1; i <= length_b; ++i) // as "i = 0" is the head node
{
int temp;
GetElem_i(B, i, temp); // You should manually get the element instead of using B.elem[i]
if (LocateElem_e(A, temp) == 0) InsertElem_i(A, ++length_a, temp); // ++length_a to always insert at the end
}
}
template <class DT>
void Intersection(LNode<DT>*& A, LNode<DT>*& B, int length_a)
{
int k = 0;
for (int i = 1; i <= length_a; ++i)
{
int temp;
GetElem_i(A, i, temp);
if (LocateElem_e(B, temp) == 0)
{
DeleElem_i(A, i--);
length_a--;
}
}
}
int main()
{
LNode<int>* A;
LNode<int>* B;
InitList(A);
InitList(B);
int length_a, length_b;
cout << "请输入集合A的元素个数: ";
cin >> length_a;
CreateListNoRepeat(A, length_a);
cout << "请输入集合B的元素个数: ";
cin >> length_b;
CreateListNoRepeat(B, length_b);
cout << "集合A: ";
DispList(A);
cout << "集合B: ";
DispList(B);
Union(A, B, length_a, length_b); // You should manually pass the length of the list instead of using A.length
cout << "AB: ";
DispList(A);
while (true) if (!DeleElem_i(A, length_a + 1)) break; // Delete elements after the original list A
Intersection(A, B, length_a);
cout << "A∩B: ";
DispList(A);
DestroyList(A);
DestroyList(B);
return 0;
}

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{b4e4a6f7-1b97-421c-98bb-79482b2743fe}</ProjectGuid>
<RootNamespace>SequentialList</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,175 @@
template <class DT>
struct SqList // 顺序表
{
DT* elem; // 表首址
int length; // 表长
int size; // 表容量
};
//算法2.1
template <class DT>
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
{
int k;
k = LocateElem_e(L, e); //
if (k > 1)
{
GetElem_i(L, k - 1, pre_e);
return false;
}
else
return true;
}
//【算法2.2】 初始化
template <class DT>
bool InitList(SqList<DT>& L, int m)
{
L.elem = new DT[m]; // 申请表空间
if (L.elem == NULL)
{
cout << "未创建成功!"; // 申请不成功,退出
exit(1);
}
L.length = 0; // 申请成功属性赋值。空表表长为0
L.size = m; // 表容量为m
return true;
}
//【算法2.3】 创建表元素
template <class DT>
bool CreateList(SqList<DT>& L, int n)
{
int i;
if (n > L.size) // 1.元素个数大于表容量,不能创建。
{
cout << "元素个数大于表长,不能创建!" << endl;
return false;
}
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
for (i = 1; i <= n; i++)
cin >> L.elem[i - 1];
L.length = n; // 3.表长为创建的元素个数
return true;
}
//【算法2.4】 销毁顺序表
template <class DT>
void DestroyList(SqList<DT>& L)
{
delete[] L.elem; // 1.释放表空间
L.length = 0; // 2.属性赋值
L.size = 0;
}
//【算法2.5】 获取第i个元素值
template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e)
{
if (i<1 || i>L.length) // 1.位序不合理返回false
{
cout << "该元素不存在!" << endl;
return false;
}
e = L.elem[i - 1]; // 2. 否则获取第i个元素值
return true; // 返回true
}
//【算法2.6】 按值查找
template<class DT>
int LocateElem_e(SqList<DT> L, DT e)
{
for (int i = 1; i <= L.length; i++) // 顺序查找
if (L.elem[i - 1] == e) // 1.找到
return i; // 返回元素位序
return 0; // 2.未找到返回0
}
//【算法2.7】
template<class DT>
bool InsertElem_i(SqList<DT>& L, int i, DT e)
{
if (L.length >= L.size) // 1.表满,不能插入
return false;
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
return false;
for (int j = L.length; j >= i; j--) // 3. an~ai依次后移
L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e;
L.length++;
return true; // 插入成功返回true
}
//【算法2.8】 删除第i个元素
template<class DT>
bool DeleElem_i(SqList<DT>& L, int i)
{
if (L.length == 0) // 1.表空,不能删除
return false;
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
return false;
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
L.elem[j - 1] = L.elem[j];
L.length--;
return true; // 删除成功返回true
}
//【算法2.9】
template<class DT>
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
{
if (i<1 || i>L.length) // 1.位序不合理,不能修改,
return false; // 返回false
L.elem[i - 1] = e; // 2.重置第i个元素值
return true; // 3.修改成功返回true
}
// 清空顺序表
template<class DT>
void ClearList(SqList<DT>& L)
{
L.length = 0; // 空表表长为0
}
// 测表长
template<class DT>
int ListLength(SqList<DT> L)
{
return L.length;
}
template<class DT>
bool ListEmpty(SqList<DT> L) // 测表空
{
if (L.length == 0) // 空表返回true
return true;
else
return false; // 非空表返回false
}
template<class DT>
bool ListFull(SqList<DT> L)
{
if (L.length == L.size) // 表满返回true
return true;
else
return false; // 表不满返回false
}
//【算法2.10】 遍历输出
template <class DT>
void DispList(SqList<DT> L)
{
int i;
for (i = 1; i <= L.length; i++) // 依位序输出元素值
{
cout << L.elem[i - 1] << "\t";
}
cout << endl;
}

View File

@@ -0,0 +1,64 @@
#include <iostream>
using namespace std;
#include "SqList.h"
template <class DT>
bool CreateListNoRepeat(SqList<DT>& L, int n)
{
int i;
if (n > L.size)
{
cout << "元素个数大于表长,不能创建!" << endl;
return false;
}
cout << "请依次输入" << n << "个元素值:" << endl;
for (i = 1; i <= n; i++)
{
int temp;
cin >> temp;
if (LocateElem_e(L, temp) > 0) i--;
else L.elem[i - 1] = temp;
L.length++;
}
L.length = n;
return true;
}
template <class DT>
void Union(SqList<DT>& A, const SqList<DT>& B)
{
for (int i = 0; i < B.length; ++i) if (LocateElem_e(A, B.elem[i]) == 0) InsertElem_i(A, A.length + 1, B.elem[i]);
}
template <class DT>
void Intersection(SqList<DT>& A, const SqList<DT>& B)
{
int k = 0;
for (int i = 0; i < A.length; ++i) if (LocateElem_e(B, A.elem[i]) > 0) A.elem[k++] = A.elem[i];
A.length = k;
}
int main()
{
SqList<int> A;
SqList<int> B;
InitList(A, 32);
InitList(B, 32);
int length_a, length_b;
cout << "请输入集合A的元素个数: ";
cin >> length_a;
CreateListNoRepeat(A, length_a);
cout << "请输入集合B的元素个数: ";
cin >> length_b;
CreateListNoRepeat(B, length_b);
cout << "集合A: ";
DispList(A);
cout << "集合B: ";
DispList(B);
Union(A, B);
cout << "AB: ";
DispList(A);
A.length = length_a; // Reset A to its original length, no need to delete the elements
Intersection(A, B);
cout << "A∩B: ";
DispList(A);
DestroyList(A);
DestroyList(B);
return 0;
}

View File

@@ -0,0 +1,253 @@
template <class DT>
struct LNode //链表结点
{
DT data; //数据域,存储数据元素值
LNode* next; //指针域,指向下一个结点
};
//算法2.1
template <class DT>
bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱
{
int k;
k = LocateElem_e(L, e); // 1.获取e的位序k
if (k > 1) // 2.位序k大于1
{
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
return true;
}
else // 3.元素e无前驱
return false; // 返回false
}
//【算法2.14】 创建空单链表
template <class DT>
bool InitList(LNode<DT>*& L)
{
L = new LNode<DT>; // 1.创建头结点
if (!L) exit(1); // 2.创建失败,退出
L->next = NULL; // 3.创建成功
return true; // 返回true
}
//【算法2.15】 尾插法创建n的元素
template <class DT>
bool CreateList_1(LNode<DT>*& L, int n)
{
int i;
LNode<DT>* p, * s;
p = L; //1.工作指针初始化,指向尾结点
cout << "依次输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点
{
s = new LNode<DT>; // 2.1 新建一个结点s
if (!s) // 2.2 创建失败返回false
return false;
cin >> s->data; // 2.3 输入结点值
s->next = p->next; // 2.4 s 链在表尾
p->next = s;
p = s; // 2.5 工作指针指向 s
}
return true; // 3.创建成功返回true
}
//【算法2.16】 头插法创建n个元素
template <class DT>
bool CreateList_2(LNode<DT>* (&L), int n)
{
int i;
LNode<DT>* s;
cout << "逆序输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
{
s = new LNode<DT>; // 1.1 新建一个结点 s
if (!s) // 1.2 创建失败返回false
return false;
cin >> s->data; // 1.3 输入结点值
s->next = L->next; // 1.4 s 在头结点后
L->next = s;
}
return true; // 1.创建成功返回true
}
//【算法2.17】
template <class DT>
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
{
LNode<DT>* p;
while (L) // 1. 表非空,从头结点开始,依次释放结点
{
p = L; // 1.1 处理表头结点
L = L->next; // 1.2 头指针后移
delete p; // 1.3 释放表头结点所占内存
}
L = NULL; // 2.头指针指向空
}
//【算法2.18】 获取第i个元素
template<class DT>
bool GetElem_i(LNode<DT>* L, int i, DT& e)
{
LNode<DT>* p; // 1.初始化
p = L->next; // 1.1 设置工作指针,从首结点开始数结点
int j = 1; // 1.2 计数器初始化
while (p && j < i) // 2.定位到第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3 未找到返回false
return false;
else // 4. 找到
{
e = p->data; // 获取第i个元素值
return true; // 返回true
}
}
//【算法2.19】 查找值为e的元素位序
template<class DT>
int LocateElem_e(LNode<DT>* L, DT e)
{
LNode<DT>* p; // 1.初始化从首元开始查找
p = L->next; // 1.1从首元开始查找
int j = 1; // 1.2 计数器初值
while (p && p->data != e) // 2.顺序查找
{
p = p->next; // 2.1未找到指针后移
j++; // 2.2 计数器增1
}
if (p == NULL) // 3. 判断是否找到
return 0; // 3.1末找到返回0
else
return j; // 3.2 找到,返回位序
}
//【算法2.20】 插入第i个元素
template<class DT>
bool InsertElem_i(LNode<DT>*& L, int i, DT e)
{
int j = 0;
LNode<DT>* p; // 1.初始化
p = L; // 工作指针初始化
while (p && j < i - 1) // 2. 定位到插入点前驱
{
p = p->next;
j++;
}
if (!p || j > i - 1) // 3.判断定位是否成功:
return false; // 3.1 定位失败,不能插入
else // 3.2 定位成功
{
LNode<DT>* s;
s = new LNode<DT>; // 3.2.1建立新结点
s->data = e; // 3.2.2新结点赋值
s->next = p->next; // 3.2.3结点S链接到p结点之后
p->next = s;
return true; // 3.2.4 插入成功返回true
}
}
//【算法2.21】 删除第i个元素
template<class DT>
bool DeleElem_i(LNode<DT>* (&L), int i)
{
LNode<DT>* p, * q; //1.初始化:设置工作指针
p = L; //查找从头结点开始
int j = 0; //计数器初始化
while (p->next && j < i - 1) //2.p定位到删除点的前驱
{
p = p->next;
j++;
}
if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
return false; //返回false
else //4.删除操作
{
q = p->next; //4.1暂存删除结点位置
p->next = q->next; //4.2从链表中摘除删除结点
delete q;
return true; //4.3删除成功返回true
}
}
//【算法2.22】 修改第i个元素值
template<class DT>
bool PutElem_i(LNode<DT>* (&L), int i, DT e)
{
LNode<DT>* p; // 1.初始化:设置工作指针
p = L->next; // 从首结点开始,数结点
int j = 1; // 计数器初始化
while (p && j < i) // 2.查找第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3.元素不存在,返回false
return false;
else // 4.定位成功
{
p->data = e; // 修改元素值
return true; // 返回true
}
}
// 释放链表所占空间
template<class DT>
void ClearList(LNode<DT>* (&L))
{
LNode<DT>* p;
while (L->next) // 从首元结点开始,依次释放结点
{
p = L->next;
L->next = p->next;
delete p;
}
cout << endl << "表已清空!" << endl;
}
//【算法2.23】 测表长
template<class DT>
int ListLength(LNode<DT>* L)
{ // 1.初始化
int len = 0; // 1.1 结点计数器赋初值0
LNode<DT>* p; // 1.2设置工作指针
p = L; // 指向头结点
while (p->next) // 2.数结点个数。有后继结点,
{
len++; p = p->next; // 结点数增1指针后移
}
return len; // 3.返回表长
}
//
template<class DT>
bool ListEmpty(LNode<DT>* L) //测表空
{
if (L->next == NULL)
return true; //空表返回1
else
return false; //不空返回0
}
//【算法2.24】 遍历表
template <class DT>
void DispList(LNode<DT>* L) // 显示表内容
{
LNode<DT>* p; // 1. 设置工作指针
p = L; // 从首元结点开始遍历
while (p->next) // 2.依次输出各结点值
{
p = p->next; cout << p->data << "\t";
}
cout << endl;
}

View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34701.34
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LinkListOperation", "LinkListOperation.vcxproj", "{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x64.ActiveCfg = Debug|x64
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x64.Build.0 = Debug|x64
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x86.ActiveCfg = Debug|Win32
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x86.Build.0 = Debug|Win32
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x64.ActiveCfg = Release|x64
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x64.Build.0 = Release|x64
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x86.ActiveCfg = Release|Win32
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E04AA2D6-0453-4D15-9DFB-2771EC700397}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{baa96efe-86cd-47ea-8d5b-caf1cc77bdc6}</ProjectGuid>
<RootNamespace>LinkListOperation</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="LinkList.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,76 @@
#include <iostream>
using namespace std;
#include "LinkList.h"
template <class DT>
bool CreateOrderedList(LNode<DT>*& L, int n)
{
int i;
LNode<DT>* p, * s;
p = L;
cout << "依次输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++)
{
s = new LNode<DT>;
if (!s) return false;
cin >> s->data;
if (s->data >= p->data || p == NULL)
{
s->next = p->next;
p->next = s;
p = s;
}
else i--;
}
return true;
}
template <class DT>
void MergeTwoOrderedLists(LNode<DT>*& L1, LNode<DT>*& L2)
{
LNode<DT>* p, * q, * s;
p = L1->next;
q = L1->next->next;
s = L2->next;
while (!ListEmpty(L2))
{
if (q == NULL)
{
p->next = s;
return;
}
else if ((q->data) >= (s->data) && (p->data) <= (s->data))
{
LNode<DT>* t;
t = s->next;
s->next = q;
p->next = s;
s = t;
if (s == NULL) return;
p = p->next;
}
else
{
p = p->next;
q = q->next;
}
}
}
int main()
{
LNode<int>* L;
InitList(L);
int length;
cout << "请输入有序表的长度: ";
cin >> length;
CreateOrderedList(L, length);
DispList(L);
LNode<int>* L2;
InitList(L2);
int length2;
cout << "请输入另一个有序表的长度: ";
cin >> length2;
CreateOrderedList(L2, length2);
DispList(L2);
MergeTwoOrderedLists(L, L2);
DispList(L);
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,54 @@
#include <string>
#include <iostream>
using namespace std;
#include "LinkStack.h"
bool match(string exp)
{
SNode<char>* S;
InitStack(S);
int flag = 1, i = 0;
char ch, e, x;
ch = exp[i++];
while (ch != '#' && flag)
{
switch (ch)
{
case '[':
case '(':
cout << "左括号进栈!" << endl;
Push(S, ch);
break;
case ')':
GetTop(S, e);
if (!StackEmpty(S) && e == '(')
{
Pop(S, x);
cout << "右括号出栈!" << endl;
}
else flag = 0;
break;
case ']':
GetTop(S, e);
if (!StackEmpty(S) && e == '[') Pop(S, x);
else flag = 0;
break;
}
ch = exp[i++];
}
if (StackEmpty(S) && flag) return true;
else return false;
}
int main()
{
int flag;
string exp;
cout << "请输入待匹配的表达式,以“#”结束:" << endl;
cin >> exp;
flag = match(exp);
if (flag) cout << "括号匹配成功!" << endl;
else cout << "括号匹配失败!" << endl;
return 0;
}

View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BracketsMatch", "BracketsMatch.vcxproj", "{BC507152-80AB-4A4F-9006-339728D357CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BC507152-80AB-4A4F-9006-339728D357CA}.Debug|x64.ActiveCfg = Debug|x64
{BC507152-80AB-4A4F-9006-339728D357CA}.Debug|x64.Build.0 = Debug|x64
{BC507152-80AB-4A4F-9006-339728D357CA}.Debug|x86.ActiveCfg = Debug|Win32
{BC507152-80AB-4A4F-9006-339728D357CA}.Debug|x86.Build.0 = Debug|Win32
{BC507152-80AB-4A4F-9006-339728D357CA}.Release|x64.ActiveCfg = Release|x64
{BC507152-80AB-4A4F-9006-339728D357CA}.Release|x64.Build.0 = Release|x64
{BC507152-80AB-4A4F-9006-339728D357CA}.Release|x86.ActiveCfg = Release|Win32
{BC507152-80AB-4A4F-9006-339728D357CA}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {12C9DCB1-398A-4F5B-9896-2020AF51DA75}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{bc507152-80ab-4a4f-9006-339728d357ca}</ProjectGuid>
<RootNamespace>BracketsMatch</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="LinkStack.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="BracketsMatch.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkStack.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="BracketsMatch.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,92 @@
template <class DT>
struct SNode //结点
{
DT data; //数据域,存储数据元素值
SNode* next;//指针域,指向下一个结点
};
//【算法3.6】
template <class DT>
void InitStack(SNode<DT>*& S)//创建空链栈
{
S = NULL;
}
//【算法3.7】
template <class DT>
void DestroyStack(SNode<DT>* (&S))//释放链栈
{
SNode<DT>* p;
while (S)//从头结点开始,依次释放结点
{
p = S;
S = S->next;
delete p;
}
//L=NULL;//头结点指向空
}
//【算法3.8】
template<class DT>
bool Push(SNode<DT>*& S, DT e)
{
SNode<DT>* p;
p = new SNode<DT>;
if (!p) return false; //创建失败,结束运行
p->data = e; // 新结点赋值
p->next = S; //结点S链接到p结点之后
S = p;
return true; // 插入成功返回true
}
//【算法3.9】
template<class DT>
bool Pop(SNode<DT>*& S, DT& e)
{
SNode<DT>* p;
if (S == NULL) return false;
p = S;
e = p->data;
S = S->next;
delete p;
return true; // 删除成功返回true
}
//【算法3.10】
template<class DT>
bool GetTop(SNode<DT>* S, DT& e)
{
SNode<DT>* p;
if (S == NULL) return false;
p = S;
e = p->data;
return true; // 删除成功返回true
}
//测栈空
template<class DT>
bool StackEmpty(SNode<DT>* S)
{
if (S == NULL)
return true;
else
return false;
}
//显示栈内容
template<class DT>
void DispStack(SNode<DT>* S)
{
SNode<DT>* p;
p = S;
while (p)
{
cout << p->data << "\t";
p = p->next;
}
cout << endl;
}

View File

@@ -0,0 +1,77 @@
#include <iostream>
#include "LinkQueue.h"
using namespace std;
struct Car
{
int no; //车号
char type; //c:客车t:货车
};
//求队列长度
template<class DT>
int QueueLength(LinkQueue<DT> Q)
{
int n = 0;
QNode<DT>* p = Q.front->next;
while (p)
{
p = p->next;
n++;
}
return n;
}
//车辆入队
void Ferry(LinkQueue<Car>& Qc, LinkQueue<Car>& Qt)
{
int i = 0;
Car e;
while (QueueLength(Qc) + QueueLength(Qt) > 0) //队列非空
{
if (QueueLength(Qc) > 0) //客车队列非空
{
while ((i < 4) && (QueueLength(Qc) > 0)) //客车数小于4且客车队列非空
{
DeQueue(Qc, e); //客车出队
cout << "车号:" << e.no << "\t类型:" << e.type << endl;
i++;
}
i = 0;
}
if (QueueLength(Qt) > 0) //货车队列非空
{
DeQueue(Qt, e); //货车出队
cout << "车号:" << e.no << "\t类型:" << e.type << endl;
}
}
cout << "所有车辆已过江!" << endl;
}
int main()
{
LinkQueue<Car> Qc, Qt;
Car e;
int i, n;
InitQueue(Qc);
InitQueue(Qt);
cout << "客车数:";
cin >> n;
for (i = 1; i <= n; i++) //客车入队
{
e.no = i;
e.type = 'c';
EnQueue(Qc, e);
}
cout << "货车数:";
cin >> n;
for (i = 1; i <= n; i++) //货车入队
{
e.no = i;
e.type = 't';
EnQueue(Qt, e);
}
Ferry(Qc, Qt); //车辆过江
DestroyQueue(Qc);
DestroyQueue(Qt);
return 0;
}

View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Ferry", "Ferry.vcxproj", "{DFE075CA-094D-4856-92A8-0461FC180B13}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x64.ActiveCfg = Debug|x64
{DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x64.Build.0 = Debug|x64
{DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x86.ActiveCfg = Debug|Win32
{DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x86.Build.0 = Debug|Win32
{DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x64.ActiveCfg = Release|x64
{DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x64.Build.0 = Release|x64
{DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x86.ActiveCfg = Release|Win32
{DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B24F9342-2DAA-4663-8993-00F01D577229}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{dfe075ca-094d-4856-92a8-0461fc180b13}</ProjectGuid>
<RootNamespace>Ferry</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="LinkQueue.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Ferry.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkQueue.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Ferry.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,129 @@
#include <iostream>
using namespace std;
template <class DT>
struct QNode //结点
{
DT data; //数据域,存储数据元素值
QNode* next;//指针域,指向下一个结点
};
template<class DT>
struct LinkQueue
{
QNode<DT>* front;
QNode<DT>* rear;
};
//【算法3.19】
template <class DT>
void InitQueue(LinkQueue<DT>& Q)//创建空队列
{
Q.front = new QNode<DT>; //创建头结点
if (!Q.front) exit(1); //创建失败,结束运行
Q.front->next = NULL;
Q.rear = Q.front;
}
//【算法3.20】
template <class DT>
void DestroyQueue(LinkQueue<DT>& Q)//释放链队
{
QNode<DT>* p;
while (Q.front)//从头结点开始,依次释放结点
{
p = Q.front;
Q.front = Q.front->next;
delete p;
}
Q.rear = Q.front = NULL;
}
template <class DT>
void ClearQueue(LinkQueue<DT>& Q) // 清空链队
{
QNode<DT>* p;
while (Q.front->next) //从队头开始,依次释放结点
{
p = Q.front->next;
Q.front->next = p->next;
delete p;
}
Q.front->next = NULL; // 空队
Q.rear = Q.front;
}
//【算法3.21】 入队
template<class DT>
bool EnQueue(LinkQueue<DT>& Q, DT e)
{
QNode<DT>* p;
p = new QNode<DT>; // 创建新结点
if (!p) return false; // 创建失败,结束运行
p->data = e; // 新结点赋值
p->next = NULL; // 链在队尾
Q.rear->next = p;
Q.rear = p;
return true; // 入队成功返回true
}
//【算法3.22】 出队
template<class DT>
bool DeQueue(LinkQueue<DT>& Q, DT& e)
{
QNode<DT>* p;
if (Q.front == Q.rear) return false; //队空返回false
p = Q.front->next; // 取出队元素
e = p->data;
Q.front->next = p->next; //队首元素出队
if (Q.rear == p) //只有一个元素时出队,
Q.rear = Q.front; // 修改队尾
delete p;
return true; // 出队成功返回true
}
//【算法3.23】 取队头元素
template<class DT>
bool GetHead(LinkQueue<DT> Q, DT& e)
{
if (Q.front == Q.rear) return false; // 队空返回false
e = Q.front->next->data;
return true; // 删除成功返回true
}
//取队尾元素
template<class DT>
bool GetTail(LinkQueue<DT> Q, DT& e)
{
if (Q.front == Q.rear) // 队空
return false; // 返回false
e = Q.rear->data; // 获取队尾元素
return true; // 返回true
}
//测队空
template<class DT>
bool QueueEmpty(LinkQueue<DT> Q)
{
if (Q.front == Q.rear) // 队空
return true; //返回true
else //非空
return false; //返回false
}
//显示队列内容
template<class DT>
void DispQueue(LinkQueue<DT> Q)
{
QNode<DT>* p;
p = Q.front->next;
while (p)
{
cout << p->data << "\t";
p = p->next;
}
cout << endl;
}

View File

@@ -0,0 +1,53 @@
#include <iostream>
#include "BiTree.h"
using namespace std;
//递归方法求解二叉树叶节点个数
template <class DT>
int LeafNum_1(BTNode<DT>* bt) {
if (!bt) {
return 0;
}
else if (bt->lchild == NULL && bt->rchild == NULL) {
return 1;
}
else {
return LeafNum_1(bt->lchild) + LeafNum_1(bt->rchild);
}
}
//非递归方法求解二叉树叶节点个数
template <class DT>
int LeafNum_2(BTNode<DT>* bt) {
if (!bt)
return 0;
SqStack<DT> S;
InitStack(S, 20);
BTNode<DT>* p = bt;
int count = 0;
while (p != NULL || !StackEmpty(S))
{
while (p != NULL)
{
if (p->lchild == NULL && p->rchild == NULL)
count++;
Push(S, p);
p = p->lchild;
}
if (!StackEmpty(S))
{
Pop(S, p);
p = p->rchild;
}
}
return count;
}
int main() {
BTNode<char>* bt;
CreateBiTree(bt);
cout << "递归方法求得二叉树叶节点个数为:" << LeafNum_1(bt) << endl;
cout << "非递归方法求得二叉树叶节点个数为:" << LeafNum_2(bt) << endl;
DestroyBiTree(bt);
return 0;
}

View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LeafNum", "LeafNum.vcxproj", "{A1435839-D838-4C8A-8F43-03AE8436D845}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x64.ActiveCfg = Debug|x64
{A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x64.Build.0 = Debug|x64
{A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x86.ActiveCfg = Debug|Win32
{A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x86.Build.0 = Debug|Win32
{A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x64.ActiveCfg = Release|x64
{A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x64.Build.0 = Release|x64
{A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x86.ActiveCfg = Release|Win32
{A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C6AC6B0D-CC6B-4023-9F49-D0756F2B0732}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{a1435839-d838-4c8a-8f43-03ae8436d845}</ProjectGuid>
<RootNamespace>LeafNum</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="bitree.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="LeafNum.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="bitree.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="LeafNum.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

Binary file not shown.

View File

@@ -0,0 +1,424 @@
#include <iostream>
using namespace std;
template <class DT>
struct BTNode
{
DT data; //数据域
BTNode* lchild; //指向左子树的指针
BTNode* rchild; //指向右子树的指针
};
//队列工具
template <class DT>
struct SqQueue // 顺序队类
{
BTNode<DT>** base; // 队列首址
int front; // 队头指针
int rear; // 队尾指针
int queuesize; // 队容量
};
//栈
template <class DT>
struct SqStack // 顺序栈
{
BTNode<DT>** base; // 栈首址
int top; // 栈顶指针
int stacksize; // 栈容量
};
//算法5.1 先序遍历递归算法
template <class DT>
void PreOrDerBiTree(BTNode<DT>* bt)
{
if (bt != NULL)
{
cout << bt->data << ' '; //输出结点上的数据
PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树
PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树
}
return;
}
//算法5.2 中序遍历递归算法
template <class DT>
void InOrDerBiTree(BTNode<DT>* bt)
{
if (bt != NULL)
{
InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树
cout << bt->data << ' '; //输出结点上的数据
InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树
}
return;
}
//算法5.3 后序遍历递归算法
template <class DT>
void PostOrDerBiTree(BTNode<DT>* bt)
{
if (bt != NULL)
{
PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树
PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树
cout << bt->data << ' '; //输出结点上的数据
}
return;
}
//算法5.4 层序遍历算法
template<class DT>
void LevelBiTree(BTNode<DT>* bt)
{
SqQueue<DT> Q; // 创建一个队
int m = 20;
InitQueue(Q, m);
BTNode<DT>* p;
p = bt;
if (p) EnQueue(Q, p); // 树非空,入队
while (!QueueEmpty(Q)) // 队非空
{
DeQueue(Q, p); // 出队
cout << p->data; // 访问
if (p->lchild != NULL) // 有左孩子
EnQueue(Q, p->lchild); // 左孩子入队
if (p->rchild != NULL) // 有右孩子
EnQueue(Q, p->rchild); // 右孩子入队
}
DestroyQueue(Q); // 销毁队列
}
//算法5.5 先序非遍历递归算法
template <class DT>
void PreOrderBiTree_N(BTNode<DT>* bt)
{
SqStack<DT> S; // 创建栈
int m = 20;
InitStack(S, m);
BTNode<DT>* p;
p = bt;
while (p != NULL || !StackEmpty(S)) // 树非空或栈非空
{
while (p != NULL) // 结点非空
{
cout << p->data << ' '; // 访问结点
Push(S, p); // 入栈
p = p->lchild; // 转左子树
}
if (!StackEmpty(S)) // 栈非空
{
Pop(S, p); // 出栈
p = p->rchild; // 转出栈结点的右子树
}
}
DestroyStack(S); //销毁栈
}
//算法5.6 中序非遍历递归算法
template <class DT>
void InOrderBiTree_N(BTNode<DT>* bt)
{
SqStack<DT> S; // 创建一个栈
int m = 20;
InitStack(S, m);
BTNode<DT>* p;
p = bt;
while (p != NULL || !StackEmpty(S)) // 结点非空或栈非空
{
while (p != NULL) // 结点非空
{
Push(S, p); // 入栈
p = p->lchild; // 转出栈结点右子树
}
if (!StackEmpty(S)) // 栈非空
{
Pop(S, p); // 出栈
cout << p->data << ' '; // 访问出栈结点
p = p->rchild; // 转出栈结点的右子树
}
}
DestroyStack(S); // 销毁栈
}
//算法5.7 后序非遍历递归算法
template <class DT>
void PostOrderBiTree_N(BTNode<DT>* bt)
{
SqStack<DT> S; // 创建一个栈
int m = 20;
InitStack(S, m);
BTNode<DT>* p;
BTNode<DT>* r;
p = bt;
bool flag;
do
{
while (p) // 结点非空
{
Push(S, p); // 结点入栈
p = p->lchild; // 转左子树
}
r = NULL; // 指向刚被访问点,初值为空
flag = true; // true表示处理栈顶结点
while (!StackEmpty(S) && flag) // 栈非空且当前处理的是栈顶结点
{
GetTop(S, p); // 获取栈顶元素
if (p->rchild == r) // 如果 当前结点是栈元素的右孩子
{
cout << p->data << ' '; // 访问栈顶元素
Pop(S, p); // 出栈
r = p; // r指向被访问结点
}
else // 否则
{
p = p->rchild; // 转栈顶元素右孩子
flag = false; // 处理非栈顶结点
}
}
} while (!StackEmpty(S)); // 栈非空,循环
cout << endl;
DestroyStack(S); // 销毁栈
}
//算法5.8 创建二叉树
template <class DT>
void CreateBiTree(BTNode<DT>*& bt)
{
char ch;
cin >> ch; // 输入根结点的数据
if (ch == '#') // # 表示指针为空,说明树为空
bt = NULL;
else
{
bt = new BTNode<DT>; // 申请内存
if (!bt)
{
cout << "申请内存失败!" << endl;
exit(-1); // 申请内存失败退出
}
bt->data = ch;
CreateBiTree(bt->lchild); // 创建根结点左子树
CreateBiTree(bt->rchild); // 创建根结点右子树
}
return;
}
//算法5.9 销毁二叉树
template <class DT>
void DestroyBiTree(BTNode<DT>*& bt)
{
if (bt) // 树非空
{
DestroyBiTree(bt->lchild); // 销毁左子树
DestroyBiTree(bt->rchild); // 销毁右子树
delete bt;
}
}
//算法5.10 结点查找
template<class DT>
BTNode<DT>* Search(BTNode<DT>* bt, DT e) // 查找值为e的元素
{
BTNode<DT>* p;
if (bt == NULL) // 结点为空,返回
return NULL;
else if (bt->data == e) // 找到,返回结点指针
return bt;
else // 结点值不为e
{
p = Search(bt->lchild, e); // 在左子树上查找
if (p != NULL) // 找到
return p; // 返回结点指针
else // 未找到
return Search(bt->rchild, e); // 转右子树上查找
}
}
//算法5.11 求树深
template <class DT>
int Depth(BTNode<DT>* bt)
{
int hl, hr;
if (bt == NULL) // 树空
return 0; // 深度为0
else // 树非空
{
hl = Depth(bt->lchild); // 求左子树深度
hr = Depth(bt->lchild); // 求右子树深度
if (hl > hr) // 左子树高
return hl + 1; // 树高为左子树高加1
else return hr + 1; // 左子树高,树高为左子树高加1
}
}
//算法5.12 结点计数
template <class DT>
int NodeCount(BTNode<DT>* bt)
{
if (bt == NULL) // 空树结点数为0
return 0;
else // 非空树结点数为左、右子树结点数的和加1
return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
}
template <class DT>
void DispBiTree(BTNode<DT>* bt, int level) // 显示树
{
if (bt) //空二叉树不显示
{
DispBiTree(bt->rchild, level + 1); //显示右子树
cout << endl; //显示新行
for (int i = 0; i < level - 1; i++)
cout << " "; //确保在第level列显示节点
cout << bt->data; //显示节点
DispBiTree(bt->lchild, level + 1); //显示左子树
cout << endl;
}//if
}
template <class DT>
int leftCount(BTNode<DT>* bt)
{
if (bt->lchild == NULL && bt->rchild == NULL) // 空树结点数为0
return 0;
else // 非空树结点数为左、右子树结点数的和加1
return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
}
//【算法3.14】 初始化队列
template <class DT>
void InitQueue(SqQueue<DT>& Q, int m)
{
Q.base = new BTNode<DT>*[m]; // 申请队列空间
if (Q.base == NULL) // 申请空间失败
{
cout << "未创建成功!";
exit(1); // 退出
}
Q.front = Q.rear = 0; // 设置队列属性
Q.queuesize = m;
}
//算法3.15】 销毁列列
template <class DT>
void DestroyQueue(SqQueue<DT>& Q)
{
delete[] Q.base; // 释放队列空间
Q.front = Q.rear = 0; // 设置队列属性
Q.queuesize = 0;
}
//【算法3.16】 入队
template<class DT>
bool EnQueue(SqQueue<DT>& Q, BTNode<DT>* e)
{
if ((Q.rear + 1) % Q.queuesize == Q.front) // 队满
return false; // 返回false
Q.base[Q.rear] = e; // 出队
Q.rear = (Q.rear + 1) % Q.queuesize; // 修改队列属性
return true; // 返回true
}
//【算法3.17】 出队
template<class DT>
bool DeQueue(SqQueue<DT>& Q, BTNode<DT>*& e)
{
if (Q.front == Q.rear) // 队空
return false;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % Q.queuesize;
return true; // 删除成功返回true
}
// 测队空
template<class DT>
bool QueueEmpty(SqQueue<DT> Q)
{
if (Q.front == Q.rear) // 队空
return true; // 返回true
else // 队不空
return false; // 返回false
}
//【算法3.1】 初始化栈
template <class DT>
void InitStack(SqStack<DT>& S, int m)
{
S.base = new BTNode<DT>*[m]; // 申请栈空间
if (S.base == NULL)
{
cout << "未创建成功!";
exit(1);
}
S.top = -1; // 空栈
S.stacksize = m; // 栈容量为m
}
//算法3.2】 销毁栈
template <class DT>
void DestroyStack(SqStack<DT>& S)//析构函数
{
delete[] S.base; //释放栈空间
S.top = -1;
S.stacksize = 0;
}
//【算法3.3】
template<class DT>
bool Push(SqStack<DT>& S, BTNode<DT>* e)
{
if (S.top == S.stacksize - 1) // 栈满,不能插入
return false;
S.top++;
S.base[S.top] = e;
return true; // 插入成功返回true
}
//【算法3.4】 出栈
template<class DT>
bool Pop(SqStack<DT>& S, BTNode<DT>*& e)
{
if (S.top == -1) //栈空
return false;
e = S.base[S.top];
S.top--;
return true; // 出栈成功返回true
}
template<class DT> // 获取栈元素
bool GetTop(SqStack<DT>& S, BTNode<DT>*& e)
{
if (S.top == -1) // 栈空返回false
return false;
e = S.base[S.top]; // 取栈元素
return true; // 返回true
}
// 测栈空
template<class DT>
bool StackEmpty(SqStack<DT> S)
{
if (S.top == -1) // 栈空返回true
return true;
else // 栈非空返回false
return false;
}

View File

@@ -0,0 +1,62 @@
#include <iostream>
#include "LinkList.h"
using namespace std;
template <class DT>
void InsertSort_LL(LNode<DT>* L) {
if (L == NULL || L->next == NULL) {
return; // 空表或只有一个结点,不需要排序
}
LNode<DT>* r = L->next; // 已排序表表尾
LNode<DT>* q = r->next; // q为当前处理项r的后继
while (q) {
LNode<DT>* p1 = L;
LNode<DT>* p2 = p1->next;
while (q->data >= p2->data && p2 != q) { // 当前结点数据大于等于p2插入点后移
p1 = p2;
p2 = p2->next;
}
if (p2 == q) { // 当前项无须移动
r = q; // 有序表表尾顺移
}
else { // 插入p2前面
r->next = q->next; // 摘除q结点
q->next = p1->next; // 在p1后插入结点q
p1->next = q;
}
q = r->next; // 下一个需处理的项
}
}
int main() {
LNode<int>* L;
int n;
// 创建单链表
cout << "请输入单链表的长度:";
cin >> n;
InitList(L);
CreateList_1(L, n);
// 显示初始单链表
cout << "初始单链表:";
DispList(L);
// 插入排序
InsertSort_LL(L);
// 显示排序后的单链表
cout << "排序后的单链表:";
DispList(L);
// 销毁单链表
DestroyList(L);
return 0;
}

View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34916.146
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "InsertSort", "InsertSort.vcxproj", "{3CA81F66-07E1-40A0-9984-17556857602C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x64.ActiveCfg = Debug|x64
{3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x64.Build.0 = Debug|x64
{3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x86.ActiveCfg = Debug|Win32
{3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x86.Build.0 = Debug|Win32
{3CA81F66-07E1-40A0-9984-17556857602C}.Release|x64.ActiveCfg = Release|x64
{3CA81F66-07E1-40A0-9984-17556857602C}.Release|x64.Build.0 = Release|x64
{3CA81F66-07E1-40A0-9984-17556857602C}.Release|x86.ActiveCfg = Release|Win32
{3CA81F66-07E1-40A0-9984-17556857602C}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {638C3207-9916-43D8-88EC-BC64BB58E25C}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{3ca81f66-07e1-40a0-9984-17556857602c}</ProjectGuid>
<RootNamespace>InsertSort</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="InsertSort.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkList.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="InsertSort.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,256 @@
#include <iostream>
using namespace std;
template <class DT>
struct LNode //链表结点
{
DT data; //数据域,存储数据元素值
LNode* next; //指针域,指向下一个结点
};
//算法2.1
template <class DT>
bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱
{
int k;
k = LocateElem_e(L, e); // 1.获取e的位序k
if (k > 1) // 2.位序k大于1
{
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
return true;
}
else // 3.元素e无前驱
return false; // 返回false
}
//【算法2.14】 创建空单链表
template <class DT>
bool InitList(LNode<DT>*& L)
{
L = new LNode<DT>; // 1.创建头结点
if (!L) exit(1); // 2.创建失败,退出
L->next = NULL; // 3.创建成功
return true; // 返回true
}
//【算法2.15】 尾插法创建n的元素
template <class DT>
bool CreateList_1(LNode<DT>*& L, int n)
{
int i;
LNode<DT>* p, * s;
p = L; //1.工作指针初始化,指向尾结点
cout << "依次输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点
{
s = new LNode<DT>; // 2.1 新建一个结点s
if (!s) // 2.2 创建失败返回false
return false;
cin >> s->data; // 2.3 输入结点值
s->next = p->next; // 2.4 s 链在表尾
p->next = s;
p = s; // 2.5 工作指针指向 s
}
return true; // 3.创建成功返回true
}
//【算法2.16】 头插法创建n个元素
template <class DT>
bool CreateList_2(LNode<DT>* (&L), int n)
{
int i;
LNode<DT>* s;
cout << "逆序输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
{
s = new LNode<DT>; // 1.1 新建一个结点 s
if (!s) // 1.2 创建失败返回false
return false;
cin >> s->data; // 1.3 输入结点值
s->next = L->next; // 1.4 s 在头结点后
L->next = s;
}
return true; // 1.创建成功返回true
}
//【算法2.17】
template <class DT>
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
{
LNode<DT>* p;
while (L) // 1. 表非空,从头结点开始,依次释放结点
{
p = L; // 1.1 处理表头结点
L = L->next; // 1.2 头指针后移
delete p; // 1.3 释放表头结点所占内存
}
L = NULL; // 2.头指针指向空
}
//【算法2.18】 获取第i个元素
template<class DT>
bool GetElem_i(LNode<DT>* L, int i, DT& e)
{
LNode<DT>* p; // 1.初始化
p = L->next; // 1.1 设置工作指针,从首结点开始数结点
int j = 1; // 1.2 计数器初始化
while (p && j < i) // 2.定位到第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3 未找到返回false
return false;
else // 4. 找到
{
e = p->data; // 获取第i个元素值
return true; // 返回true
}
}
//【算法2.19】 查找值为e的元素位序
template<class DT>
int LocateElem_e(LNode<DT>* L, DT e)
{
LNode<DT>* p; // 1.初始化从首元开始查找
p = L->next; // 1.1从首元开始查找
int j = 1; // 1.2 计数器初值
while (p && p->data != e) // 2.顺序查找
{
p = p->next; // 2.1未找到指针后移
j++; // 2.2 计数器增1
}
if (p == NULL) // 3. 判断是否找到
return 0; // 3.1末找到返回0
else
return j; // 3.2 找到,返回位序
}
//【算法2.20】 插入第i个元素
template<class DT>
bool InsertElem_i(LNode<DT>*& L, int i, DT e)
{
int j = 0;
LNode<DT>* p; // 1.初始化
p = L; // 工作指针初始化
while (p && j < i - 1) // 2. 定位到插入点前驱
{
p = p->next;
j++;
}
if (!p || j > i - 1) // 3.判断定位是否成功:
return false; // 3.1 定位失败,不能插入
else // 3.2 定位成功
{
LNode<DT>* s;
s = new LNode<DT>; // 3.2.1建立新结点
s->data = e; // 3.2.2新结点赋值
s->next = p->next; // 3.2.3结点S链接到p结点之后
p->next = s;
return true; // 3.2.4 插入成功返回true
}
}
//【算法2.21】 删除第i个元素
template<class DT>
bool DeleElem_i(LNode<DT>* (&L), int i)
{
LNode<DT>* p, * q; //1.初始化:设置工作指针
p = L; //查找从头结点开始
int j = 0; //计数器初始化
while (p->next && j < i - 1) //2.p定位到删除点的前驱
{
p = p->next;
j++;
}
if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
return false; //返回false
else //4.删除操作
{
q = p->next; //4.1暂存删除结点位置
p->next = q->next; //4.2从链表中摘除删除结点
delete q;
return true; //4.3删除成功返回true
}
}
//【算法2.22】 修改第i个元素值
template<class DT>
bool PutElem_i(LNode<DT>* (&L), int i, DT e)
{
LNode<DT>* p; // 1.初始化:设置工作指针
p = L->next; // 从首结点开始,数结点
int j = 1; // 计数器初始化
while (p && j < i) // 2.查找第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3.元素不存在,返回false
return false;
else // 4.定位成功
{
p->data = e; // 修改元素值
return true; // 返回true
}
}
// 释放链表所占空间
template<class DT>
void ClearList(LNode<DT>* (&L))
{
LNode<DT>* p;
while (L->next) // 从首元结点开始,依次释放结点
{
p = L->next;
L->next = p->next;
delete p;
}
cout << endl << "表已清空!" << endl;
}
//【算法2.23】 测表长
template<class DT>
int ListLength(LNode<DT>* L)
{ // 1.初始化
int len = 0; // 1.1 结点计数器赋初值0
LNode<DT>* p; // 1.2设置工作指针
p = L; // 指向头结点
while (p->next) // 2.数结点个数。有后继结点,
{
len++; p = p->next; // 结点数增1指针后移
}
return len; // 3.返回表长
}
//
template<class DT>
bool ListEmpty(LNode<DT>* L) //测表空
{
if (L->next == NULL)
return true; //空表返回1
else
return false; //不空返回0
}
//【算法2.24】 遍历表
template <class DT>
void DispList(LNode<DT>* L) // 显示表内容
{
LNode<DT>* p; // 1. 设置工作指针
p = L; // 从首元结点开始遍历
while (p->next) // 2.依次输出各结点值
{
p = p->next; cout << p->data << "\t";
}
cout << endl;
}
#pragma once

Binary file not shown.