2024-3-18
This commit is contained in:
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"iostream": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
69
Chapter3/BracketsMatch/BracketsMatch.cpp
Normal file
69
Chapter3/BracketsMatch/BracketsMatch.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/***链栈实现括号匹配***/
|
||||||
|
#include<string>
|
||||||
|
#include<iostream>
|
||||||
|
using namespace std;
|
||||||
|
#include"LinkStack.h"
|
||||||
|
|
||||||
|
//算法3.11 括号的匹配
|
||||||
|
bool match(string exp)
|
||||||
|
{
|
||||||
|
//检验表达式(表达式以"#"结束)中所含"["和"]"、"("和")"是否匹配,如果匹配,则返回true,否则返回false。
|
||||||
|
//表达式以“#”结束
|
||||||
|
SNode<char>* S;
|
||||||
|
InitStack(S);
|
||||||
|
int flag = 1; // 标记查找结果以控制循环及返回结果
|
||||||
|
char ch;
|
||||||
|
char e, x;
|
||||||
|
int i = 0;
|
||||||
|
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;
|
||||||
|
}//switch
|
||||||
|
ch = exp[i++]; //继续读入下一个字符
|
||||||
|
}//while
|
||||||
|
if (StackEmpty(S) && flag) // 栈空且标志为true,括号匹配,返回true
|
||||||
|
return true;
|
||||||
|
else // 否则,括号不匹配,返回false
|
||||||
|
return false;
|
||||||
|
}//match
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int flag;
|
||||||
|
string exp;
|
||||||
|
cout << "请输入待匹配的表达式,以“#”结束:" << endl;
|
||||||
|
cin >> exp;
|
||||||
|
flag = match(exp);
|
||||||
|
if (flag)
|
||||||
|
cout << "括号匹配成功!" << endl;
|
||||||
|
else
|
||||||
|
cout << "括号匹配失败!" << endl;
|
||||||
|
return 0;
|
||||||
|
}//end of main function
|
||||||
|
|
||||||
138
Chapter3/BracketsMatch/BracketsMatch.vcxproj
Normal file
138
Chapter3/BracketsMatch/BracketsMatch.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>{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>
|
||||||
27
Chapter3/BracketsMatch/BracketsMatch.vcxproj.filters
Normal file
27
Chapter3/BracketsMatch/BracketsMatch.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="LinkStack.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="BracketsMatch.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
92
Chapter3/BracketsMatch/LinkStack.h
Normal file
92
Chapter3/BracketsMatch/LinkStack.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
91
Chapter3/Chapter3.sln
Normal file
91
Chapter3/Chapter3.sln
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
|
||||||
|
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}") = "BracketsMatch", "BracketsMatch\BracketsMatch.vcxproj", "{BC507152-80AB-4A4F-9006-339728D357CA}"
|
||||||
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DancePartner", "DancePartner\DancePartner.vcxproj", "{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}"
|
||||||
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LinkQueue", "LinkQueue\LinkQueue.vcxproj", "{492ABB97-962F-46E9-BB91-6300E9D12898}"
|
||||||
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LinkStack", "LinkStack\LinkStack.vcxproj", "{3682E867-34C4-4BEB-8224-FCA94BADAAF5}"
|
||||||
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SqQueue", "SqQueue\SqQueue.vcxproj", "{6169383D-812E-4602-8711-BBBE639A6601}"
|
||||||
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SqStack", "SqStack\SqStack.vcxproj", "{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}"
|
||||||
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ValExpression", "ValExpression\ValExpression.vcxproj", "{85FF5098-D9C7-4D2E-B248-4C3E402AD373}"
|
||||||
|
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
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Release|x64.Build.0 = Release|x64
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{ADD4A0D1-E0B4-4F29-B92F-F9B6C3AF2A30}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Release|x64.Build.0 = Release|x64
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{492ABB97-962F-46E9-BB91-6300E9D12898}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Release|x64.Build.0 = Release|x64
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{3682E867-34C4-4BEB-8224-FCA94BADAAF5}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Release|x64.Build.0 = Release|x64
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{6169383D-812E-4602-8711-BBBE639A6601}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Release|x64.Build.0 = Release|x64
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{5FB4ADAB-1035-4344-AEC9-72E903AC27A2}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Release|x64.Build.0 = Release|x64
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{85FF5098-D9C7-4D2E-B248-4C3E402AD373}.Release|x86.Build.0 = Release|Win32
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {30AB47D5-FA1A-476B-AB4B-39972632BC13}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
167
Chapter3/DancePartner/DancePartner.cpp
Normal file
167
Chapter3/DancePartner/DancePartner.cpp
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
#include<iostream>
|
||||||
|
#include<string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct dancer // 舞者信息
|
||||||
|
{
|
||||||
|
string name; // 姓名
|
||||||
|
char sex; // 性别
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Node // 队列结点
|
||||||
|
{
|
||||||
|
dancer data; // 数据域
|
||||||
|
Node* next; // 指针域,指向后继
|
||||||
|
}*front, * rear; // 队头、队尾
|
||||||
|
|
||||||
|
struct LinkQueue
|
||||||
|
{
|
||||||
|
Node* front;
|
||||||
|
Node* rear;
|
||||||
|
}; // 男队和女队
|
||||||
|
|
||||||
|
void InitialLinkQueue(LinkQueue& Q) // 初始化队列
|
||||||
|
{
|
||||||
|
Q.front = new Node;
|
||||||
|
Q.front->next = NULL;
|
||||||
|
Q.rear = Q.front;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DestroyLinkQueue(LinkQueue& Q) // 销毁队列
|
||||||
|
{
|
||||||
|
Node* p;
|
||||||
|
while (Q.front != NULL)
|
||||||
|
{
|
||||||
|
p = Q.front;
|
||||||
|
Q.front = Q.front->next;
|
||||||
|
delete p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnQueue(LinkQueue& Q, dancer& e) // 入队
|
||||||
|
{
|
||||||
|
Node* s = new Node;
|
||||||
|
s->data = e;
|
||||||
|
s->next = Q.rear->next;
|
||||||
|
Q.rear->next = s;
|
||||||
|
Q.rear = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsEmpty(LinkQueue Q) // 判队空
|
||||||
|
{
|
||||||
|
if (Q.front == Q.rear)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeQueue(LinkQueue& Q, dancer& e) // 出队
|
||||||
|
{
|
||||||
|
Node* p;
|
||||||
|
if (IsEmpty(Q)) // 队空,
|
||||||
|
{
|
||||||
|
cout << "队列为空,无法出队列!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
p = Q.front->next;
|
||||||
|
e = p->data;
|
||||||
|
Q.front->next = p->next;
|
||||||
|
if (p == Q.rear) // 只有一个元素,出队
|
||||||
|
Q.rear = Q.front; // 修改队尾指针
|
||||||
|
delete p;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GetHead(LinkQueue Q, dancer& e)
|
||||||
|
{
|
||||||
|
if (IsEmpty(Q)) // 队空
|
||||||
|
{
|
||||||
|
cout << "队列为空,无法取得队首元素!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
e = Q.front->next->data; // 返回队头元素
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void QueueTranverse(LinkQueue Q) // 遍历队
|
||||||
|
{
|
||||||
|
Node* p;
|
||||||
|
p = Q.front->next;
|
||||||
|
while (p != NULL)
|
||||||
|
{
|
||||||
|
cout << (p->data).name << " ";
|
||||||
|
p = p->next;
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntranHall(dancer person[], int num) // 舞者入场
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < num; i++)
|
||||||
|
{
|
||||||
|
cout << "请输入第" << i + 1 << "个舞者性别(F(女) or M(男))及姓名:" << endl;
|
||||||
|
cin >> person[i].sex;
|
||||||
|
cin >> person[i].name;
|
||||||
|
}
|
||||||
|
cout << "现有舞者:" << endl;
|
||||||
|
for (i = 0; i < num; i++)
|
||||||
|
{
|
||||||
|
cout << i + 1 << ":" << person[i].sex
|
||||||
|
<< "," << person[i].name << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//算法3.24
|
||||||
|
void DancePartner(dancer person[], int num)
|
||||||
|
{
|
||||||
|
dancer newdancer, m, f, p;
|
||||||
|
LinkQueue GenQueue;
|
||||||
|
LinkQueue LadyQueue;
|
||||||
|
InitialLinkQueue(GenQueue); // 初始化男队
|
||||||
|
InitialLinkQueue(LadyQueue); // 初始化女队
|
||||||
|
for (int i = 0; i < num; i++) // 舞者入场
|
||||||
|
{
|
||||||
|
p = person[i];
|
||||||
|
if (p.sex == 'F')
|
||||||
|
EnQueue(LadyQueue, p);
|
||||||
|
else
|
||||||
|
EnQueue(GenQueue, p);
|
||||||
|
}
|
||||||
|
while ((!IsEmpty(GenQueue)) && (!IsEmpty(LadyQueue))) // 匹配舞者
|
||||||
|
{
|
||||||
|
DeQueue(GenQueue, m); // 女士出队
|
||||||
|
DeQueue(LadyQueue, f); // 男士出队
|
||||||
|
cout << m.name << "<---配对--->" << f.name << endl; // 男、女配队
|
||||||
|
}
|
||||||
|
if (!IsEmpty(GenQueue))
|
||||||
|
{
|
||||||
|
GetHead(GenQueue, m);
|
||||||
|
cout << m.name << "先生还在等着呢!" << endl;
|
||||||
|
}
|
||||||
|
else if (!IsEmpty(LadyQueue))
|
||||||
|
{
|
||||||
|
GetHead(LadyQueue, f);
|
||||||
|
cout << f.name << "女士还在等着呢!" << endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << "配对完美结束!" << endl;
|
||||||
|
DestroyLinkQueue(GenQueue); // 销毁队列
|
||||||
|
DestroyLinkQueue(LadyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
dancer* person;
|
||||||
|
int num;
|
||||||
|
cout << "请输入舞伴总数量:" << endl;
|
||||||
|
cin >> num;
|
||||||
|
person = new dancer[num];
|
||||||
|
EntranHall(person, num); // 舞者入场
|
||||||
|
DancePartner(person, num); // 舞者匹配
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
138
Chapter3/DancePartner/DancePartner.vcxproj
Normal file
138
Chapter3/DancePartner/DancePartner.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>{add4a0d1-e0b4-4f29-b92f-f9b6c3af2a30}</ProjectGuid>
|
||||||
|
<RootNamespace>DancePartner</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="DancePartner.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
27
Chapter3/DancePartner/DancePartner.vcxproj.filters
Normal file
27
Chapter3/DancePartner/DancePartner.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="LinkQueue.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="DancePartner.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
112
Chapter3/DancePartner/LinkQueue.h
Normal file
112
Chapter3/DancePartner/LinkQueue.h
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//【算法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;
|
||||||
|
}
|
||||||
|
|
||||||
108
Chapter3/LinkQueue/LinkQueue.cpp
Normal file
108
Chapter3/LinkQueue/LinkQueue.cpp
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
#include<iostream> //cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include "LinkQueue.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 << "8-显示队列元素\n";
|
||||||
|
cout << "0-退出\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int e;
|
||||||
|
LinkQueue<int> Q;
|
||||||
|
system("cls"); // 执行系统命令cls,清屏
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
dispmenu(); // 显示主菜单
|
||||||
|
cout << "功能选择(1~8,0 退出):";
|
||||||
|
cin >> choice;
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1: // 初始化链队
|
||||||
|
InitQueue(Q);
|
||||||
|
cout << endl << "创建成功!" << endl;
|
||||||
|
break;
|
||||||
|
case 2: //入队
|
||||||
|
cout << "输入要插入的元素值:" << endl;
|
||||||
|
cin >> e;
|
||||||
|
cout << endl;
|
||||||
|
if (EnQueue(Q, e))
|
||||||
|
{
|
||||||
|
cout << endl << "入队成功!队列元素为:" << endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "入队不成功!" << endl;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3: // 出队
|
||||||
|
if (DeQueue(Q, e))
|
||||||
|
{
|
||||||
|
cout << endl << "出队成功!出队元素为:" << e << endl;
|
||||||
|
cout << endl << "出队后,队列元素为:" << endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "队空,出队失败!" << endl;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4: // 获取队头元素
|
||||||
|
if (GetHead(Q, e))
|
||||||
|
{
|
||||||
|
cout << "队列元素为:" << endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
cout << endl << "队头元素为:" << e << endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "队空!" << endl;
|
||||||
|
break;
|
||||||
|
case 5: // 获取队尾元素
|
||||||
|
if (GetTail(Q, e))
|
||||||
|
{
|
||||||
|
cout << "队列元素为:" << endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
cout << endl << "队尾元素为:" << e << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "队空!" << endl;
|
||||||
|
break;
|
||||||
|
case 6: // 清空队
|
||||||
|
ClearQueue(Q);
|
||||||
|
cout << endl << "队已空!" << endl;
|
||||||
|
case 7: // 测队空
|
||||||
|
if (QueueEmpty(Q))
|
||||||
|
cout << endl << "空队!" << endl;
|
||||||
|
else
|
||||||
|
cout << endl << "不是空队!" << endl;
|
||||||
|
break;
|
||||||
|
case 8: // 查看队列元素
|
||||||
|
DispQueue(Q);
|
||||||
|
cout << endl;
|
||||||
|
break;
|
||||||
|
case 0: // 退出
|
||||||
|
DestroyQueue(Q);
|
||||||
|
cout << "结束运行Bye-bye!" << endl;
|
||||||
|
break;
|
||||||
|
default: // 无效选择
|
||||||
|
cout << "无效选择!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (choice != 0);
|
||||||
|
return 0;
|
||||||
|
}//end of main function
|
||||||
127
Chapter3/LinkQueue/LinkQueue.h
Normal file
127
Chapter3/LinkQueue/LinkQueue.h
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
138
Chapter3/LinkQueue/LinkQueue.vcxproj
Normal file
138
Chapter3/LinkQueue/LinkQueue.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>{492abb97-962f-46e9-bb91-6300e9d12898}</ProjectGuid>
|
||||||
|
<RootNamespace>LinkQueue</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="LinkQueue.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
27
Chapter3/LinkQueue/LinkQueue.vcxproj.filters
Normal file
27
Chapter3/LinkQueue/LinkQueue.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="LinkQueue.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="LinkQueue.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
93
Chapter3/LinkStack/LinkStack.cpp
Normal file
93
Chapter3/LinkStack/LinkStack.cpp
Normal 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~6,0 退出):";
|
||||||
|
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
|
||||||
96
Chapter3/LinkStack/LinkStack.h
Normal file
96
Chapter3/LinkStack/LinkStack.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
138
Chapter3/LinkStack/LinkStack.vcxproj
Normal file
138
Chapter3/LinkStack/LinkStack.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>{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>
|
||||||
27
Chapter3/LinkStack/LinkStack.vcxproj.filters
Normal file
27
Chapter3/LinkStack/LinkStack.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="LinkStack.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="LinkStack.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
126
Chapter3/SqQueue/SqQueue.cpp
Normal file
126
Chapter3/SqQueue/SqQueue.cpp
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
|
||||||
|
#include<iostream>//cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include "SqQueue.h"
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
|
||||||
|
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 << "8-测队满\n";
|
||||||
|
cout << "9-显示队列元素\n";
|
||||||
|
cout << "10-显示队头、队尾指针\n";
|
||||||
|
cout << "0-退出\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int e;
|
||||||
|
SqQueue<int> Q; // 元素类型为整型的顺序队列
|
||||||
|
system("cls"); // 清屏
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
dispmenu(); //显示主菜单
|
||||||
|
cout << "功能选择(1~10,0 退出!):";
|
||||||
|
cin >> choice;
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1: // 初始化顺序队列
|
||||||
|
cout << "请输入要创建的顺序队列的长度";
|
||||||
|
cin >> i;
|
||||||
|
cout << endl;
|
||||||
|
InitQueue(Q, i);
|
||||||
|
cout << endl << "创建成功!" << endl;
|
||||||
|
break;
|
||||||
|
case 2: // 入队
|
||||||
|
cout << "输入要入队的元素值:" << endl;
|
||||||
|
cin >> e;
|
||||||
|
cout << endl;
|
||||||
|
if (EnQueue(Q, e))
|
||||||
|
{
|
||||||
|
cout << endl << "入队成功!入队后队列元素为:" << endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "队满,不能入队!" << endl;
|
||||||
|
break;
|
||||||
|
case 3: // 出队
|
||||||
|
if (DeQueue(Q, e))
|
||||||
|
{
|
||||||
|
cout << endl << "队列元素为:";
|
||||||
|
DispQueue(Q);
|
||||||
|
cout << endl << "出队元素为:" << e << endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "队空,不能出队!" << endl;
|
||||||
|
break;
|
||||||
|
case 4: // 取队头元素
|
||||||
|
if (GetHead(Q, e))
|
||||||
|
{
|
||||||
|
cout << endl << "队列元素为:";
|
||||||
|
DispQueue(Q);
|
||||||
|
cout << endl << "队头元素为:" << e << endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "队空!" << endl;
|
||||||
|
break;
|
||||||
|
case 5: // 取队尾元素
|
||||||
|
if (GetTail(Q, e))
|
||||||
|
{
|
||||||
|
cout << endl << "队列元素为:";
|
||||||
|
DispQueue(Q);
|
||||||
|
cout << endl << "队尾元素为:" << e << endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "队空!无数据元素" << endl;
|
||||||
|
break;
|
||||||
|
case 6: // 清空队
|
||||||
|
ClearQueue(Q);
|
||||||
|
cout << "队已清空!" << endl;
|
||||||
|
break;
|
||||||
|
case 7: // 测队空
|
||||||
|
if (QueueEmpty(Q))
|
||||||
|
cout << endl << "空队!" << endl;
|
||||||
|
else
|
||||||
|
cout << endl << "非空队!" << endl;
|
||||||
|
break;
|
||||||
|
case 8: // 测队满
|
||||||
|
if (QueueFull(Q))
|
||||||
|
cout << endl << "满队!" << endl;
|
||||||
|
else
|
||||||
|
cout << endl << "非满队!" << endl;
|
||||||
|
break;
|
||||||
|
case 9: // 显示队列元素
|
||||||
|
DispQueue(Q);
|
||||||
|
cout << endl;
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
cout << "\nQ.fornt=" << Q.front << endl;
|
||||||
|
cout << "Q.rear=" << Q.rear << endl;
|
||||||
|
break;
|
||||||
|
case 0: // 退出
|
||||||
|
DestroyQueue(Q);
|
||||||
|
cout << "结束运行Bye-bye!" << endl;
|
||||||
|
break;
|
||||||
|
default: // 无效选择
|
||||||
|
cout << "无效选择!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (choice != 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}//end of main function
|
||||||
|
|
||||||
135
Chapter3/SqQueue/SqQueue.vcxproj
Normal file
135
Chapter3/SqQueue/SqQueue.vcxproj
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
<?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>{6169383d-812e-4602-8711-bbbe639a6601}</ProjectGuid>
|
||||||
|
<RootNamespace>SqQueue</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="SqQueue.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
22
Chapter3/SqQueue/SqQueue.vcxproj.filters
Normal file
22
Chapter3/SqQueue/SqQueue.vcxproj.filters
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?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="SqQueue.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
113
Chapter3/SqStack/SqStack.cpp
Normal file
113
Chapter3/SqStack/SqStack.cpp
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
|
||||||
|
#include<iostream>//cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include "SqStack.h"
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
|
||||||
|
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 << "8-显示栈元素\n";
|
||||||
|
cout << "9-查看栈顶指针\n";
|
||||||
|
cout << "0-退出\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int e;
|
||||||
|
SqStack<int> S;//建立容量为20、元素类型为整型的空顺序栈
|
||||||
|
system("cls"); // 清屏
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
dispmenu(); // 显示主菜单
|
||||||
|
cout << "Enter choice(1~6,0 退出):";
|
||||||
|
cin >> choice;
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1: // 初始化顺序栈
|
||||||
|
cout << "请输入要创建的顺序栈的长度";
|
||||||
|
cin >> i;
|
||||||
|
cout << endl;
|
||||||
|
InitStack(S, i);
|
||||||
|
cout << endl << "创建成功!" << endl;
|
||||||
|
break;
|
||||||
|
case 2: // 入栈
|
||||||
|
cout << "输入要入栈的元素值:" << endl;
|
||||||
|
cin >> e;
|
||||||
|
if (Push(S, e))
|
||||||
|
{
|
||||||
|
cout << "入栈成功!入栈后栈元素为:" << endl;
|
||||||
|
DispStack(S);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << endl << "栈满,不能入栈!" << endl;
|
||||||
|
break;
|
||||||
|
case 3: // 出栈
|
||||||
|
if (Pop(S, e))
|
||||||
|
{
|
||||||
|
cout << "出栈成功!出栈元素为:" << 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: // 清空栈
|
||||||
|
ClearStack(S);
|
||||||
|
cout << endl << "栈已空!" << endl;
|
||||||
|
break;
|
||||||
|
case 6: // 测栈空
|
||||||
|
if (StackEmpty(S))
|
||||||
|
cout << endl << "空栈!" << endl;
|
||||||
|
else
|
||||||
|
cout << endl << "不是空栈!" << endl;
|
||||||
|
break;
|
||||||
|
case 7: // 测栈满
|
||||||
|
if (StackFull(S))
|
||||||
|
cout << endl << "栈满!" << endl;
|
||||||
|
else
|
||||||
|
cout << endl << "栈不满!" << endl;
|
||||||
|
break;
|
||||||
|
case 8: // 显示栈元素
|
||||||
|
DispStack(S);
|
||||||
|
cout << endl;
|
||||||
|
break;
|
||||||
|
case 9: // 显示栈顶指针
|
||||||
|
cout << "栈顶指针top= " << S.top << endl;
|
||||||
|
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
|
||||||
|
|
||||||
107
Chapter3/SqStack/SqStack.h
Normal file
107
Chapter3/SqStack/SqStack.h
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
template <class DT>
|
||||||
|
struct SqStack // 顺序栈
|
||||||
|
{
|
||||||
|
DT* base; // 栈首址
|
||||||
|
int top; // 栈顶指针
|
||||||
|
int stacksize; // 栈容量
|
||||||
|
};
|
||||||
|
|
||||||
|
//基本操作的实现
|
||||||
|
//【算法3.1】 // 初始化栈
|
||||||
|
template <class DT>
|
||||||
|
void InitStack(SqStack<DT>& S, int m)
|
||||||
|
{
|
||||||
|
S.base = new DT[m]; // 申请栈空间
|
||||||
|
if (S.base == NULL) // 申请失败,退出
|
||||||
|
{
|
||||||
|
cout << "未创建成功!";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
S.top = -1; // 设置空栈属性
|
||||||
|
S.stacksize = 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, DT e)
|
||||||
|
{
|
||||||
|
if (S.top == S.stacksize - 1) // 栈满,不能入栈
|
||||||
|
return false; // 返回false
|
||||||
|
S.top++;
|
||||||
|
S.base[S.top] = e;
|
||||||
|
return true; // 入栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
//【算法3.4】 // 出栈
|
||||||
|
template<class DT>
|
||||||
|
bool Pop(SqStack<DT>& S, DT& e)
|
||||||
|
{
|
||||||
|
if (S.top == -1) // 栈空
|
||||||
|
return false; // 返回false
|
||||||
|
e = S.base[S.top]; // 取栈顶元素
|
||||||
|
S.top--; // 栈顶指针下移
|
||||||
|
return true; // 出栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//【算法3.5】 // 获取栈顶元素
|
||||||
|
template<class DT>
|
||||||
|
bool GetTop(SqStack<DT> S, DT& e)
|
||||||
|
{
|
||||||
|
if (S.top == -1) // 栈空
|
||||||
|
return false; // 返回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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//显示栈内容
|
||||||
|
template<class DT>
|
||||||
|
void DispStack(SqStack<DT> S)
|
||||||
|
{
|
||||||
|
int i = S.top;
|
||||||
|
while (i >= 0)
|
||||||
|
{
|
||||||
|
cout << S.base[i--] << "\t";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class DT>
|
||||||
|
void ClearStack(SqStack<DT>& S)
|
||||||
|
{
|
||||||
|
S.top = -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class DT>
|
||||||
|
int StackFull(SqStack<DT> S)
|
||||||
|
{
|
||||||
|
if (S.top == S.stacksize - 1)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
138
Chapter3/SqStack/SqStack.vcxproj
Normal file
138
Chapter3/SqStack/SqStack.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>{5fb4adab-1035-4344-aec9-72e903ac27a2}</ProjectGuid>
|
||||||
|
<RootNamespace>SqStack</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="SqStack.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="SqStack.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
27
Chapter3/SqStack/SqStack.vcxproj.filters
Normal file
27
Chapter3/SqStack/SqStack.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="SqStack.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="SqStack.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
282
Chapter3/ValExpression/Expresion.cpp
Normal file
282
Chapter3/ValExpression/Expresion.cpp
Normal file
@@ -0,0 +1,282 @@
|
|||||||
|
#include<iostream> //cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include"SqStack.h"
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
char Precede(char t1, char t2) //算符的优先级比较
|
||||||
|
{
|
||||||
|
char f;
|
||||||
|
switch (t2)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
case '-':if (t1 == '(' || t1 == '=')
|
||||||
|
f = '<';
|
||||||
|
else
|
||||||
|
f = '>';
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
case '/':if (t1 == '*' || t1 == '/' || t1 == ')')
|
||||||
|
f = '>';
|
||||||
|
else
|
||||||
|
f = '<';
|
||||||
|
break;
|
||||||
|
case '(':if (t1 == ')')
|
||||||
|
{
|
||||||
|
cout << "ERROR1" << endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
f = '<';
|
||||||
|
break;
|
||||||
|
case ')':switch (t1)
|
||||||
|
{
|
||||||
|
case '(':f = '=';
|
||||||
|
break;
|
||||||
|
case '=':cout << "ERROR2" << endl;
|
||||||
|
exit(0);
|
||||||
|
default: f = '>';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '=':switch (t1)
|
||||||
|
{
|
||||||
|
case '=':f = '=';
|
||||||
|
break;
|
||||||
|
case '(':cout << "ERROR2" << endl;
|
||||||
|
exit(0);
|
||||||
|
default: f = '>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool In(char ch) // 判断 ch 是否为运算符
|
||||||
|
{
|
||||||
|
switch (ch)
|
||||||
|
{
|
||||||
|
case'+':
|
||||||
|
case'-':
|
||||||
|
case'*':
|
||||||
|
case'/':
|
||||||
|
case'(':
|
||||||
|
case')':
|
||||||
|
case'=':return true;
|
||||||
|
default:return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float Operate(float a, char theta, float b)
|
||||||
|
{ // 实施一次运算
|
||||||
|
float ch;
|
||||||
|
switch (theta)
|
||||||
|
{
|
||||||
|
case'+':ch = a + b;
|
||||||
|
break;
|
||||||
|
case'-':ch = a - b;
|
||||||
|
break;
|
||||||
|
case'*':ch = a * b;
|
||||||
|
break;
|
||||||
|
case'/':ch = a / b;
|
||||||
|
}
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
//算法3.12
|
||||||
|
float Val_Exp(char* exp) // 中缀表达式求值。
|
||||||
|
{
|
||||||
|
SqStack<char> OP; // 运算符栈
|
||||||
|
SqStack<float> OD; // 运算数栈
|
||||||
|
InitStack(OP, 30);
|
||||||
|
InitStack(OD, 30);
|
||||||
|
char theta;
|
||||||
|
float a, b, result;
|
||||||
|
char ch, x; // 存放由键盘接收的字符
|
||||||
|
char z[6]; // 存放符点数字符串
|
||||||
|
int i;
|
||||||
|
Push(OP, '='); // # 是表达式结束标志
|
||||||
|
ch = *exp++;
|
||||||
|
GetTop(OP, x);
|
||||||
|
while (ch != '=' || x != '=')
|
||||||
|
{
|
||||||
|
if (ch >= '0' && ch <= '9' || ch == '.') // ch 是操作数
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
z[i] = ch;
|
||||||
|
i++;
|
||||||
|
ch = *exp++;
|
||||||
|
} while (ch >= '0' && ch <= '9' || ch == '.');
|
||||||
|
z[i] = '\0';
|
||||||
|
result = atof(z); // 将字符串数组转为符点型存于result
|
||||||
|
Push(OD, result);
|
||||||
|
}
|
||||||
|
else if (In(ch)) // 是7种运算符之一
|
||||||
|
switch (Precede(x, ch))
|
||||||
|
{
|
||||||
|
case'<':Push(OP, ch); // 栈顶元素优先权低
|
||||||
|
ch = *exp++;
|
||||||
|
break;
|
||||||
|
case'=':Pop(OP, x); // 脱括号并接收下一字符
|
||||||
|
ch = *exp++;
|
||||||
|
break;
|
||||||
|
case'>':Pop(OP, theta); // 退栈并将运算结果入栈
|
||||||
|
Pop(OD, b);
|
||||||
|
Pop(OD, a);
|
||||||
|
Push(OD, Operate(a, theta, b));
|
||||||
|
}
|
||||||
|
else // ch是非法字符
|
||||||
|
{
|
||||||
|
cout << "ERROR3" << endl;;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
GetTop(OP, x);
|
||||||
|
}
|
||||||
|
GetTop(OD, result);
|
||||||
|
DestroyStack(OP);
|
||||||
|
DestroyStack(OD);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreatePostExp(char* exp, char*& postexp) // 由中缀式求后缀式
|
||||||
|
{
|
||||||
|
char ch, x;
|
||||||
|
int i = 0;
|
||||||
|
SqStack<char> OP;
|
||||||
|
InitStack(OP, 30);
|
||||||
|
Push(OP, '='); // # 是表达式结束标志
|
||||||
|
cout << "中缀表达式:" << exp << endl;
|
||||||
|
ch = *exp++;
|
||||||
|
while (ch)
|
||||||
|
{
|
||||||
|
if ((ch >= '0' && ch <= '9') || ch == '.')
|
||||||
|
{
|
||||||
|
postexp[i++] = ch;
|
||||||
|
ch = *exp++;
|
||||||
|
}
|
||||||
|
if (In(ch)) // 是7种运算符之一
|
||||||
|
{
|
||||||
|
postexp[i++] = ' ';
|
||||||
|
GetTop(OP, x);
|
||||||
|
switch (Precede(x, ch))
|
||||||
|
{
|
||||||
|
case'<':Push(OP, ch); // 栈顶元素优先权低
|
||||||
|
ch = *exp++;
|
||||||
|
break;
|
||||||
|
case'=':Pop(OP, x); // 脱括号并接收下一字符
|
||||||
|
ch = *exp++;
|
||||||
|
break;
|
||||||
|
case'>':
|
||||||
|
Pop(OP, postexp[i++]); // 运算符出栈输出
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
postexp[i] = '\0';
|
||||||
|
}//while
|
||||||
|
cout << "\n后缀表达式为:" << postexp << endl;
|
||||||
|
DestroyStack(OP);
|
||||||
|
}
|
||||||
|
|
||||||
|
//算法3.13
|
||||||
|
float Val_PostExp(char* postexp) // 后缀表达式求值
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char z[6];
|
||||||
|
float result = 0, d = 0, a, b;
|
||||||
|
char ch;
|
||||||
|
SqStack<float> OD;
|
||||||
|
InitStack(OD, 30);
|
||||||
|
ch = *postexp++;
|
||||||
|
while (ch != '\0')
|
||||||
|
{
|
||||||
|
|
||||||
|
if (ch >= '0' && ch <= '9' || ch == '.') // ch为操作数符号
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
z[i++] = ch;
|
||||||
|
ch = *postexp++; // 取下一个操作数符号
|
||||||
|
} while (ch >= '0' && ch <= '9' || ch == '.');
|
||||||
|
z[i] = '\0';
|
||||||
|
d = atof(z); // 将字符串数组转为符点型存于result
|
||||||
|
Push(OD, d); // 操作数进栈
|
||||||
|
}
|
||||||
|
if (In(ch)) // ch为运算符
|
||||||
|
{
|
||||||
|
Pop(OD, a);
|
||||||
|
Pop(OD, b);
|
||||||
|
Push(OD, Operate(b, ch, a));
|
||||||
|
ch = *postexp++;
|
||||||
|
}
|
||||||
|
ch = *postexp++;
|
||||||
|
}
|
||||||
|
Pop(OD, result);
|
||||||
|
DestroyStack(OD);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
//int i;
|
||||||
|
char exp[20] = "(2.2+5)+4*(5-3.1)=";
|
||||||
|
char* postexp;
|
||||||
|
postexp = new char[30];
|
||||||
|
*postexp = '\0';
|
||||||
|
float v;
|
||||||
|
system("cls"); // 清屏
|
||||||
|
cout << "\n缺省表达式:" << exp;
|
||||||
|
cout << endl;
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{ // 显示主菜单
|
||||||
|
cout << endl;
|
||||||
|
cout << "1-创建表达式\n";
|
||||||
|
cout << "2-表达式求值\n";
|
||||||
|
cout << "3-求后缀表达式\n";
|
||||||
|
cout << "4-后缀表达式求值\n";
|
||||||
|
cout << "5-显示表达式\n";
|
||||||
|
cout << "0-退出\n";
|
||||||
|
cout << "输入功能选项(1~5,0 退出):\n";
|
||||||
|
cin >> choice;
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1: // 创建表达式
|
||||||
|
cout << "\n请输入表达式,以=结束" << endl;
|
||||||
|
cin >> exp;
|
||||||
|
break;
|
||||||
|
case 2: // 表达式求值
|
||||||
|
v = Val_Exp(exp);
|
||||||
|
cout << exp;
|
||||||
|
cout << v << endl;
|
||||||
|
break;
|
||||||
|
case 3: // 求后缀表达式
|
||||||
|
CreatePostExp(exp, postexp);
|
||||||
|
break;
|
||||||
|
case 4: // 后缀表达式求值
|
||||||
|
v = Val_PostExp(postexp);
|
||||||
|
cout << '\n' << postexp << "=" << v << endl;
|
||||||
|
break;
|
||||||
|
case 5: // 显示表达式
|
||||||
|
cout << endl;
|
||||||
|
cout << "\n已创建的表达式为:";
|
||||||
|
cout << exp << endl;
|
||||||
|
if (strlen(postexp))
|
||||||
|
{
|
||||||
|
cout << "\n后缀表达式为:";
|
||||||
|
cout << postexp << endl;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0: // 退出
|
||||||
|
cout << "\n结束运行,Bye-Bye!" << endl;
|
||||||
|
break;
|
||||||
|
default://
|
||||||
|
cout << "\n无效选择!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (choice != 0);
|
||||||
|
}//end main
|
||||||
89
Chapter3/ValExpression/SqStack.h
Normal file
89
Chapter3/ValExpression/SqStack.h
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
template <class DT>
|
||||||
|
struct SqStack // 顺序栈
|
||||||
|
{
|
||||||
|
DT* base; // 栈首址
|
||||||
|
int top; // 栈顶指针
|
||||||
|
int stacksize; // 栈容量
|
||||||
|
};
|
||||||
|
|
||||||
|
//基本操作的实现
|
||||||
|
//【算法3.1】 // 初始化栈
|
||||||
|
template <class DT>
|
||||||
|
void InitStack(SqStack<DT>& S, int m)
|
||||||
|
{
|
||||||
|
S.base = new DT[m]; // 申请栈空间
|
||||||
|
if (S.base == NULL) // 申请失败,退出
|
||||||
|
{
|
||||||
|
cout << "未创建成功!";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
S.top = -1; // 设置空栈属性
|
||||||
|
S.stacksize = 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, DT e)
|
||||||
|
{
|
||||||
|
if (S.top == S.stacksize - 1) // 栈满,不能入栈
|
||||||
|
return false; // 返回false
|
||||||
|
S.top++;
|
||||||
|
S.base[S.top] = e;
|
||||||
|
return true; // 入栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
//【算法3.4】 // 出栈
|
||||||
|
template<class DT>
|
||||||
|
bool Pop(SqStack<DT>& S, DT& e)
|
||||||
|
{
|
||||||
|
if (S.top == -1) // 栈空
|
||||||
|
return false; // 返回false
|
||||||
|
e = S.base[S.top]; // 取栈顶元素
|
||||||
|
S.top--; // 栈顶指针下移
|
||||||
|
return true; // 出栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//【算法3.5】 // 获取栈顶元素
|
||||||
|
template<class DT>
|
||||||
|
bool GetTop(SqStack<DT> S, DT& e)
|
||||||
|
{
|
||||||
|
if (S.top == -1) // 栈空
|
||||||
|
return false; // 返回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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//显示栈内容
|
||||||
|
template<class DT>
|
||||||
|
void DispStack(SqStack<DT> S)
|
||||||
|
{
|
||||||
|
int i = S.top;
|
||||||
|
while (i >= 0)
|
||||||
|
{
|
||||||
|
cout << S.base[i--] << "\t";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
138
Chapter3/ValExpression/ValExpression.vcxproj
Normal file
138
Chapter3/ValExpression/ValExpression.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>{85ff5098-d9c7-4d2e-b248-4c3e402ad373}</ProjectGuid>
|
||||||
|
<RootNamespace>ValExpression</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="SqStack.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Expresion.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
27
Chapter3/ValExpression/ValExpression.vcxproj.filters
Normal file
27
Chapter3/ValExpression/ValExpression.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="SqStack.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Expresion.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
41
Del_e_in_Linear_List/Del_e_in_Linear_List.sln
Normal file
41
Del_e_in_Linear_List/Del_e_in_Linear_List.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", "{5C50B77F-47BA-4F08-8881-E89E1C0735D3}"
|
||||||
|
EndProject
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Linked_List", "Linked_List\Linked_List.vcxproj", "{E36A0D17-BE4F-4839-8226-C3D568D00AB3}"
|
||||||
|
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
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Release|x64.Build.0 = Release|x64
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{5C50B77F-47BA-4F08-8881-E89E1C0735D3}.Release|x86.Build.0 = Release|Win32
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Release|x64.Build.0 = Release|x64
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{E36A0D17-BE4F-4839-8226-C3D568D00AB3}.Release|x86.Build.0 = Release|Win32
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {3FAF92A0-59FA-4D3A-BE9A-5A96D63DD9CC}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
253
Del_e_in_Linear_List/Linked_List/LinkList.h
Normal file
253
Del_e_in_Linear_List/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
Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj
Normal file
138
Del_e_in_Linear_List/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>{e36a0d17-be4f-4839-8226-c3d568d00ab3}</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
Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj.filters
Normal file
27
Del_e_in_Linear_List/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>
|
||||||
34
Del_e_in_Linear_List/Linked_List/main.cpp
Normal file
34
Del_e_in_Linear_List/Linked_List/main.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
#include "LinkList.h"
|
||||||
|
template <class DT>
|
||||||
|
void DeleElem_e(LNode<DT>*& L, int e)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= L.length; i++)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
GetElem_i(L, i, n);
|
||||||
|
if (e == n) DeleElem_i(L, i--);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
LNode<int>* L;
|
||||||
|
InitList(L);
|
||||||
|
int length;
|
||||||
|
cout << "请输入要创建的元素个数:";
|
||||||
|
cin >> length;
|
||||||
|
CreateList_1(L, length);
|
||||||
|
cout << "创建的顺序表元素为:\n";
|
||||||
|
DispList(L);
|
||||||
|
cout << endl;
|
||||||
|
int e;
|
||||||
|
cout << "请输入要删除的元素的值:";
|
||||||
|
cin >> e;
|
||||||
|
DeleElem_e(L, e);
|
||||||
|
cout << "删除后的顺序表元素为:\n";
|
||||||
|
DispList(L);
|
||||||
|
cout << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
138
Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj
Normal file
138
Del_e_in_Linear_List/Sequential_List/Sequential_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>{5c50b77f-47ba-4f08-8881-e89e1c0735d3}</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>
|
||||||
|
<ClInclude Include="SqList.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="main.cpp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
||||||
@@ -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="SqList.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="main.cpp">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
175
Del_e_in_Linear_List/Sequential_List/SqList.h
Normal file
175
Del_e_in_Linear_List/Sequential_List/SqList.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
34
Del_e_in_Linear_List/Sequential_List/main.cpp
Normal file
34
Del_e_in_Linear_List/Sequential_List/main.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
#include "SqList.h"
|
||||||
|
template <class DT>
|
||||||
|
void DeleElem_e(SqList<DT>& L, int e)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= L.length; i++)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
GetElem_i(L, i, n);
|
||||||
|
if (e == n) DeleElem_i(L, i--);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
SqList<int> L;
|
||||||
|
InitList(L, 32);
|
||||||
|
cout << "请输入要创建的元素个数:";
|
||||||
|
cin >> i;
|
||||||
|
cout << endl;
|
||||||
|
CreateList(L, i);
|
||||||
|
cout << "创建的顺序表元素为:\n";
|
||||||
|
DispList(L);
|
||||||
|
cout << endl;
|
||||||
|
int e;
|
||||||
|
cout << "请输入要删除的元素的值:";
|
||||||
|
cin >> e;
|
||||||
|
DeleElem_e(L, e);
|
||||||
|
cout << "删除后的顺序表元素为:\n";
|
||||||
|
DispList(L);
|
||||||
|
cout << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
69
OrigFiles/3-特殊线性表/3-BracketsMatch(括号匹配)/BracketsMatch.cpp
Normal file
69
OrigFiles/3-特殊线性表/3-BracketsMatch(括号匹配)/BracketsMatch.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/***链栈实现括号匹配***/
|
||||||
|
#include<string>
|
||||||
|
#include<iostream>
|
||||||
|
using namespace std;
|
||||||
|
#include"LinkStack.h"
|
||||||
|
|
||||||
|
//算法3.11 括号的匹配
|
||||||
|
bool match(string exp)
|
||||||
|
{
|
||||||
|
//检验表达式(表达式以"#"结束)中所含"["和"]"、"("和")"是否匹配,如果匹配,则返回true,否则返回false。
|
||||||
|
//表达式以“#”结束
|
||||||
|
SNode<char> *S;
|
||||||
|
InitStack(S);
|
||||||
|
int flag=1; // 标记查找结果以控制循环及返回结果
|
||||||
|
char ch;
|
||||||
|
char e,x;
|
||||||
|
int i=0;
|
||||||
|
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;
|
||||||
|
}//switch
|
||||||
|
ch=exp[i++]; //继续读入下一个字符
|
||||||
|
}//while
|
||||||
|
if (StackEmpty(S) && flag ) // 栈空且标志为true,括号匹配,返回true
|
||||||
|
return true;
|
||||||
|
else // 否则,括号不匹配,返回false
|
||||||
|
return false;
|
||||||
|
}//match
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int flag;
|
||||||
|
string exp;
|
||||||
|
cout<<"请输入待匹配的表达式,以“#”结束:"<<endl;
|
||||||
|
cin>>exp;
|
||||||
|
flag = match(exp);
|
||||||
|
if(flag)
|
||||||
|
cout<<"括号匹配成功!"<<endl;
|
||||||
|
else
|
||||||
|
cout<<"括号匹配失败!"<<endl;
|
||||||
|
return 0;
|
||||||
|
}//end of main function
|
||||||
|
|
||||||
92
OrigFiles/3-特殊线性表/3-BracketsMatch(括号匹配)/LinkStack.h
Normal file
92
OrigFiles/3-特殊线性表/3-BracketsMatch(括号匹配)/LinkStack.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
158
OrigFiles/3-特殊线性表/3-DancePartner(舞伴问题)/DancePartner.cpp
Normal file
158
OrigFiles/3-特殊线性表/3-DancePartner(舞伴问题)/DancePartner.cpp
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
#include<iostream>
|
||||||
|
#include<string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct dancer // 舞者信息
|
||||||
|
{ string name; // 姓名
|
||||||
|
char sex; // 性别
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Node // 队列结点
|
||||||
|
{ dancer data; // 数据域
|
||||||
|
Node* next; // 指针域,指向后继
|
||||||
|
}*front,*rear; // 队头、队尾
|
||||||
|
|
||||||
|
struct LinkQueue
|
||||||
|
{ Node *front;
|
||||||
|
Node *rear;
|
||||||
|
}; // 男队和女队
|
||||||
|
|
||||||
|
void InitialLinkQueue(LinkQueue &Q) // 初始化队列
|
||||||
|
{
|
||||||
|
Q.front=new Node;
|
||||||
|
Q.front->next=NULL;
|
||||||
|
Q.rear=Q.front;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DestroyLinkQueue(LinkQueue &Q) // 销毁队列
|
||||||
|
{ Node *p;
|
||||||
|
while(Q.front!=NULL)
|
||||||
|
{ p=Q.front;
|
||||||
|
Q.front=Q.front->next;
|
||||||
|
delete p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnQueue(LinkQueue &Q,dancer &e) // 入队
|
||||||
|
{
|
||||||
|
Node *s=new Node;
|
||||||
|
s->data=e;
|
||||||
|
s->next=Q.rear->next;
|
||||||
|
Q.rear->next=s;
|
||||||
|
Q.rear=s;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsEmpty(LinkQueue Q) // 判队空
|
||||||
|
{
|
||||||
|
if (Q.front==Q.rear)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DeQueue(LinkQueue &Q,dancer &e) // 出队
|
||||||
|
{
|
||||||
|
Node *p;
|
||||||
|
if (IsEmpty(Q)) // 队空,
|
||||||
|
{
|
||||||
|
cout<<"队列为空,无法出队列!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
p=Q.front->next;
|
||||||
|
e=p->data;
|
||||||
|
Q.front->next=p->next;
|
||||||
|
if (p==Q.rear) // 只有一个元素,出队
|
||||||
|
Q.rear=Q.front; // 修改队尾指针
|
||||||
|
delete p;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GetHead(LinkQueue Q,dancer &e)
|
||||||
|
{ if(IsEmpty(Q)) // 队空
|
||||||
|
{
|
||||||
|
cout<< "队列为空,无法取得队首元素!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
e=Q.front->next->data; // 返回队头元素
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void QueueTranverse(LinkQueue Q) // 遍历队
|
||||||
|
{ Node *p;
|
||||||
|
p=Q.front->next;
|
||||||
|
while(p!=NULL)
|
||||||
|
{ cout<<(p->data).name<<" ";
|
||||||
|
p=p->next;
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void EntranHall(dancer person[],int num) // 舞者入场
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for(i=0;i<num;i++)
|
||||||
|
{
|
||||||
|
cout<<"请输入第"<<i+1<<"个舞者性别(F(女) or M(男))及姓名:"<<endl;
|
||||||
|
cin>>person[i].sex;
|
||||||
|
cin>>person[i].name;
|
||||||
|
}
|
||||||
|
cout<<"现有舞者:"<<endl;
|
||||||
|
for(i=0;i<num;i++)
|
||||||
|
{
|
||||||
|
cout<<i+1<<":"<<person[i].sex
|
||||||
|
<<","<<person[i].name<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//算法3.24
|
||||||
|
void DancePartner(dancer person[],int num)
|
||||||
|
{
|
||||||
|
dancer newdancer,m,f,p;
|
||||||
|
LinkQueue GenQueue;
|
||||||
|
LinkQueue LadyQueue;
|
||||||
|
InitialLinkQueue(GenQueue); // 初始化男队
|
||||||
|
InitialLinkQueue(LadyQueue); // 初始化女队
|
||||||
|
for(int i=0;i<num;i++) // 舞者入场
|
||||||
|
{ p=person[i];
|
||||||
|
if(p.sex=='F')
|
||||||
|
EnQueue(LadyQueue,p);
|
||||||
|
else
|
||||||
|
EnQueue(GenQueue,p);
|
||||||
|
}
|
||||||
|
while ( (!IsEmpty(GenQueue)) && (!IsEmpty(LadyQueue)) ) // 匹配舞者
|
||||||
|
{
|
||||||
|
DeQueue(GenQueue,m); // 女士出队
|
||||||
|
DeQueue(LadyQueue,f); // 男士出队
|
||||||
|
cout<<m.name <<"<---配对--->"<<f.name<<endl; // 男、女配队
|
||||||
|
}
|
||||||
|
if (!IsEmpty(GenQueue))
|
||||||
|
{
|
||||||
|
GetHead(GenQueue,m);
|
||||||
|
cout<<m.name<<"先生还在等着呢!"<<endl;
|
||||||
|
}
|
||||||
|
else if (!IsEmpty(LadyQueue))
|
||||||
|
{
|
||||||
|
GetHead(LadyQueue,f);
|
||||||
|
cout<<f.name<<"女士还在等着呢!"<<endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<"配对完美结束!"<<endl;
|
||||||
|
DestroyLinkQueue(GenQueue); // 销毁队列
|
||||||
|
DestroyLinkQueue(LadyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
dancer *person;
|
||||||
|
int num;
|
||||||
|
cout<<"请输入舞伴总数量:"<<endl;
|
||||||
|
cin>>num;
|
||||||
|
person=new dancer[num];
|
||||||
|
EntranHall(person, num); // 舞者入场
|
||||||
|
DancePartner(person,num); // 舞者匹配
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
112
OrigFiles/3-特殊线性表/3-DancePartner(舞伴问题)/LinkQueue.h
Normal file
112
OrigFiles/3-特殊线性表/3-DancePartner(舞伴问题)/LinkQueue.h
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//【算法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;
|
||||||
|
}
|
||||||
|
|
||||||
108
OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp
Normal file
108
OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
#include<iostream> //cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include "LinkQueue.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<<"8-显示队列元素\n";
|
||||||
|
cout<<"0-退出\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int e;
|
||||||
|
LinkQueue<int> Q;
|
||||||
|
system("cls"); // 执行系统命令cls,清屏
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
dispmenu(); // 显示主菜单
|
||||||
|
cout<<"功能选择(1~8,0 退出):";
|
||||||
|
cin>>choice;
|
||||||
|
switch(choice)
|
||||||
|
{
|
||||||
|
case 1: // 初始化链队
|
||||||
|
InitQueue(Q);
|
||||||
|
cout<<endl<<"创建成功!"<<endl;
|
||||||
|
break;
|
||||||
|
case 2: //入队
|
||||||
|
cout<<"输入要插入的元素值:"<<endl;
|
||||||
|
cin>>e;
|
||||||
|
cout<<endl;
|
||||||
|
if(EnQueue(Q,e))
|
||||||
|
{
|
||||||
|
cout<<endl<<"入队成功!队列元素为:"<<endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"入队不成功!"<<endl;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3: // 出队
|
||||||
|
if(DeQueue(Q,e))
|
||||||
|
{
|
||||||
|
cout<<endl<<"出队成功!出队元素为:"<<e<<endl;
|
||||||
|
cout<<endl<<"出队后,队列元素为:"<<endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"队空,出队失败!"<<endl;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4: // 获取队头元素
|
||||||
|
if(GetHead(Q,e))
|
||||||
|
{
|
||||||
|
cout<<"队列元素为:"<<endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
cout<<endl<<"队头元素为:"<<e<<endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"队空!"<<endl;
|
||||||
|
break;
|
||||||
|
case 5: // 获取队尾元素
|
||||||
|
if(GetTail(Q,e))
|
||||||
|
{
|
||||||
|
cout<<"队列元素为:"<<endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
cout<<endl<<"队尾元素为:"<<e<<endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"队空!"<<endl;
|
||||||
|
break;
|
||||||
|
case 6: // 清空队
|
||||||
|
ClearQueue(Q);
|
||||||
|
cout<<endl<<"队已空!"<<endl;
|
||||||
|
case 7: // 测队空
|
||||||
|
if(QueueEmpty(Q))
|
||||||
|
cout<<endl<<"空队!"<<endl;
|
||||||
|
else
|
||||||
|
cout<<endl<<"不是空队!"<<endl;
|
||||||
|
break;
|
||||||
|
case 8: // 查看队列元素
|
||||||
|
DispQueue(Q);
|
||||||
|
cout<<endl;
|
||||||
|
break;
|
||||||
|
case 0: // 退出
|
||||||
|
DestroyQueue(Q);
|
||||||
|
cout<<"结束运行Bye-bye!"<<endl;
|
||||||
|
break;
|
||||||
|
default: // 无效选择
|
||||||
|
cout<<"无效选择!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}while(choice!=0);
|
||||||
|
return 0;
|
||||||
|
}//end of main function
|
||||||
127
OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.h
Normal file
127
OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.h
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
93
OrigFiles/3-特殊线性表/3-LinkStack(链栈)/LinkStack.cpp
Normal file
93
OrigFiles/3-特殊线性表/3-LinkStack(链栈)/LinkStack.cpp
Normal 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~6,0 退出):";
|
||||||
|
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
|
||||||
96
OrigFiles/3-特殊线性表/3-LinkStack(链栈)/LinkStack.h
Normal file
96
OrigFiles/3-特殊线性表/3-LinkStack(链栈)/LinkStack.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
126
OrigFiles/3-特殊线性表/3-SqQueue(循环队列)/SqQueue.cpp
Normal file
126
OrigFiles/3-特殊线性表/3-SqQueue(循环队列)/SqQueue.cpp
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
|
||||||
|
#include<iostream>//cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include "SqQueue.h"
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
|
||||||
|
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<<"8-测队满\n";
|
||||||
|
cout<<"9-显示队列元素\n";
|
||||||
|
cout<<"10-显示队头、队尾指针\n";
|
||||||
|
cout<<"0-退出\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int e;
|
||||||
|
SqQueue<int> Q; // 元素类型为整型的顺序队列
|
||||||
|
system("cls"); // 清屏
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
dispmenu(); //显示主菜单
|
||||||
|
cout<<"功能选择(1~10,0 退出!):";
|
||||||
|
cin>>choice;
|
||||||
|
switch(choice)
|
||||||
|
{
|
||||||
|
case 1: // 初始化顺序队列
|
||||||
|
cout<<"请输入要创建的顺序队列的长度";
|
||||||
|
cin>>i;
|
||||||
|
cout<<endl;
|
||||||
|
InitQueue (Q,i);
|
||||||
|
cout<<endl<<"创建成功!"<<endl;
|
||||||
|
break;
|
||||||
|
case 2: // 入队
|
||||||
|
cout<<"输入要入队的元素值:"<<endl;
|
||||||
|
cin>>e;
|
||||||
|
cout<<endl;
|
||||||
|
if(EnQueue(Q,e))
|
||||||
|
{
|
||||||
|
cout<<endl<<"入队成功!入队后队列元素为:"<<endl;
|
||||||
|
DispQueue(Q);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"队满,不能入队!"<<endl;
|
||||||
|
break;
|
||||||
|
case 3: // 出队
|
||||||
|
if(DeQueue(Q,e))
|
||||||
|
{
|
||||||
|
cout<<endl<<"队列元素为:";
|
||||||
|
DispQueue(Q);
|
||||||
|
cout<<endl<<"出队元素为:"<<e<<endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"队空,不能出队!"<<endl;
|
||||||
|
break;
|
||||||
|
case 4: // 取队头元素
|
||||||
|
if(GetHead(Q,e))
|
||||||
|
{
|
||||||
|
cout<<endl<<"队列元素为:";
|
||||||
|
DispQueue(Q);
|
||||||
|
cout<<endl<<"队头元素为:"<<e<<endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"队空!"<<endl;
|
||||||
|
break;
|
||||||
|
case 5: // 取队尾元素
|
||||||
|
if(GetTail(Q,e))
|
||||||
|
{
|
||||||
|
cout<<endl<<"队列元素为:";
|
||||||
|
DispQueue(Q);
|
||||||
|
cout<<endl<<"队尾元素为:"<<e<<endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"队空!无数据元素"<<endl;
|
||||||
|
break;
|
||||||
|
case 6: // 清空队
|
||||||
|
ClearQueue(Q);
|
||||||
|
cout<<"队已清空!"<<endl;
|
||||||
|
break;
|
||||||
|
case 7: // 测队空
|
||||||
|
if(QueueEmpty(Q))
|
||||||
|
cout<<endl<<"空队!"<<endl;
|
||||||
|
else
|
||||||
|
cout<<endl<<"非空队!"<<endl;
|
||||||
|
break;
|
||||||
|
case 8: // 测队满
|
||||||
|
if(QueueFull(Q))
|
||||||
|
cout<<endl<<"满队!"<<endl;
|
||||||
|
else
|
||||||
|
cout<<endl<<"非满队!"<<endl;
|
||||||
|
break;
|
||||||
|
case 9: // 显示队列元素
|
||||||
|
DispQueue(Q);
|
||||||
|
cout<<endl;
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
cout<<"\nQ.fornt="<<Q.front<<endl;
|
||||||
|
cout<<"Q.rear="<<Q.rear<<endl;
|
||||||
|
break;
|
||||||
|
case 0: // 退出
|
||||||
|
DestroyQueue(Q);
|
||||||
|
cout<<"结束运行Bye-bye!"<<endl;
|
||||||
|
break;
|
||||||
|
default: // 无效选择
|
||||||
|
cout<<"无效选择!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}while(choice!=0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}//end of main function
|
||||||
|
|
||||||
113
OrigFiles/3-特殊线性表/3-SqStack(顺序栈)/SqStack.cpp
Normal file
113
OrigFiles/3-特殊线性表/3-SqStack(顺序栈)/SqStack.cpp
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
|
||||||
|
#include<iostream>//cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include "SqStack.h"
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
|
||||||
|
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<<"8-显示栈元素\n";
|
||||||
|
cout<<"9-查看栈顶指针\n";
|
||||||
|
cout<<"0-退出\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int e;
|
||||||
|
SqStack<int> S;//建立容量为20、元素类型为整型的空顺序栈
|
||||||
|
system("cls"); // 清屏
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
dispmenu(); // 显示主菜单
|
||||||
|
cout<<"Enter choice(1~6,0 退出):";
|
||||||
|
cin>>choice;
|
||||||
|
switch(choice)
|
||||||
|
{
|
||||||
|
case 1: // 初始化顺序栈
|
||||||
|
cout<<"请输入要创建的顺序栈的长度";
|
||||||
|
cin>>i;
|
||||||
|
cout<<endl;
|
||||||
|
InitStack (S,i);
|
||||||
|
cout<<endl<<"创建成功!"<<endl;
|
||||||
|
break;
|
||||||
|
case 2: // 入栈
|
||||||
|
cout<<"输入要入栈的元素值:"<<endl;
|
||||||
|
cin>>e;
|
||||||
|
if(Push(S,e))
|
||||||
|
{
|
||||||
|
cout<<"入栈成功!入栈后栈元素为:"<<endl;
|
||||||
|
DispStack(S);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout<<endl<<"栈满,不能入栈!"<<endl;
|
||||||
|
break;
|
||||||
|
case 3: // 出栈
|
||||||
|
if(Pop(S,e))
|
||||||
|
{
|
||||||
|
cout<<"出栈成功!出栈元素为:"<<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: // 清空栈
|
||||||
|
ClearStack(S);
|
||||||
|
cout<<endl<<"栈已空!"<<endl;
|
||||||
|
break;
|
||||||
|
case 6: // 测栈空
|
||||||
|
if(StackEmpty(S))
|
||||||
|
cout<<endl<<"空栈!"<<endl;
|
||||||
|
else
|
||||||
|
cout<<endl<<"不是空栈!"<<endl;
|
||||||
|
break;
|
||||||
|
case 7: // 测栈满
|
||||||
|
if(StackFull(S))
|
||||||
|
cout<<endl<<"栈满!"<<endl;
|
||||||
|
else
|
||||||
|
cout<<endl<<"栈不满!"<<endl;
|
||||||
|
break;
|
||||||
|
case 8: // 显示栈元素
|
||||||
|
DispStack(S);
|
||||||
|
cout<<endl;
|
||||||
|
break;
|
||||||
|
case 9: // 显示栈顶指针
|
||||||
|
cout<<"栈顶指针top= "<<S.top<<endl;
|
||||||
|
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
|
||||||
|
|
||||||
107
OrigFiles/3-特殊线性表/3-SqStack(顺序栈)/SqStack.h
Normal file
107
OrigFiles/3-特殊线性表/3-SqStack(顺序栈)/SqStack.h
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
template <class DT>
|
||||||
|
struct SqStack // 顺序栈
|
||||||
|
{
|
||||||
|
DT *base; // 栈首址
|
||||||
|
int top; // 栈顶指针
|
||||||
|
int stacksize; // 栈容量
|
||||||
|
};
|
||||||
|
|
||||||
|
//基本操作的实现
|
||||||
|
//【算法3.1】 // 初始化栈
|
||||||
|
template <class DT>
|
||||||
|
void InitStack(SqStack<DT> &S, int m)
|
||||||
|
{
|
||||||
|
S.base=new DT[m]; // 申请栈空间
|
||||||
|
if(S.base==NULL) // 申请失败,退出
|
||||||
|
{
|
||||||
|
cout<<"未创建成功!";
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
S.top=-1; // 设置空栈属性
|
||||||
|
S.stacksize=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,DT e)
|
||||||
|
{
|
||||||
|
if(S.top==S.stacksize-1) // 栈满,不能入栈
|
||||||
|
return false; // 返回false
|
||||||
|
S.top++;
|
||||||
|
S.base[S.top]=e;
|
||||||
|
return true; // 入栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
//【算法3.4】 // 出栈
|
||||||
|
template<class DT>
|
||||||
|
bool Pop(SqStack<DT> &S,DT &e)
|
||||||
|
{
|
||||||
|
if(S.top==-1) // 栈空
|
||||||
|
return false; // 返回false
|
||||||
|
e=S.base[S.top]; // 取栈顶元素
|
||||||
|
S.top--; // 栈顶指针下移
|
||||||
|
return true; // 出栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//【算法3.5】 // 获取栈顶元素
|
||||||
|
template<class DT>
|
||||||
|
bool GetTop(SqStack<DT> S,DT &e)
|
||||||
|
{
|
||||||
|
if(S.top==-1) // 栈空
|
||||||
|
return false; // 返回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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//显示栈内容
|
||||||
|
template<class DT>
|
||||||
|
void DispStack(SqStack<DT> S)
|
||||||
|
{
|
||||||
|
int i=S.top;
|
||||||
|
while(i>=0)
|
||||||
|
{
|
||||||
|
cout<<S.base[i--]<<"\t";
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class DT>
|
||||||
|
void ClearStack(SqStack<DT> &S)
|
||||||
|
{
|
||||||
|
S.top=-1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class DT>
|
||||||
|
int StackFull(SqStack<DT> S)
|
||||||
|
{
|
||||||
|
if(S.top==S.stacksize-1)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
282
OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/Expresion.cpp
Normal file
282
OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/Expresion.cpp
Normal file
@@ -0,0 +1,282 @@
|
|||||||
|
#include<iostream> //cout,cin
|
||||||
|
using namespace std;
|
||||||
|
#include"SqStack.h"
|
||||||
|
|
||||||
|
char pause;
|
||||||
|
|
||||||
|
char Precede(char t1,char t2) //算符的优先级比较
|
||||||
|
{
|
||||||
|
char f;
|
||||||
|
switch(t2)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
case '-':if(t1=='('||t1=='=')
|
||||||
|
f='<';
|
||||||
|
else
|
||||||
|
f='>';
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
case '/':if(t1=='*'||t1=='/'||t1==')')
|
||||||
|
f='>';
|
||||||
|
else
|
||||||
|
f='<';
|
||||||
|
break;
|
||||||
|
case '(':if(t1==')')
|
||||||
|
{
|
||||||
|
cout<<"ERROR1"<<endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
f='<';
|
||||||
|
break;
|
||||||
|
case ')':switch(t1)
|
||||||
|
{
|
||||||
|
case '(':f='=';
|
||||||
|
break;
|
||||||
|
case '=':cout<<"ERROR2"<<endl;
|
||||||
|
exit(0);
|
||||||
|
default: f='>';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '=':switch(t1)
|
||||||
|
{
|
||||||
|
case '=':f='=';
|
||||||
|
break;
|
||||||
|
case '(':cout<<"ERROR2"<<endl;
|
||||||
|
exit(0);
|
||||||
|
default: f='>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool In(char ch) // 判断 ch 是否为运算符
|
||||||
|
{
|
||||||
|
switch(ch)
|
||||||
|
{
|
||||||
|
case'+':
|
||||||
|
case'-':
|
||||||
|
case'*':
|
||||||
|
case'/':
|
||||||
|
case'(':
|
||||||
|
case')':
|
||||||
|
case'=':return true;
|
||||||
|
default:return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float Operate(float a,char theta,float b)
|
||||||
|
{ // 实施一次运算
|
||||||
|
float ch;
|
||||||
|
switch(theta)
|
||||||
|
{
|
||||||
|
case'+':ch=a+b;
|
||||||
|
break;
|
||||||
|
case'-':ch=a-b;
|
||||||
|
break;
|
||||||
|
case'*':ch=a*b;
|
||||||
|
break;
|
||||||
|
case'/':ch=a/b;
|
||||||
|
}
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
//算法3.12
|
||||||
|
float Val_Exp(char *exp) // 中缀表达式求值。
|
||||||
|
{
|
||||||
|
SqStack<char> OP; // 运算符栈
|
||||||
|
SqStack<float> OD; // 运算数栈
|
||||||
|
InitStack(OP,30);
|
||||||
|
InitStack(OD,30);
|
||||||
|
char theta;
|
||||||
|
float a,b,result;
|
||||||
|
char ch,x; // 存放由键盘接收的字符
|
||||||
|
char z[6]; // 存放符点数字符串
|
||||||
|
int i;
|
||||||
|
Push(OP,'='); // # 是表达式结束标志
|
||||||
|
ch=*exp++;
|
||||||
|
GetTop(OP,x);
|
||||||
|
while(ch!='='||x!='=')
|
||||||
|
{
|
||||||
|
if(ch>='0'&&ch<='9'||ch=='.') // ch 是操作数
|
||||||
|
{
|
||||||
|
i=0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
z[i]=ch;
|
||||||
|
i++;
|
||||||
|
ch=*exp++;
|
||||||
|
} while(ch>='0'&&ch<='9'||ch=='.');
|
||||||
|
z[i]='\0';
|
||||||
|
result=atof(z); // 将字符串数组转为符点型存于result
|
||||||
|
Push(OD,result);
|
||||||
|
}
|
||||||
|
else if(In(ch)) // 是7种运算符之一
|
||||||
|
switch(Precede(x,ch))
|
||||||
|
{
|
||||||
|
case'<':Push(OP,ch); // 栈顶元素优先权低
|
||||||
|
ch=*exp++;
|
||||||
|
break;
|
||||||
|
case'=':Pop(OP,x); // 脱括号并接收下一字符
|
||||||
|
ch=*exp++;
|
||||||
|
break;
|
||||||
|
case'>':Pop(OP,theta); // 退栈并将运算结果入栈
|
||||||
|
Pop(OD,b);
|
||||||
|
Pop(OD,a);
|
||||||
|
Push(OD,Operate(a,theta,b));
|
||||||
|
}
|
||||||
|
else // ch是非法字符
|
||||||
|
{
|
||||||
|
cout<<"ERROR3"<<endl;;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
GetTop(OP,x);
|
||||||
|
}
|
||||||
|
GetTop(OD,result);
|
||||||
|
DestroyStack(OP);
|
||||||
|
DestroyStack(OD);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreatePostExp(char * exp,char * &postexp) // 由中缀式求后缀式
|
||||||
|
{
|
||||||
|
char ch,x;
|
||||||
|
int i=0;
|
||||||
|
SqStack<char> OP;
|
||||||
|
InitStack(OP,30);
|
||||||
|
Push(OP,'='); // # 是表达式结束标志
|
||||||
|
cout<<"中缀表达式:"<<exp<<endl;
|
||||||
|
ch=*exp++;
|
||||||
|
while(ch)
|
||||||
|
{
|
||||||
|
if((ch>='0'&&ch<='9')||ch=='.')
|
||||||
|
{
|
||||||
|
postexp[i++]=ch;
|
||||||
|
ch=*exp++;
|
||||||
|
}
|
||||||
|
if(In(ch)) // 是7种运算符之一
|
||||||
|
{
|
||||||
|
postexp[i++]=' ';
|
||||||
|
GetTop(OP,x);
|
||||||
|
switch(Precede(x,ch))
|
||||||
|
{
|
||||||
|
case'<':Push(OP,ch); // 栈顶元素优先权低
|
||||||
|
ch=*exp++;
|
||||||
|
break;
|
||||||
|
case'=':Pop(OP,x); // 脱括号并接收下一字符
|
||||||
|
ch=*exp++;
|
||||||
|
break;
|
||||||
|
case'>':
|
||||||
|
Pop(OP,postexp[i++]); // 运算符出栈输出
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
postexp[i]='\0';
|
||||||
|
}//while
|
||||||
|
cout<<"\n后缀表达式为:"<<postexp<<endl;
|
||||||
|
DestroyStack(OP);
|
||||||
|
}
|
||||||
|
|
||||||
|
//算法3.13
|
||||||
|
float Val_PostExp(char *postexp) // 后缀表达式求值
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char z[6];
|
||||||
|
float result=0,d=0,a,b;
|
||||||
|
char ch;
|
||||||
|
SqStack<float> OD;
|
||||||
|
InitStack(OD,30);
|
||||||
|
ch=*postexp++;
|
||||||
|
while(ch!='\0')
|
||||||
|
{
|
||||||
|
|
||||||
|
if(ch>='0'&&ch<='9'||ch=='.') // ch为操作数符号
|
||||||
|
{
|
||||||
|
i=0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
z[i++]=ch;
|
||||||
|
ch=*postexp++; // 取下一个操作数符号
|
||||||
|
} while(ch>='0'&&ch<='9'||ch=='.');
|
||||||
|
z[i]='\0';
|
||||||
|
d=atof(z); // 将字符串数组转为符点型存于result
|
||||||
|
Push(OD,d); // 操作数进栈
|
||||||
|
}
|
||||||
|
if(In(ch)) // ch为运算符
|
||||||
|
{
|
||||||
|
Pop (OD,a);
|
||||||
|
Pop (OD,b);
|
||||||
|
Push (OD,Operate(b,ch,a));
|
||||||
|
ch=*postexp++;
|
||||||
|
}
|
||||||
|
ch=*postexp++;
|
||||||
|
}
|
||||||
|
Pop (OD,result);
|
||||||
|
DestroyStack(OD);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//主函数
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
//int i;
|
||||||
|
char exp[20]="(2.2+5)+4*(5-3.1)=";
|
||||||
|
char *postexp;
|
||||||
|
postexp=new char[30];
|
||||||
|
*postexp='\0';
|
||||||
|
float v;
|
||||||
|
system("cls"); // 清屏
|
||||||
|
cout<<"\n缺省表达式:"<<exp;
|
||||||
|
cout<<endl;
|
||||||
|
int choice;
|
||||||
|
do
|
||||||
|
{ // 显示主菜单
|
||||||
|
cout<<endl;
|
||||||
|
cout<<"1-创建表达式\n";
|
||||||
|
cout<<"2-表达式求值\n";
|
||||||
|
cout<<"3-求后缀表达式\n";
|
||||||
|
cout<<"4-后缀表达式求值\n";
|
||||||
|
cout<<"5-显示表达式\n";
|
||||||
|
cout<<"0-退出\n";
|
||||||
|
cout<<"输入功能选项(1~5,0 退出):\n";
|
||||||
|
cin>>choice;
|
||||||
|
switch(choice)
|
||||||
|
{
|
||||||
|
case 1: // 创建表达式
|
||||||
|
cout<<"\n请输入表达式,以=结束"<<endl;
|
||||||
|
cin>>exp;
|
||||||
|
break;
|
||||||
|
case 2: // 表达式求值
|
||||||
|
v=Val_Exp(exp) ;
|
||||||
|
cout<<exp;
|
||||||
|
cout<<v<<endl;
|
||||||
|
break;
|
||||||
|
case 3: // 求后缀表达式
|
||||||
|
CreatePostExp(exp,postexp);
|
||||||
|
break;
|
||||||
|
case 4: // 后缀表达式求值
|
||||||
|
v=Val_PostExp(postexp);
|
||||||
|
cout<<'\n'<<postexp<<"="<<v<<endl;
|
||||||
|
break;
|
||||||
|
case 5: // 显示表达式
|
||||||
|
cout<<endl;
|
||||||
|
cout<<"\n已创建的表达式为:";
|
||||||
|
cout<<exp<<endl;
|
||||||
|
if(strlen(postexp))
|
||||||
|
{
|
||||||
|
cout<<"\n后缀表达式为:";
|
||||||
|
cout<<postexp<<endl;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0: // 退出
|
||||||
|
cout<<"\n结束运行,Bye-Bye!"<<endl;
|
||||||
|
break;
|
||||||
|
default://
|
||||||
|
cout<<"\n无效选择!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}while(choice!=0);
|
||||||
|
}//end main
|
||||||
89
OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/SqStack.h
Normal file
89
OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/SqStack.h
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
template <class DT>
|
||||||
|
struct SqStack // 顺序栈
|
||||||
|
{
|
||||||
|
DT *base; // 栈首址
|
||||||
|
int top; // 栈顶指针
|
||||||
|
int stacksize; // 栈容量
|
||||||
|
};
|
||||||
|
|
||||||
|
//基本操作的实现
|
||||||
|
//【算法3.1】 // 初始化栈
|
||||||
|
template <class DT>
|
||||||
|
void InitStack(SqStack<DT> &S, int m)
|
||||||
|
{
|
||||||
|
S.base=new DT[m]; // 申请栈空间
|
||||||
|
if(S.base==NULL) // 申请失败,退出
|
||||||
|
{
|
||||||
|
cout<<"未创建成功!";
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
S.top=-1; // 设置空栈属性
|
||||||
|
S.stacksize=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,DT e)
|
||||||
|
{
|
||||||
|
if(S.top==S.stacksize-1) // 栈满,不能入栈
|
||||||
|
return false; // 返回false
|
||||||
|
S.top++;
|
||||||
|
S.base[S.top]=e;
|
||||||
|
return true; // 入栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
//【算法3.4】 // 出栈
|
||||||
|
template<class DT>
|
||||||
|
bool Pop(SqStack<DT> &S,DT &e)
|
||||||
|
{
|
||||||
|
if(S.top==-1) // 栈空
|
||||||
|
return false; // 返回false
|
||||||
|
e=S.base[S.top]; // 取栈顶元素
|
||||||
|
S.top--; // 栈顶指针下移
|
||||||
|
return true; // 出栈成功,返回true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//【算法3.5】 // 获取栈顶元素
|
||||||
|
template<class DT>
|
||||||
|
bool GetTop(SqStack<DT> S,DT &e)
|
||||||
|
{
|
||||||
|
if(S.top==-1) // 栈空
|
||||||
|
return false; // 返回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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//显示栈内容
|
||||||
|
template<class DT>
|
||||||
|
void DispStack(SqStack<DT> S)
|
||||||
|
{
|
||||||
|
int i=S.top;
|
||||||
|
while(i>=0)
|
||||||
|
{
|
||||||
|
cout<<S.base[i--]<<"\t";
|
||||||
|
}
|
||||||
|
cout<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user