2024-3-18
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user