This commit is contained in:
2024-03-04 23:45:56 +08:00
commit 2e9a13a76c
33 changed files with 4563 additions and 0 deletions

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>
<ItemGroup>
<ClCompile Include="SqList.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{15e9e8ad-d18e-45b2-861a-699717de99bb}</ProjectGuid>
<RootNamespace>My21SqList</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>
<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="SqList.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,156 @@
// 2-1-SqList-顺序表
#include <iostream>//cout,cin
using namespace std;
#include "SqList.h"
char pause;
void dispmenu() {
//显示主菜单
cout << endl;
cout << "*** 顺 序 表 ***\n";
cout << " 1-初始化顺序表\n";
cout << " 2-创建顺序表\n";
cout << " 3-获取第i个元素\n";
cout << " 4-按值查找元素位序\n";
cout << " 5-插入第i个元素\n";
cout << " 6-删除第i个元素\n";
cout << " 7-修改第i个元素\n";
cout << " 8-清空表\n";
cout << " 9-测表长\n";
cout << " 10-测表空\n";
cout << " 11-测表满\n";
cout << " 12-显示表\n";
cout << " 13-按值查找前驱\n";
cout << " 0-退出\n";
}
//主函数
void main() {
int i;
int e, pre_e;
SqList<int> L; //建立容量为20、元素类型为整型的空顺序表
system("cls"); //执行系统命令cls清屏
int choice;
do {
dispmenu(); //显示主菜单
cout << "Enter choice(1~120 退出):";
cin >> choice;
switch (choice) {
case 1: //初始化线性表
cout << "请输入要创建的顺序表的长度:";
cin >> i;
cout << endl;
InitList(L, i);
cout << endl << "创建成功!" << endl;
break;
case 2: //创建线性表
cout << "请输入要创建的元素个数:";
cin >> i;
cout << endl;
CreateList(L, i);
cout << "创建的顺序表元素为:\n"; //显示表元素
DispList(L);
cout << endl;
break;
case 3: //获取第i个元素
cout << "请输入元素序号:";
cin >> i;
cout << endl;
if (GetElem_i(L, i, e))
cout << endl << "" << i << "个元素为:" << e << endl;
else
cout << endl << "元素不存在!" << endl;
break;
case 4: //按值查找
cout << "请输入要查询的元素值:";
cin >> e;
i = LocateElem_e(L, e);
if (i)
cout << endl << e << "是第" << i << "个数据元素" << endl;
else
cout << endl << "不存在此元素!" << endl;
break;
case 5: // 在第i个位置插入元素
cout << "输入插入位置:" << endl;
cin >> i;
cout << "输入插入元素值:" << endl;
cin >> e;
if (InsertElem_i(L, i, e)) {
cout << endl << "插入成功!" << endl;
DispList(L);
}
else
cout << endl << "插入不成功!" << endl;;
break;
case 6: //删除第i个元素
cout << "输入删除元素位置:" << endl;
cin >> i;
if (DeleElem_i(L, i)) {
cout << endl << "删除成功!" << endl;
DispList(L);
}
else
cout << endl << "删除失败!" << endl;
break;
case 7: //修改第i个元素的值
cout << "输入修改元素位置:" << endl;
cin >> i;
cout << "输入新元素值:" << endl;
cin >> e;
if (PutElem(L, i, e)) {
cout << endl << "修改成功!" << endl;
DispList(L);
}
else
cout << endl << "修改失败!" << endl;
break;
case 8: // 清空表
ClearList(L);
break;
case 9: // 测表长
cout << "表长为:" << ListLength(L) << endl;
break;
case 10: //测表空
if (ListEmpty(L))
cout << endl << "空表!" << endl;
else
cout << endl << "不是空表!" << endl;
break;
case 11: // 未表满
if (ListFull(L))
cout << endl << "表满!" << endl;
else
cout << endl << "表不满!" << endl;
break;
case 12: //遍历显示表
DispList(L);
cout << endl;
cin.get(pause);
system("pause");
break;
case 13:
cout << "测试顺序表为\n";
DispList(L);
cout << "输入查找前驱的元素值:\n";
cin >> e;
if (PriorElem_e(L, e, pre_e))
cout << e << "的前驱元素为:" << pre_e << endl;
else
cout << e << "无前驱元素!" << endl;
break;
case 0: //退出
cout << "结束运行bye-bye!" << endl;
break;
default: //无效选择
cout << "无效选择!\n";
break;
}
} while (choice != 0);
}//end of main function

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 = 0; i < L.length; i++) // 顺序查找
if (L.elem[i] == e) // 1.找到
return i + 1; // 返回元素位序
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 = 0; i < L.length; i++) // 依位序输出元素值
{
cout << L.elem[i] << "\t";
}
cout << endl;
}