2024-3-18

This commit is contained in:
2024-03-18 20:18:12 +08:00
parent 8933bff3c7
commit 59e0405033
51 changed files with 5263 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
#include<iostream> //cout,cin
using namespace std;
#include "LinkStack.h"
void dispmenu()
{//显示主菜单
cout << endl;
cout << "1-初始化链栈\n";
cout << "2-元素入栈\n";
cout << "3-元素出栈\n";
cout << "4-取栈顶元素\n";
cout << "5-清空栈\n";
cout << "6-测栈空\n";
cout << "7-显示栈元素\n";
cout << "0-退出\n";
}
//主函数
int main()
{
int e;
SNode<int>* S;
system("cls"); // 清屏
int choice;
do
{
dispmenu(); //显示主菜单
cout << "Enter choice(1~60 退出):";
cin >> choice;
switch (choice)
{
case 1: //初始化链栈
InitStack(S);
cout << endl << "创建成功!" << endl;
break;
case 2: //入栈
cout << "输入要入栈的元素值:" << endl;
cin >> e;
cout << endl;
if (Push(S, e))
{
cout << endl << "入栈成功!栈中元素为:" << endl;
DispStack(S);
}
else
cout << endl << "入栈不成功!" << endl;
break;
case 3: //出栈
if (Pop(S, e))
{
cout << endl << "出栈成功!出栈元素为:" << e << endl;
cout << "出栈后,栈中元素为" << endl;
DispStack(S);
}
else
cout << endl << "栈空,出栈失败!" << endl;
break;
case 4: //获取栈顶元素
if (GetTop(S, e))
{
cout << endl << "栈顶元素为:" << e << endl;
}
else
cout << endl << "栈空!" << endl;
break;
case 5: // 清空栈
DestroyStack(S);
cout << "栈已清空!" << endl;
break;
case 6: //测栈空
if (StackEmpty(S))
cout << endl << "空栈!" << endl;
else
cout << endl << "不是空栈!" << endl;
break;
case 7: //显示栈元素
DispStack(S);
cout << endl;
break;
case 0: //退出
DestroyStack(S);
cout << "结束运行Bye-bye!" << endl;
break;
default: //非法选择
cout << "无效选择!\n";
break;
}
} while (choice != 0);
return 0;
}//end of main function

View File

@@ -0,0 +1,96 @@
template <class DT>
struct SNode // 结点
{
DT data; // 数据域,存储数据元素值
SNode* next; // 指针域,指向下一个结点
};
//【算法3.6】
template <class DT>
bool InitStack(SNode<DT>*& S) // 创建空链栈
{
S = NULL;
return true;
}
//【算法3.7】
template <class DT>
void DestroyStack(SNode<DT>* (&S)) // 释放链栈所占内存
{
SNode<DT>* p;
while (S) //从头结点开始,依次释放结点
{
p = S;
S = S->next;
delete p;
}
S = 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,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>{3682e867-34c4-4beb-8224-fca94badaaf5}</ProjectGuid>
<RootNamespace>LinkStack</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="LinkStack.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="LinkStack.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>