2024-3-14
This commit is contained in:
41
Intersection_Union/Intersection_Union.sln
Normal file
41
Intersection_Union/Intersection_Union.sln
Normal 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
|
||||
253
Intersection_Union/Linked_List/LinkList.h
Normal file
253
Intersection_Union/Linked_List/LinkList.h
Normal 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;
|
||||
}
|
||||
138
Intersection_Union/Linked_List/Linked_List.vcxproj
Normal file
138
Intersection_Union/Linked_List/Linked_List.vcxproj
Normal 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>
|
||||
27
Intersection_Union/Linked_List/Linked_List.vcxproj.filters
Normal file
27
Intersection_Union/Linked_List/Linked_List.vcxproj.filters
Normal 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>
|
||||
85
Intersection_Union/Linked_List/main.cpp
Normal file
85
Intersection_Union/Linked_List/main.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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 << "A∪B: ";
|
||||
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;
|
||||
}
|
||||
@@ -21,8 +21,8 @@
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{9df092c9-0251-46f4-a747-83ad359959a0}</ProjectGuid>
|
||||
<RootNamespace>homework240306</RootNamespace>
|
||||
<ProjectGuid>{b4e4a6f7-1b97-421c-98bb-79482b2743fe}</ProjectGuid>
|
||||
<RootNamespace>SequentialList</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
@@ -127,10 +127,10 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqList.h" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClInclude Include="SqList.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
@@ -14,14 +14,14 @@
|
||||
<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="main.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -66,7 +66,7 @@ int main()
|
||||
Union(A, B);
|
||||
cout << "A∪B: ";
|
||||
DispList(A);
|
||||
A.length = length_a; // Reset A to its original length
|
||||
A.length = length_a; // Reset A to its original length, no need to delete the elements
|
||||
Intersection(A, B);
|
||||
cout << "A∩B: ";
|
||||
DispList(A);
|
||||
@@ -1,31 +0,0 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34322.80
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "homework240306", "homework240306.vcxproj", "{9DF092C9-0251-46F4-A747-83AD359959A0}"
|
||||
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
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x64.Build.0 = Debug|x64
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Debug|x86.Build.0 = Debug|Win32
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x64.ActiveCfg = Release|x64
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x64.Build.0 = Release|x64
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x86.ActiveCfg = Release|Win32
|
||||
{9DF092C9-0251-46F4-A747-83AD359959A0}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {D125820C-4FDB-4C08-9519-7885288F1B5B}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Reference in New Issue
Block a user