Compare commits
10 Commits
2e9a13a76c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e01c041259 | |||
| 5dbdfef1a1 | |||
| 6b458a6a3e | |||
| 59e0405033 | |||
| 8933bff3c7 | |||
| 0dd8916b05 | |||
| 3c1d1432d5 | |||
| b671321535 | |||
| 68d6c32692 | |||
| 79c0a970f0 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
!OrigFiles/
|
||||
|
||||
### C++ template
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"iostream": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34322.80
|
||||
VisualStudioVersion = 17.9.34701.34
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "app1-4", "app1-4\app1-4.vcxproj", "{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "b", "b\b.vcxproj", "{51CE097C-C445-4A3E-A678-539C70E334DF}"
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Chapter1", "Chapter1.vcxproj", "{38D719C8-7D69-48A6-93DC-234C93F7B6BC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -15,27 +13,19 @@ Global
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x64.Build.0 = Debug|x64
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x86.Build.0 = Debug|Win32
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x64.ActiveCfg = Release|x64
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x64.Build.0 = Release|x64
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x86.ActiveCfg = Release|Win32
|
||||
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x86.Build.0 = Release|Win32
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x64.Build.0 = Debug|x64
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x86.Build.0 = Debug|Win32
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x64.ActiveCfg = Release|x64
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x64.Build.0 = Release|x64
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x86.ActiveCfg = Release|Win32
|
||||
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x86.Build.0 = Release|Win32
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x64.Build.0 = Debug|x64
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x86.Build.0 = Debug|Win32
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x64.ActiveCfg = Release|x64
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x64.Build.0 = Release|x64
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x86.ActiveCfg = Release|Win32
|
||||
{38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {2E7BE105-4A42-43C5-A5F2-51518A78298B}
|
||||
SolutionGuid = {E30ECD0C-3E43-481E-9914-3B07AAFE7C8B}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@@ -18,14 +18,11 @@
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="b.cpp" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{51ce097c-c445-4a3e-a678-539c70e334df}</ProjectGuid>
|
||||
<RootNamespace>b</RootNamespace>
|
||||
<ProjectGuid>{38d719c8-7d69-48a6-93dc-234c93f7b6bc}</ProjectGuid>
|
||||
<RootNamespace>Chapter1</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
@@ -129,6 +126,9 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="app1-4.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
33
Chapter1/app1-4.cpp
Normal file
33
Chapter1/app1-4.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
|
||||
struct stu
|
||||
{
|
||||
int xh; // 学号
|
||||
float height; //身高
|
||||
};
|
||||
|
||||
int MaxH; // 最高身高
|
||||
|
||||
//算法1.1
|
||||
int maxHeight(stu student[], int n)
|
||||
{
|
||||
MaxH = 1; // 1. 设第1个学生身高最高
|
||||
for (int i = 2; i <= n; i++) // 2. 从第2个学生起依次与最高身比
|
||||
{
|
||||
if (student[i - 1].height > student[MaxH - 1].height) // 2.1 大于最高身高
|
||||
MaxH = i; // 2.2为新最高身高
|
||||
}
|
||||
return student[MaxH - 1].xh; // 3.返回最高身高学生学学号
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "获取数据" << endl;
|
||||
int n = 6; //学生数
|
||||
int xh;
|
||||
stu student[6] = { {1703001,176},{1703002,180},{1703003,175},{1703004,182.5},{1703005,158.5},{1703005,173.8} };
|
||||
xh = maxHeight(student, n);
|
||||
cout << "身高最高者是:" << xh << endl;
|
||||
return 1;
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
// USE B.CPP INSTEAD
|
||||
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
void main()
|
||||
{
|
||||
int i=0,j=0,count=0;
|
||||
for (i=2;i<=1000;i++)
|
||||
{
|
||||
for (j=2;j<=i-1;j++)
|
||||
if(i%j==0)
|
||||
break;
|
||||
if(j>i-1)
|
||||
{
|
||||
cout<<j<<'\t';
|
||||
count++:
|
||||
if(cout%5==0) cout<<endl;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
void main()
|
||||
{
|
||||
int i = 0, j = 0, count = 0;
|
||||
for (i = 2; i <= 1000; i++)
|
||||
{
|
||||
for (j = 2; j <= i - 1; j++)
|
||||
if (i % j == 0)
|
||||
break;
|
||||
if (j > i - 1)
|
||||
{
|
||||
cout << j << '\t';
|
||||
count++;
|
||||
if (count % 5 == 0) cout << endl;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// 2-1-SqList-顺序表
|
||||
// 2-1-SqList-顺序表
|
||||
|
||||
#include <iostream>//cout,cin
|
||||
using namespace std;
|
||||
@@ -8,147 +8,147 @@ char pause;
|
||||
|
||||
|
||||
void dispmenu() {
|
||||
//显示主菜单
|
||||
//显示主菜单
|
||||
cout << endl;
|
||||
cout << "*** 顺 序 表 ***\n";
|
||||
cout << " 1-初始化顺序表\n";
|
||||
cout << " 2-创建顺序表\n";
|
||||
cout << " 3-获取第i个元素\n";
|
||||
cout << " 4-按值查找元素位序\n";
|
||||
cout << " 5-插入第i个元素\n";
|
||||
cout << " 6-删除第i个元素\n";
|
||||
cout << " 7-修改第i个元素\n";
|
||||
cout << " 8-清空表\n";
|
||||
cout << " 9-测表长\n";
|
||||
cout << " 10-测表空\n";
|
||||
cout << " 11-测表满\n";
|
||||
cout << " 12-显示表\n";
|
||||
cout << " 13-按值查找前驱\n";
|
||||
cout << " 0-退出\n";
|
||||
cout << "*** 顺 序 表 ***\n";
|
||||
cout << " 1-初始化顺序表\n";
|
||||
cout << " 2-创建顺序表\n";
|
||||
cout << " 3-获取第i个元素\n";
|
||||
cout << " 4-按值查找元素位序\n";
|
||||
cout << " 5-插入第i个元素\n";
|
||||
cout << " 6-删除第i个元素\n";
|
||||
cout << " 7-修改第i个元素\n";
|
||||
cout << " 8-清空表\n";
|
||||
cout << " 9-测表长\n";
|
||||
cout << " 10-测表空\n";
|
||||
cout << " 11-测表满\n";
|
||||
cout << " 12-显示表\n";
|
||||
cout << " 13-按值查找前驱\n";
|
||||
cout << " 0-退出\n";
|
||||
}
|
||||
|
||||
|
||||
//主函数
|
||||
//主函数
|
||||
void main() {
|
||||
int i;
|
||||
int e, pre_e;
|
||||
SqList<int> L; //建立容量为20、元素类型为整型的空顺序表
|
||||
system("cls"); //执行系统命令cls,清屏
|
||||
SqList<int> L; //建立容量为20、元素类型为整型的空顺序表
|
||||
system("cls"); //执行系统命令cls,清屏
|
||||
|
||||
int choice;
|
||||
do {
|
||||
dispmenu(); //显示主菜单
|
||||
cout << "Enter choice(1~12,0 退出):";
|
||||
dispmenu(); //显示主菜单
|
||||
cout << "Enter choice(1~12,0 退出):";
|
||||
cin >> choice;
|
||||
switch (choice) {
|
||||
case 1: //初始化线性表
|
||||
cout << "请输入要创建的顺序表的长度:";
|
||||
case 1: //初始化线性表
|
||||
cout << "请输入要创建的顺序表的长度:";
|
||||
cin >> i;
|
||||
cout << endl;
|
||||
InitList(L, i);
|
||||
cout << endl << "创建成功!" << endl;
|
||||
cout << endl << "创建成功!" << endl;
|
||||
break;
|
||||
case 2: //创建线性表
|
||||
cout << "请输入要创建的元素个数:";
|
||||
case 2: //创建线性表
|
||||
cout << "请输入要创建的元素个数:";
|
||||
cin >> i;
|
||||
cout << endl;
|
||||
CreateList(L, i);
|
||||
cout << "创建的顺序表元素为:\n"; //显示表元素
|
||||
cout << "创建的顺序表元素为:\n"; //显示表元素
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
break;
|
||||
case 3: //获取第i个元素
|
||||
cout << "请输入元素序号:";
|
||||
case 3: //获取第i个元素
|
||||
cout << "请输入元素序号:";
|
||||
cin >> i;
|
||||
cout << endl;
|
||||
if (GetElem_i(L, i, e))
|
||||
cout << endl << "第" << i << "个元素为:" << e << endl;
|
||||
cout << endl << "第" << i << "个元素为:" << e << endl;
|
||||
else
|
||||
cout << endl << "元素不存在!" << endl;
|
||||
cout << endl << "元素不存在!" << endl;
|
||||
break;
|
||||
case 4: //按值查找
|
||||
cout << "请输入要查询的元素值:";
|
||||
case 4: //按值查找
|
||||
cout << "请输入要查询的元素值:";
|
||||
cin >> e;
|
||||
i = LocateElem_e(L, e);
|
||||
if (i)
|
||||
cout << endl << e << "是第" << i << "个数据元素" << endl;
|
||||
cout << endl << e << "是第" << i << "个数据元素" << endl;
|
||||
else
|
||||
cout << endl << "不存在此元素!" << endl;
|
||||
cout << endl << "不存在此元素!" << endl;
|
||||
break;
|
||||
case 5: // 在第i个位置插入元素
|
||||
cout << "输入插入位置:" << endl;
|
||||
case 5: // 在第i个位置插入元素
|
||||
cout << "输入插入位置:" << endl;
|
||||
cin >> i;
|
||||
cout << "输入插入元素值:" << endl;
|
||||
cout << "输入插入元素值:" << endl;
|
||||
cin >> e;
|
||||
if (InsertElem_i(L, i, e)) {
|
||||
cout << endl << "插入成功!" << endl;
|
||||
cout << endl << "插入成功!" << endl;
|
||||
DispList(L);
|
||||
}
|
||||
else
|
||||
cout << endl << "插入不成功!" << endl;;
|
||||
cout << endl << "插入不成功!" << endl;;
|
||||
break;
|
||||
case 6: //删除第i个元素
|
||||
cout << "输入删除元素位置:" << endl;
|
||||
case 6: //删除第i个元素
|
||||
cout << "输入删除元素位置:" << endl;
|
||||
cin >> i;
|
||||
if (DeleElem_i(L, i)) {
|
||||
cout << endl << "删除成功!" << endl;
|
||||
cout << endl << "删除成功!" << endl;
|
||||
DispList(L);
|
||||
}
|
||||
else
|
||||
cout << endl << "删除失败!" << endl;
|
||||
cout << endl << "删除失败!" << endl;
|
||||
break;
|
||||
case 7: //修改第i个元素的值
|
||||
cout << "输入修改元素位置:" << endl;
|
||||
case 7: //修改第i个元素的值
|
||||
cout << "输入修改元素位置:" << endl;
|
||||
cin >> i;
|
||||
cout << "输入新元素值:" << endl;
|
||||
cout << "输入新元素值:" << endl;
|
||||
cin >> e;
|
||||
if (PutElem(L, i, e)) {
|
||||
cout << endl << "修改成功!" << endl;
|
||||
cout << endl << "修改成功!" << endl;
|
||||
DispList(L);
|
||||
}
|
||||
else
|
||||
cout << endl << "修改失败!" << endl;
|
||||
cout << endl << "修改失败!" << endl;
|
||||
break;
|
||||
case 8: // 清空表
|
||||
case 8: // 清空表
|
||||
ClearList(L);
|
||||
break;
|
||||
case 9: // 测表长
|
||||
cout << "表长为:" << ListLength(L) << endl;
|
||||
case 9: // 测表长
|
||||
cout << "表长为:" << ListLength(L) << endl;
|
||||
break;
|
||||
case 10: //测表空
|
||||
case 10: //测表空
|
||||
if (ListEmpty(L))
|
||||
cout << endl << "空表!" << endl;
|
||||
cout << endl << "空表!" << endl;
|
||||
else
|
||||
cout << endl << "不是空表!" << endl;
|
||||
cout << endl << "不是空表!" << endl;
|
||||
break;
|
||||
case 11: // 未表满
|
||||
case 11: // 未表满
|
||||
if (ListFull(L))
|
||||
cout << endl << "表满!" << endl;
|
||||
cout << endl << "表满!" << endl;
|
||||
else
|
||||
cout << endl << "表不满!" << endl;
|
||||
cout << endl << "表不满!" << endl;
|
||||
break;
|
||||
case 12: //遍历显示表
|
||||
case 12: //遍历显示表
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 13:
|
||||
cout << "测试顺序表为\n";
|
||||
cout << "测试顺序表为\n";
|
||||
DispList(L);
|
||||
cout << "输入查找前驱的元素值:\n";
|
||||
cout << "输入查找前驱的元素值:\n";
|
||||
cin >> e;
|
||||
if (PriorElem_e(L, e, pre_e))
|
||||
cout << e << "的前驱元素为:" << pre_e << endl;
|
||||
cout << e << "的前驱元素为:" << pre_e << endl;
|
||||
else
|
||||
cout << e << "无前驱元素!" << endl;
|
||||
cout << e << "无前驱元素!" << endl;
|
||||
break;
|
||||
case 0: //退出
|
||||
case 0: //退出
|
||||
|
||||
cout << "结束运行bye-bye!" << endl;
|
||||
cout << "结束运行bye-bye!" << endl;
|
||||
break;
|
||||
default: //无效选择
|
||||
cout << "无效选择!\n";
|
||||
default: //无效选择
|
||||
cout << "无效选择!\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
template <class DT>
|
||||
struct SqList // 顺序表
|
||||
struct SqList // 顺序表
|
||||
{
|
||||
DT* elem; // 表首址
|
||||
int length; // 表长
|
||||
int size; // 表容量
|
||||
DT* elem; // 表首址
|
||||
int length; // 表长
|
||||
int size; // 表容量
|
||||
};
|
||||
|
||||
//算法2.1
|
||||
//算法2.1
|
||||
template <class DT>
|
||||
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
|
||||
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
|
||||
{
|
||||
int k;
|
||||
k = LocateElem_e(L, e); //
|
||||
@@ -21,119 +21,119 @@ bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) //
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.2】 初始化
|
||||
//【算法2.2】 初始化
|
||||
template <class DT>
|
||||
bool InitList(SqList<DT>& L, int m)
|
||||
{
|
||||
L.elem = new DT[m]; // 申请表空间
|
||||
L.elem = new DT[m]; // 申请表空间
|
||||
if (L.elem == NULL)
|
||||
{
|
||||
cout << "未创建成功!"; // 申请不成功,退出
|
||||
cout << "未创建成功!"; // 申请不成功,退出
|
||||
exit(1);
|
||||
}
|
||||
L.length = 0; // 申请成功,属性赋值。空表,表长为0
|
||||
L.size = m; // 表容量为m
|
||||
L.length = 0; // 申请成功,属性赋值。空表,表长为0
|
||||
L.size = m; // 表容量为m
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.3】 创建表元素
|
||||
//【算法2.3】 创建表元素
|
||||
template <class DT>
|
||||
bool CreateList(SqList<DT>& L, int n)
|
||||
{
|
||||
int i;
|
||||
if (n > L.size) // 1.元素个数大于表容量,不能创建。
|
||||
if (n > L.size) // 1.元素个数大于表容量,不能创建。
|
||||
{
|
||||
cout << "元素个数大于表长,不能创建!" << endl;
|
||||
cout << "元素个数大于表长,不能创建!" << endl;
|
||||
return false;
|
||||
}
|
||||
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
|
||||
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
|
||||
for (i = 1; i <= n; i++)
|
||||
cin >> L.elem[i - 1];
|
||||
L.length = n; // 3.表长为创建的元素个数
|
||||
L.length = n; // 3.表长为创建的元素个数
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.4】 销毁顺序表
|
||||
//【算法2.4】 销毁顺序表
|
||||
template <class DT>
|
||||
void DestroyList(SqList<DT>& L)
|
||||
{
|
||||
delete[] L.elem; // 1.释放表空间
|
||||
L.length = 0; // 2.属性赋值
|
||||
delete[] L.elem; // 1.释放表空间
|
||||
L.length = 0; // 2.属性赋值
|
||||
L.size = 0;
|
||||
}
|
||||
|
||||
//【算法2.5】 获取第i个元素值
|
||||
//【算法2.5】 获取第i个元素值
|
||||
template<class DT>
|
||||
bool GetElem_i(SqList<DT> L, int i, DT& e)
|
||||
{
|
||||
if (i<1 || i>L.length) // 1.位序不合理,返回false
|
||||
if (i<1 || i>L.length) // 1.位序不合理,返回false
|
||||
{
|
||||
cout << "该元素不存在!" << endl;
|
||||
cout << "该元素不存在!" << endl;
|
||||
return false;
|
||||
}
|
||||
e = L.elem[i - 1]; // 2. 否则,获取第i个元素值
|
||||
return true; // 返回true
|
||||
e = L.elem[i - 1]; // 2. 否则,获取第i个元素值
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
//【算法2.6】 按值查找
|
||||
//【算法2.6】 按值查找
|
||||
template<class DT>
|
||||
int LocateElem_e(SqList<DT> L, DT e)
|
||||
{
|
||||
for (int i = 0; i < L.length; i++) // 顺序查找
|
||||
if (L.elem[i] == e) // 1.找到
|
||||
return i + 1; // 返回元素位序
|
||||
return 0; // 2.未找到,返回0
|
||||
for (int i = 1; i <= L.length; i++) // 顺序查找
|
||||
if (L.elem[i - 1] == e) // 1.找到
|
||||
return i; // 返回元素位序
|
||||
return 0; // 2.未找到,返回0
|
||||
}
|
||||
|
||||
//【算法2.7】
|
||||
//【算法2.7】
|
||||
template<class DT>
|
||||
bool InsertElem_i(SqList<DT>& L, int i, DT e)
|
||||
{
|
||||
if (L.length >= L.size) // 1.表满,不能插入
|
||||
if (L.length >= L.size) // 1.表满,不能插入
|
||||
return false;
|
||||
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
|
||||
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
|
||||
return false;
|
||||
for (int j = L.length; j >= i; j--) // 3. an~ai依次后移
|
||||
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
|
||||
return true; // 插入成功,返回true
|
||||
}
|
||||
|
||||
//【算法2.8】 删除第i个元素
|
||||
//【算法2.8】 删除第i个元素
|
||||
template<class DT>
|
||||
bool DeleElem_i(SqList<DT>& L, int i)
|
||||
{
|
||||
if (L.length == 0) // 1.表空,不能删除
|
||||
if (L.length == 0) // 1.表空,不能删除
|
||||
return false;
|
||||
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
|
||||
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
|
||||
return false;
|
||||
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
|
||||
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
|
||||
L.elem[j - 1] = L.elem[j];
|
||||
L.length--;
|
||||
return true; // 删除成功,返回true
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
|
||||
//【算法2.9】
|
||||
//【算法2.9】
|
||||
template<class DT>
|
||||
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
|
||||
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
|
||||
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
|
||||
L.length = 0; // 空表,表长为0
|
||||
}
|
||||
|
||||
|
||||
// 测表长
|
||||
// 测表长
|
||||
template<class DT>
|
||||
int ListLength(SqList<DT> L)
|
||||
{
|
||||
@@ -142,31 +142,31 @@ int ListLength(SqList<DT> L)
|
||||
|
||||
|
||||
template<class DT>
|
||||
bool ListEmpty(SqList<DT> L) // 测表空
|
||||
bool ListEmpty(SqList<DT> L) // 测表空
|
||||
{
|
||||
if (L.length == 0) // 空表,返回true
|
||||
if (L.length == 0) // 空表,返回true
|
||||
return true;
|
||||
else
|
||||
return false; // 非空表,返回false
|
||||
return false; // 非空表,返回false
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool ListFull(SqList<DT> L)
|
||||
{
|
||||
if (L.length == L.size) // 表满,返回true
|
||||
if (L.length == L.size) // 表满,返回true
|
||||
return true;
|
||||
else
|
||||
return false; // 表不满,返回false
|
||||
return false; // 表不满,返回false
|
||||
}
|
||||
|
||||
//【算法2.10】 遍历输出
|
||||
//【算法2.10】 遍历输出
|
||||
template <class DT>
|
||||
void DispList(SqList<DT> L)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < L.length; i++) // 依位序输出元素值
|
||||
for (i = 1; i <= L.length; i++) // 依位序输出元素值
|
||||
{
|
||||
cout << L.elem[i] << "\t";
|
||||
cout << L.elem[i - 1] << "\t";
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
@@ -1,186 +1,186 @@
|
||||
// 2-2-LinkList-单链表
|
||||
// 2-2-LinkList-单链表
|
||||
// WARNING: /sdl is disabled to pass the compilation process.
|
||||
|
||||
#include<iostream> //cout,cin
|
||||
using namespace std;
|
||||
#include "LinkList.h"
|
||||
|
||||
//算法2.25 单链表逆置
|
||||
//算法2.25 单链表逆置
|
||||
void ReverseLinkList(LNode<int>*& L)
|
||||
{
|
||||
LNode<int>* p, * q; // 1.设置工作指针
|
||||
p = L->next; // 原链表头结点,作为逆置后表的头结点
|
||||
LNode<int>* p, * q; // 1.设置工作指针
|
||||
p = L->next; // 原链表头结点,作为逆置后表的头结点
|
||||
L->next = NULL;
|
||||
while (p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中
|
||||
while (p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中
|
||||
{
|
||||
q = p; // 2.1 q取当前结点位置
|
||||
p = p->next; // 2.2 p指向下一个待处理结点
|
||||
q->next = L->next; // 2.3 将q 插入到头结点之后
|
||||
q = p; // 2.1 q取当前结点位置
|
||||
p = p->next; // 2.2 p指向下一个待处理结点
|
||||
q->next = L->next; // 2.3 将q 插入到头结点之后
|
||||
L->next = q;
|
||||
}
|
||||
}
|
||||
|
||||
void dispmenu()
|
||||
{ // 显示主菜单
|
||||
{ // 显示主菜单
|
||||
cout << endl;
|
||||
cout << " *** 单 链 表 ***\n";
|
||||
cout << " 1-初始化单链表\n";
|
||||
cout << " 2-尾插法顺序表\n";
|
||||
cout << " 3-头插法建表\n";
|
||||
cout << " 4-获取第i个元素\n";
|
||||
cout << " 5-按值查找\n";
|
||||
cout << " 6-插入第i个元素\n";
|
||||
cout << " 7-删除第i个元素\n";
|
||||
cout << " 8-修改第i个元素\n";
|
||||
cout << " 9-清空表\n";
|
||||
cout << "10-测表长\n";
|
||||
cout << "11-测表空\n";
|
||||
cout << "12-遍历输出\n";
|
||||
cout << "13-按值查找前驱\n";
|
||||
cout << "14-单链表逆置\n";
|
||||
cout << "0-退出\n";
|
||||
cout << " *** 单 链 表 ***\n";
|
||||
cout << " 1-初始化单链表\n";
|
||||
cout << " 2-尾插法顺序表\n";
|
||||
cout << " 3-头插法建表\n";
|
||||
cout << " 4-获取第i个元素\n";
|
||||
cout << " 5-按值查找\n";
|
||||
cout << " 6-插入第i个元素\n";
|
||||
cout << " 7-删除第i个元素\n";
|
||||
cout << " 8-修改第i个元素\n";
|
||||
cout << " 9-清空表\n";
|
||||
cout << "10-测表长\n";
|
||||
cout << "11-测表空\n";
|
||||
cout << "12-遍历输出\n";
|
||||
cout << "13-按值查找前驱\n";
|
||||
cout << "14-单链表逆置\n";
|
||||
cout << "0-退出\n";
|
||||
}
|
||||
|
||||
char pause;
|
||||
|
||||
// 主函数
|
||||
// 主函数
|
||||
int main()
|
||||
{
|
||||
int i, n;
|
||||
int e, pre_e;
|
||||
LNode<int>* L;
|
||||
system("cls"); // 清屏
|
||||
system("cls"); // 清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~12,0 退出):";
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~12,0 退出):";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: // 初始化单链表
|
||||
case 1: // 初始化单链表
|
||||
InitList(L);
|
||||
cout << endl << "创建成功!" << endl;
|
||||
cout << endl << "创建成功!" << endl;
|
||||
break;
|
||||
case 2: // 尾插法建单链表
|
||||
cout << "尾插法创建单链表" << endl;
|
||||
cout << "输入要创建的顺序表中元素个数:";
|
||||
case 2: // 尾插法建单链表
|
||||
cout << "尾插法创建单链表" << endl;
|
||||
cout << "输入要创建的顺序表中元素个数:";
|
||||
cin >> n;
|
||||
cout << endl;
|
||||
CreateList_1(L, n);
|
||||
cout << "创建的单链表为:";
|
||||
cout << "创建的单链表为:";
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
break;
|
||||
case 3: // 头插法创建单链表
|
||||
cout << "头插法创建单链表" << endl;
|
||||
cout << "输入要创建的顺序表中元素个数:";
|
||||
case 3: // 头插法创建单链表
|
||||
cout << "头插法创建单链表" << endl;
|
||||
cout << "输入要创建的顺序表中元素个数:";
|
||||
cin >> n;
|
||||
cout << endl;
|
||||
CreateList_2(L, n);
|
||||
cout << "创建的单链表为:";
|
||||
cout << "创建的单链表为:";
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
break;
|
||||
case 4: // 获取第i个元素
|
||||
cout << "请输入元素序号:";
|
||||
case 4: // 获取第i个元素
|
||||
cout << "请输入元素序号:";
|
||||
cin >> i;
|
||||
cout << endl;
|
||||
if (GetElem_i(L, i, e))
|
||||
cout << endl << "第" << i << "个元素为:" << e << endl;
|
||||
cout << endl << "第" << i << "个元素为:" << e << endl;
|
||||
else
|
||||
cout << endl << "元素不存在!" << endl;
|
||||
cout << endl << "元素不存在!" << endl;
|
||||
break;
|
||||
case 5: // 查询元素位序
|
||||
cout << "请输入要查询的元素值:";
|
||||
case 5: // 查询元素位序
|
||||
cout << "请输入要查询的元素值:";
|
||||
cin >> e;
|
||||
i = LocateElem_e(L, e);
|
||||
if (i)
|
||||
cout << endl << e << "是第" << i << "个数据元素" << endl;
|
||||
cout << endl << e << "是第" << i << "个数据元素" << endl;
|
||||
else
|
||||
cout << endl << "不存在此元素!" << endl;
|
||||
cout << endl << "不存在此元素!" << endl;
|
||||
break;
|
||||
case 6: // 插入第i个元素
|
||||
cout << "输入插入位置:" << endl;
|
||||
case 6: // 插入第i个元素
|
||||
cout << "输入插入位置:" << endl;
|
||||
cin >> i;
|
||||
cout << "输入插入元素值:" << endl;
|
||||
cout << "输入插入元素值:" << endl;
|
||||
cin >> e;
|
||||
if (InsertElem_i(L, i, e))
|
||||
{
|
||||
cout << endl << "插入成功!" << endl;
|
||||
cout << "插入元素后的单链表为:" << endl;
|
||||
cout << endl << "插入成功!" << endl;
|
||||
cout << "插入元素后的单链表为:" << endl;
|
||||
DispList(L);
|
||||
}
|
||||
else
|
||||
cout << endl << "插入不成功!" << endl;
|
||||
cout << endl << "插入不成功!" << endl;
|
||||
break;
|
||||
case 7: // 删除第i个元素
|
||||
cout << "输入删除元素位置:" << endl;
|
||||
case 7: // 删除第i个元素
|
||||
cout << "输入删除元素位置:" << endl;
|
||||
cin >> i;
|
||||
if (DeleElem_i(L, i))
|
||||
{
|
||||
cout << endl << "删除成功!" << endl;
|
||||
cout << "删除元素后的单链表为:" << endl;
|
||||
cout << endl << "删除成功!" << endl;
|
||||
cout << "删除元素后的单链表为:" << endl;
|
||||
DispList(L);
|
||||
}
|
||||
else
|
||||
cout << endl << "删除失败!" << endl;
|
||||
cout << endl << "删除失败!" << endl;
|
||||
break;
|
||||
case 8: // 修改第i个元素
|
||||
cout << "输入修改元素位置:" << endl;
|
||||
case 8: // 修改第i个元素
|
||||
cout << "输入修改元素位置:" << endl;
|
||||
cin >> i;
|
||||
cout << "输入新元素值:" << endl;
|
||||
cout << "输入新元素值:" << endl;
|
||||
cin >> e;
|
||||
if (PutElem_i(L, i, e))
|
||||
{
|
||||
cout << endl << "修改成功!" << endl;
|
||||
cout << "修改后的单链表为:" << endl;
|
||||
cout << endl << "修改成功!" << endl;
|
||||
cout << "修改后的单链表为:" << endl;
|
||||
DispList(L);
|
||||
}
|
||||
else
|
||||
cout << endl << "修改失败!" << endl;
|
||||
cout << endl << "修改失败!" << endl;
|
||||
break;
|
||||
case 9: // 清空表
|
||||
case 9: // 清空表
|
||||
ClearList(L);
|
||||
break;
|
||||
case 10: // 测表长
|
||||
cout << "表长为:" << ListLength(L) << endl;
|
||||
case 10: // 测表长
|
||||
cout << "表长为:" << ListLength(L) << endl;
|
||||
break;
|
||||
case 11: // 测表空
|
||||
case 11: // 测表空
|
||||
if (ListEmpty(L))
|
||||
cout << endl << "空表!" << endl;
|
||||
cout << endl << "空表!" << endl;
|
||||
else
|
||||
cout << endl << "不是空表!" << endl;
|
||||
cout << endl << "不是空表!" << endl;
|
||||
break;
|
||||
case 12: //遍历显示表
|
||||
case 12: //遍历显示表
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
break;
|
||||
case 13:
|
||||
cout << "测试链表为\n";
|
||||
cout << "测试链表为\n";
|
||||
DispList(L);
|
||||
cout << "输入查找前驱的元素值:\n";
|
||||
cout << "输入查找前驱的元素值:\n";
|
||||
cin >> e;
|
||||
if (PriorElem_e(L, e, pre_e))
|
||||
cout << e << "的前驱元素为:" << pre_e << endl;
|
||||
cout << e << "的前驱元素为:" << pre_e << endl;
|
||||
else
|
||||
cout << e << "无前驱元素!" << endl;
|
||||
cout << e << "无前驱元素!" << endl;
|
||||
break;
|
||||
case 14: // 逆置单链表
|
||||
cout << "逆置前的单链表为:";
|
||||
case 14: // 逆置单链表
|
||||
cout << "逆置前的单链表为:";
|
||||
DispList(L);
|
||||
ReverseLinkList(L);
|
||||
cout << "逆置后的单链表为:";
|
||||
cout << "逆置后的单链表为:";
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
break;
|
||||
case 0: //退出,销毁链表
|
||||
case 0: //退出,销毁链表
|
||||
DestroyList(L);
|
||||
cout << "结束运行bye-bye!" << endl;
|
||||
cout << "结束运行bye-bye!" << endl;
|
||||
break;
|
||||
default: //无效选择
|
||||
cout << "无效选择!\n";
|
||||
default: //无效选择
|
||||
cout << "无效选择!\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
@@ -1,249 +1,249 @@
|
||||
|
||||
template <class DT>
|
||||
struct LNode //链表结点
|
||||
struct LNode //链表结点
|
||||
{
|
||||
DT data; //数据域,存储数据元素值
|
||||
LNode* next; //指针域,指向下一个结点
|
||||
DT data; //数据域,存储数据元素值
|
||||
LNode* next; //指针域,指向下一个结点
|
||||
};
|
||||
|
||||
//算法2.1
|
||||
//算法2.1
|
||||
template <class DT>
|
||||
bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱
|
||||
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
|
||||
k = LocateElem_e(L, e); // 1.获取e的位序k
|
||||
if (k > 1) // 2.位序k大于1
|
||||
{
|
||||
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
|
||||
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
|
||||
return true;
|
||||
}
|
||||
else // 3.元素e无前驱
|
||||
return false; // 返回false
|
||||
else // 3.元素e无前驱
|
||||
return false; // 返回false
|
||||
}
|
||||
|
||||
//【算法2.14】 创建空单链表
|
||||
//【算法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
|
||||
L = new LNode<DT>; // 1.创建头结点
|
||||
if (!L) exit(1); // 2.创建失败,退出
|
||||
L->next = NULL; // 3.创建成功
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
//【算法2.15】 尾插法创建n的元素
|
||||
//【算法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.按元素位序正序创建各结点
|
||||
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
|
||||
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 链在表尾
|
||||
cin >> s->data; // 2.3 输入结点值
|
||||
s->next = p->next; // 2.4 s 链在表尾
|
||||
p->next = s;
|
||||
p = s; // 2.5 工作指针指向 s
|
||||
p = s; // 2.5 工作指针指向 s
|
||||
}
|
||||
return true; // 3.创建成功,返回true
|
||||
return true; // 3.创建成功,返回true
|
||||
}
|
||||
|
||||
//【算法2.16】 头插法创建n个元素
|
||||
//【算法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.按元素位序逆序创建各结点
|
||||
cout << "逆序输入" << n << "个数据元素:" << endl;
|
||||
for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
|
||||
{
|
||||
s = new LNode<DT>; // 1.1 新建一个结点 s
|
||||
if (!s) // 1.2 创建失败,返回false
|
||||
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 在头结点后
|
||||
cin >> s->data; // 1.3 输入结点值
|
||||
s->next = L->next; // 1.4 s 在头结点后
|
||||
L->next = s;
|
||||
}
|
||||
return true; // 1.创建成功,返回true
|
||||
return true; // 1.创建成功,返回true
|
||||
}
|
||||
|
||||
//【算法2.17】
|
||||
//【算法2.17】
|
||||
template <class DT>
|
||||
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
|
||||
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
|
||||
{
|
||||
LNode<DT>* p;
|
||||
while (L) // 1. 表非空,从头结点开始,依次释放结点
|
||||
while (L) // 1. 表非空,从头结点开始,依次释放结点
|
||||
{
|
||||
p = L; // 1.1 处理表头结点
|
||||
L = L->next; // 1.2 头指针后移
|
||||
delete p; // 1.3 释放表头结点所占内存
|
||||
p = L; // 1.1 处理表头结点
|
||||
L = L->next; // 1.2 头指针后移
|
||||
delete p; // 1.3 释放表头结点所占内存
|
||||
}
|
||||
L = NULL; // 2.头指针指向空
|
||||
L = NULL; // 2.头指针指向空
|
||||
}
|
||||
|
||||
//【算法2.18】 获取第i个元素
|
||||
//【算法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个元素结点
|
||||
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
|
||||
if (!p || j > i) // 3 未找到,返回false
|
||||
return false;
|
||||
else // 4. 找到
|
||||
else // 4. 找到
|
||||
{
|
||||
e = p->data; // 获取第i个元素值
|
||||
return true; // 返回true
|
||||
e = p->data; // 获取第i个元素值
|
||||
return true; // 返回true
|
||||
}
|
||||
}
|
||||
|
||||
//【算法2.19】 查找值为e的元素位序
|
||||
//【算法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.顺序查找
|
||||
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
|
||||
p = p->next; // 2.1未找到指针后移
|
||||
j++; // 2.2 计数器增1
|
||||
}
|
||||
if (p == NULL) // 3. 判断是否找到
|
||||
return 0; // 3.1末找到,返回0
|
||||
if (p == NULL) // 3. 判断是否找到
|
||||
return 0; // 3.1末找到,返回0
|
||||
else
|
||||
return j; // 3.2 找到,返回位序
|
||||
return j; // 3.2 找到,返回位序
|
||||
}
|
||||
|
||||
//【算法2.20】 插入第i个元素
|
||||
//【算法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. 定位到插入点前驱
|
||||
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 定位成功
|
||||
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结点之后
|
||||
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
|
||||
return true; // 3.2.4 插入成功,返回true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//【算法2.21】 删除第i个元素
|
||||
//【算法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定位到删除点的前驱
|
||||
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.删除操作
|
||||
if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
|
||||
return false; //返回false
|
||||
else //4.删除操作
|
||||
{
|
||||
q = p->next; //4.1暂存删除结点位置
|
||||
p->next = q->next; //4.2从链表中摘除删除结点
|
||||
q = p->next; //4.1暂存删除结点位置
|
||||
p->next = q->next; //4.2从链表中摘除删除结点
|
||||
delete q;
|
||||
return true; //4.3删除成功,返回true
|
||||
return true; //4.3删除成功,返回true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//【算法2.22】 修改第i个元素值
|
||||
//【算法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个元素结点
|
||||
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
|
||||
if (!p || j > i) // 3.元素不存在,返回false
|
||||
return false;
|
||||
else // 4.定位成功
|
||||
else // 4.定位成功
|
||||
{
|
||||
p->data = e; // 修改元素值
|
||||
return true; // 返回true
|
||||
p->data = e; // 修改元素值
|
||||
return true; // 返回true
|
||||
}
|
||||
}
|
||||
|
||||
// 释放链表所占空间
|
||||
// 释放链表所占空间
|
||||
template<class DT>
|
||||
void ClearList(LNode<DT>* (&L))
|
||||
{
|
||||
|
||||
LNode<DT>* p;
|
||||
while (L->next) // 从首元结点开始,依次释放结点
|
||||
while (L->next) // 从首元结点开始,依次释放结点
|
||||
{
|
||||
p = L->next;
|
||||
L->next = p->next;
|
||||
delete p;
|
||||
}
|
||||
cout << endl << "表已清空!" << endl;
|
||||
cout << endl << "表已清空!" << endl;
|
||||
}
|
||||
|
||||
|
||||
//【算法2.23】 测表长
|
||||
//【算法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.数结点个数。有后继结点,
|
||||
{ // 1.初始化
|
||||
int len = 0; // 1.1 结点计数器赋初值0
|
||||
LNode<DT>* p; // 1.2设置工作指针
|
||||
p = L; // 指向头结点
|
||||
while (p->next) // 2.数结点个数。有后继结点,
|
||||
{
|
||||
len++; p = p->next; // 结点数增1,指针后移
|
||||
len++; p = p->next; // 结点数增1,指针后移
|
||||
}
|
||||
return len; // 3.返回表长
|
||||
return len; // 3.返回表长
|
||||
}
|
||||
|
||||
//
|
||||
template<class DT>
|
||||
bool ListEmpty(LNode<DT>* L) //测表空
|
||||
bool ListEmpty(LNode<DT>* L) //测表空
|
||||
{
|
||||
if (L->next == NULL)
|
||||
return true; //空表,返回1
|
||||
return true; //空表,返回1
|
||||
else
|
||||
return false; //不空,返回0
|
||||
return false; //不空,返回0
|
||||
}
|
||||
|
||||
|
||||
//【算法2.24】 遍历表
|
||||
//【算法2.24】 遍历表
|
||||
template <class DT>
|
||||
void DispList(LNode<DT>* L) // 显示表内容
|
||||
void DispList(LNode<DT>* L) // 显示表内容
|
||||
{
|
||||
LNode<DT>* p; // 1. 设置工作指针
|
||||
p = L; // 从首元结点开始遍历
|
||||
while (p->next) // 2.依次输出各结点值
|
||||
LNode<DT>* p; // 1. 设置工作指针
|
||||
p = L; // 从首元结点开始遍历
|
||||
while (p->next) // 2.依次输出各结点值
|
||||
{
|
||||
p = p->next; cout << p->data << "\t";
|
||||
|
||||
|
||||
@@ -1,55 +1,55 @@
|
||||
template <class DT>
|
||||
struct SqList //顺序表类
|
||||
struct SqList //顺序表类
|
||||
{
|
||||
DT* elem; //表首址
|
||||
int length; //表长
|
||||
int size;//表容量
|
||||
DT* elem; //表首址
|
||||
int length; //表长
|
||||
int size;//表容量
|
||||
};
|
||||
|
||||
//【算法2.2】
|
||||
//【算法2.2】
|
||||
template <class DT>
|
||||
bool InitList(SqList<DT>& L, int m)
|
||||
{//构建函数,创建一表容量为m的空表
|
||||
L.elem = new DT[m];// 申请表空间
|
||||
{//构建函数,创建一表容量为m的空表
|
||||
L.elem = new DT[m];// 申请表空间
|
||||
if (L.elem == NULL)
|
||||
{
|
||||
cout << "未创建成功!";
|
||||
cout << "未创建成功!";
|
||||
exit(1);
|
||||
}
|
||||
L.length = 0; // 空表,表长为0
|
||||
L.size = m; //表容量为m
|
||||
L.length = 0; // 空表,表长为0
|
||||
L.size = m; //表容量为m
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.3】
|
||||
//【算法2.3】
|
||||
template <class DT>
|
||||
bool CreateList(SqList<DT>& L, int n) //创建表长度为n的顺序表
|
||||
bool CreateList(SqList<DT>& L, int n) //创建表长度为n的顺序表
|
||||
{
|
||||
int i;
|
||||
if (n > L.size)
|
||||
{
|
||||
cout << "元素个数大于表长,不能创建!" << endl;
|
||||
cout << "元素个数大于表长,不能创建!" << endl;
|
||||
return false;
|
||||
}
|
||||
cout << "请依次输入" << n << "个元素值:" << endl;
|
||||
cout << "请依次输入" << n << "个元素值:" << endl;
|
||||
for (i = 1; i <= n; i++)
|
||||
cin >> L.elem[i - 1];
|
||||
L.length = n;
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.4】
|
||||
//【算法2.4】
|
||||
template <class DT>
|
||||
void DestroyList(SqList<DT>& L) //销毁顺序表,释放表空间
|
||||
void DestroyList(SqList<DT>& L) //销毁顺序表,释放表空间
|
||||
{
|
||||
delete[] L.elem;
|
||||
L.length = 0;
|
||||
L.size = 0;
|
||||
}
|
||||
|
||||
//【算法2.5】
|
||||
//【算法2.5】
|
||||
template<class DT>
|
||||
bool GetElem_i(SqList<DT> L, int i, DT& e)// 获取第i个元素值
|
||||
bool GetElem_i(SqList<DT> L, int i, DT& e)// 获取第i个元素值
|
||||
{
|
||||
if (i<0 || i>L.length)
|
||||
return false;
|
||||
@@ -57,60 +57,60 @@ bool GetElem_i(SqList<DT> L, int i, DT& e)//
|
||||
return true;
|
||||
}
|
||||
|
||||
//【算法2.6】
|
||||
//【算法2.6】
|
||||
template<class DT>
|
||||
int LocateElem_e(SqList<DT> L, DT e) // 按值查找
|
||||
int LocateElem_e(SqList<DT> L, DT e) // 按值查找
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < L.length; i++) // 顺序查找
|
||||
if (L.elem[i] == e) // 找到
|
||||
for (i = 0; i < L.length; i++) // 顺序查找
|
||||
if (L.elem[i] == e) // 找到
|
||||
return i + 1;
|
||||
return 0; // 未找到
|
||||
return 0; // 未找到
|
||||
}
|
||||
|
||||
//【算法2.7】
|
||||
//【算法2.7】
|
||||
template<class DT>
|
||||
bool InsertElem_i(SqList<DT>& L, int i, DT e) // 在第i个位置插入新元素
|
||||
bool InsertElem_i(SqList<DT>& L, int i, DT e) // 在第i个位置插入新元素
|
||||
{
|
||||
int j;
|
||||
if (L.length >= L.size) //1.表满,不能插入
|
||||
if (L.length >= L.size) //1.表满,不能插入
|
||||
return false;
|
||||
if (i<1 || i>L.length + 1) //2.插入位置不合理,不能插入
|
||||
if (i<1 || i>L.length + 1) //2.插入位置不合理,不能插入
|
||||
return false;
|
||||
for (j = L.length; j >= i; j--) //3. an~ai依次后移
|
||||
for (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
|
||||
return true; // 插入成功,返回true
|
||||
}
|
||||
|
||||
//【算法2.8】
|
||||
//【算法2.8】
|
||||
template<class DT>
|
||||
bool DeleElem_i(SqList<DT>& L, int i) // 删除第i个元素
|
||||
bool DeleElem_i(SqList<DT>& L, int i) // 删除第i个元素
|
||||
{
|
||||
int j;
|
||||
if (L.length == 0) //1.表空,不能删除
|
||||
if (L.length == 0) //1.表空,不能删除
|
||||
return false;
|
||||
if (i<1 || i>L.length) //2.删除位置不合理,不能插入
|
||||
if (i<1 || i>L.length) //2.删除位置不合理,不能插入
|
||||
return false;
|
||||
for (j = i; j < L.length; j++) //3. ai~an依次前移
|
||||
for (j = i; j < L.length; j++) //3. ai~an依次前移
|
||||
L.elem[j - 1] = L.elem[j];
|
||||
L.length--;
|
||||
return true; // 删除成功,返回true
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
|
||||
//【算法2.9】
|
||||
//【算法2.9】
|
||||
template<class DT>
|
||||
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
|
||||
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
|
||||
{
|
||||
if (i<1 || i>L.length) //1.位置不合理,不能修改
|
||||
if (i<1 || i>L.length) //1.位置不合理,不能修改
|
||||
return false;
|
||||
L.elem[i - 1] = e; //2.重置第i个元素值
|
||||
return true; //3.修改成功,返回true
|
||||
L.elem[i - 1] = e; //2.重置第i个元素值
|
||||
return true; //3.修改成功,返回true
|
||||
}
|
||||
|
||||
//清空顺序表
|
||||
//清空顺序表
|
||||
template<class DT>
|
||||
void ClearList(SqList<DT>& L)
|
||||
{
|
||||
@@ -118,14 +118,14 @@ void ClearList(SqList<DT>& L)
|
||||
}
|
||||
|
||||
|
||||
//测表长
|
||||
//测表长
|
||||
template<class DT>
|
||||
int ListLength(SqList<DT> L)
|
||||
{
|
||||
return L.length;
|
||||
}
|
||||
|
||||
//测表空
|
||||
//测表空
|
||||
template<class DT>
|
||||
bool ListEmpty(SqList<DT> L)
|
||||
{
|
||||
@@ -135,7 +135,7 @@ bool ListEmpty(SqList<DT> L)
|
||||
return false;
|
||||
}
|
||||
|
||||
//测表满
|
||||
//测表满
|
||||
template<class DT>
|
||||
bool ListFull(SqList<DT> L)
|
||||
{
|
||||
@@ -145,9 +145,9 @@ bool ListFull(SqList<DT> L)
|
||||
return false;
|
||||
}
|
||||
|
||||
//【算法2.10】
|
||||
//【算法2.10】
|
||||
template <class DT>
|
||||
void DispList(SqList<DT> L) //遍历输出
|
||||
void DispList(SqList<DT> L) //遍历输出
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < L.length; i++)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// 2-3-SqListApp-顺序表应用
|
||||
// 2-3-SqListApp-顺序表应用
|
||||
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
@@ -7,35 +7,35 @@ using namespace std;
|
||||
char pause;
|
||||
|
||||
void dispmenu()
|
||||
{ // 显示主菜单
|
||||
cout << "\n*** 顺序表的应用 ***\n";
|
||||
cout << " 1-集合并A=A∪B\n";
|
||||
cout << " 2-顺序表逆置\n";
|
||||
cout << " 3-多项式求和\n";
|
||||
cout << " 0-退出\n";
|
||||
{ // 显示主菜单
|
||||
cout << "\n*** 顺序表的应用 ***\n";
|
||||
cout << " 1-集合并A=A∪B\n";
|
||||
cout << " 2-顺序表逆置\n";
|
||||
cout << " 3-多项式求和\n";
|
||||
cout << " 0-退出\n";
|
||||
}
|
||||
|
||||
|
||||
//算法2.11
|
||||
//算法2.11
|
||||
template <class DT>
|
||||
void Union(SqList<DT>& La, SqList<DT> Lb) // 求La=La∪La
|
||||
void Union(SqList<DT>& La, SqList<DT> Lb) // 求La=La∪La
|
||||
{
|
||||
DT e;
|
||||
int k, i;
|
||||
for (i = 1; i <= Lb.length; i++) // 扫描Lb
|
||||
for (i = 1; i <= Lb.length; i++) // 扫描Lb
|
||||
{
|
||||
GetElem_i(Lb, i, e); // 1. 获取Lb的第i个元素
|
||||
if (!LocateElem_e(La, e)) // 2.如果La中无此元素
|
||||
GetElem_i(Lb, i, e); // 1. 获取Lb的第i个元素
|
||||
if (!LocateElem_e(La, e)) // 2.如果La中无此元素
|
||||
{
|
||||
k = La.length + 1; // 添加在La的表尾
|
||||
k = La.length + 1; // 添加在La的表尾
|
||||
InsertElem_i(La, k, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//算法2.12
|
||||
//算法2.12
|
||||
template <class DT>
|
||||
void ReverseSqList(SqList<DT>& L) // 顺序表元素逆置
|
||||
void ReverseSqList(SqList<DT>& L) // 顺序表元素逆置
|
||||
{
|
||||
DT t;
|
||||
int i;
|
||||
@@ -48,28 +48,28 @@ void ReverseSqList(SqList<DT>& L) // ˳
|
||||
return;
|
||||
}
|
||||
|
||||
//算法2.13 多项式求和 lc=la+lb
|
||||
//算法2.13 多项式求和 lc=la+lb
|
||||
void PolyAdd(SqList<float> la, SqList<float> lb, SqList<float>& lc)
|
||||
{
|
||||
int i = 0; // 1.初始化,设置处理起始位置
|
||||
while (i < la.length && i < lb.length) // 2.两个多项式均未处理完
|
||||
int i = 0; // 1.初始化,设置处理起始位置
|
||||
while (i < la.length && i < lb.length) // 2.两个多项式均未处理完
|
||||
{
|
||||
lc.elem[i] = la.elem[i] + lb.elem[i]; // 相同位序上的系数相加
|
||||
lc.elem[i] = la.elem[i] + lb.elem[i]; // 相同位序上的系数相加
|
||||
i++;
|
||||
|
||||
}
|
||||
if (la.length > lb.length) // 3.la未处理完,lb已处理完
|
||||
if (la.length > lb.length) // 3.la未处理完,lb已处理完
|
||||
{
|
||||
while (i < la.length) // lc取la中剩余项
|
||||
while (i < la.length) // lc取la中剩余项
|
||||
{
|
||||
lc.elem[i] = la.elem[i];
|
||||
i++;
|
||||
|
||||
}
|
||||
}
|
||||
else // 3.lb未处理完,la已处理完
|
||||
else // 3.lb未处理完,la已处理完
|
||||
{
|
||||
while (i < lb.length) // lc取lb中剩余项
|
||||
while (i < lb.length) // lc取lb中剩余项
|
||||
{
|
||||
lc.elem[i] = lb.elem[i];
|
||||
i++;
|
||||
@@ -78,7 +78,7 @@ void PolyAdd(SqList<float> la, SqList<float> lb, SqList<float>& lc)
|
||||
}
|
||||
}
|
||||
|
||||
//显示多顶式
|
||||
//显示多顶式
|
||||
void DispPoly(float A[], int n)
|
||||
{
|
||||
int i;
|
||||
@@ -91,95 +91,95 @@ void DispPoly(float A[], int n)
|
||||
|
||||
|
||||
|
||||
//主函数
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int e;
|
||||
int na, nb, nc;
|
||||
SqList<int> La, Lb; // 集合A、B
|
||||
SqList<int> Lc; // 顺序表
|
||||
SqList<float> fa, fb, fc; // 多项式A、B、C
|
||||
SqList<int> La, Lb; // 集合A、B
|
||||
SqList<int> Lc; // 顺序表
|
||||
SqList<float> fa, fb, fc; // 多项式A、B、C
|
||||
|
||||
system("cls"); // 清屏
|
||||
system("cls"); // 清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~4,0退出):";
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~4,0退出):";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: // 求集合并
|
||||
cout << "创建集合A、B\n";
|
||||
cout << "输入集合A元素个数:";
|
||||
case 1: // 求集合并
|
||||
cout << "创建集合A、B\n";
|
||||
cout << "输入集合A元素个数:";
|
||||
cin >> na;
|
||||
cout << "输入集合B元素个数:";
|
||||
cout << "输入集合B元素个数:";
|
||||
cin >> nb;
|
||||
InitList(La, na + nb); // 创建集合A
|
||||
cout << "创建集合A的元素\n";
|
||||
InitList(La, na + nb); // 创建集合A
|
||||
cout << "创建集合A的元素\n";
|
||||
CreateList(La, na);
|
||||
InitList(Lb, nb); // 创建集合B
|
||||
cout << "创建集合B的元素\n";
|
||||
InitList(Lb, nb); // 创建集合B
|
||||
cout << "创建集合B的元素\n";
|
||||
CreateList(Lb, nb);
|
||||
cout << "集合A为:" << endl; // 显示集合A
|
||||
cout << "集合A为:" << endl; // 显示集合A
|
||||
DispList(La);
|
||||
cout << "集合B为:" << endl; // 显示集合B
|
||||
cout << "集合B为:" << endl; // 显示集合B
|
||||
DispList(Lb);
|
||||
Union(La, Lb); // 求集合并
|
||||
cout << "A∪B为:" << endl; // 显示结果
|
||||
Union(La, Lb); // 求集合并
|
||||
cout << "A∪B为:" << endl; // 显示结果
|
||||
DispList(La);
|
||||
cout << endl;
|
||||
DestroyList(La);
|
||||
DestroyList(Lb);
|
||||
break;
|
||||
|
||||
case 2: // 顺序表逆置
|
||||
cout << "请输入要创建的顺序表中元素个数:";
|
||||
case 2: // 顺序表逆置
|
||||
cout << "请输入要创建的顺序表中元素个数:";
|
||||
cin >> nc;
|
||||
InitList(Lc, nc);
|
||||
cout << endl;
|
||||
CreateList(Lc, nc);
|
||||
cout << "创建的顺序表为:" << endl; // 显示集合A
|
||||
cout << "创建的顺序表为:" << endl; // 显示集合A
|
||||
DispList(Lc);
|
||||
ReverseSqList(Lc);
|
||||
cout << "逆置后的顺序表为:" << endl; // 显示集合A
|
||||
cout << "逆置后的顺序表为:" << endl; // 显示集合A
|
||||
DispList(Lc);
|
||||
cout << endl;
|
||||
DestroyList(Lc);
|
||||
break;
|
||||
case 3: //多项式求和
|
||||
cout << "\n创建多项式A\n"; // 创建多项式A
|
||||
cout << "输入多项式A的项数:";
|
||||
case 3: //多项式求和
|
||||
cout << "\n创建多项式A\n"; // 创建多项式A
|
||||
cout << "输入多项式A的项数:";
|
||||
cin >> na;
|
||||
InitList(fa, na);
|
||||
cout << "按幂升序输入多项式A各项系数\n";
|
||||
cout << "按幂升序输入多项式A各项系数\n";
|
||||
CreateList(fa, na);
|
||||
cout << "\n创建多项式B\n"; // 创建多项式B
|
||||
cout << "输入多项式B的项数:";
|
||||
cout << "\n创建多项式B\n"; // 创建多项式B
|
||||
cout << "输入多项式B的项数:";
|
||||
cin >> nb;
|
||||
InitList(fb, nb);
|
||||
cout << "按幂升序输入多项式B各项系数\n";
|
||||
cout << "按幂升序输入多项式B各项系数\n";
|
||||
CreateList(fb, nb);
|
||||
cout << "\n多项式 A 为 :" << endl; // 显示多项式A
|
||||
cout << "\n多项式 A 为 :" << endl; // 显示多项式A
|
||||
DispPoly(fa.elem, na);
|
||||
cout << "\n多项式 B 为 :" << endl; // 显示多项式B
|
||||
cout << "\n多项式 B 为 :" << endl; // 显示多项式B
|
||||
DispPoly(fb.elem, nb);
|
||||
nc = (na >= nb) ? na : nb;
|
||||
InitList(fc, nc); // 创建多项式C
|
||||
PolyAdd(fa, fb, fc); // 求多项式 C=A+B
|
||||
cout << "\n多项式A + 多项式B = " << endl; // 显示结果
|
||||
InitList(fc, nc); // 创建多项式C
|
||||
PolyAdd(fa, fb, fc); // 求多项式 C=A+B
|
||||
cout << "\n多项式A + 多项式B = " << endl; // 显示结果
|
||||
DispPoly(fc.elem, nc);
|
||||
cout << endl;
|
||||
DestroyList(fa);
|
||||
DestroyList(fb);
|
||||
DestroyList(fc);
|
||||
break;
|
||||
case 0: // 退出
|
||||
cout << " 结束运行bye-bye!" << endl;
|
||||
case 0: // 退出
|
||||
cout << " 结束运行bye-bye!" << endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout << "无效选择!\n";
|
||||
default: // 无效选择
|
||||
cout << "无效选择!\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
// 2-4-PolyAdd-稀疏多项式求和
|
||||
// 2-4-PolyAdd-稀疏多项式求和
|
||||
// WARNING: /sdl is disabled to pass the compilation process.
|
||||
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
|
||||
struct PolyNode // 多项式结点
|
||||
struct PolyNode // 多项式结点
|
||||
{
|
||||
float coef; // 系数
|
||||
int exp; // 指数
|
||||
PolyNode* next; // 指向下一项结点
|
||||
float coef; // 系数
|
||||
int exp; // 指数
|
||||
PolyNode* next; // 指向下一项结点
|
||||
};
|
||||
|
||||
void InitPoly(PolyNode*& L)
|
||||
{ //创建一空多项式
|
||||
{ //创建一空多项式
|
||||
L = new PolyNode;
|
||||
L->next = NULL;
|
||||
}
|
||||
|
||||
bool CreatePoly(PolyNode*& L, int n) // 尾插法创建n项多项式
|
||||
bool CreatePoly(PolyNode*& L, int n) // 尾插法创建n项多项式
|
||||
{
|
||||
int i;
|
||||
PolyNode* p, * s;
|
||||
p = L;
|
||||
for (i = 1; i <= n; i++) // 按幂升序依次输入多项式各项系数与幂指数
|
||||
for (i = 1; i <= n; i++) // 按幂升序依次输入多项式各项系数与幂指数
|
||||
{
|
||||
s = new PolyNode;
|
||||
if (!s)
|
||||
return false;
|
||||
cout << "输入第" << i << "项系数和幂指数:";
|
||||
cout << "输入第" << i << "项系数和幂指数:";
|
||||
cin >> s->coef >> s->exp;
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
@@ -37,13 +37,13 @@ bool CreatePoly(PolyNode*& L, int n) // β
|
||||
}
|
||||
|
||||
|
||||
//显示多顶式
|
||||
void DispPoly(PolyNode* L) // 通过遍历结点,输出多项式
|
||||
//显示多顶式
|
||||
void DispPoly(PolyNode* L) // 通过遍历结点,输出多项式
|
||||
{
|
||||
PolyNode* p;
|
||||
if (!L) // 空表,返回
|
||||
if (!L) // 空表,返回
|
||||
{
|
||||
cout << "空表!";
|
||||
cout << "空表!";
|
||||
return;
|
||||
}
|
||||
p = L->next;
|
||||
@@ -56,100 +56,100 @@ void DispPoly(PolyNode* L) // ͨ
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
//【算法2.26】 求多项式LA=LA+LB
|
||||
//【算法2.26】 求多项式LA=LA+LB
|
||||
void PolyAdd(PolyNode*& LA, PolyNode*& LB)
|
||||
{
|
||||
float sum;
|
||||
PolyNode* pa, * pb, * qa, * qb; // 1.工作指针初始化
|
||||
PolyNode* pa, * pb, * qa, * qb; // 1.工作指针初始化
|
||||
pa = LA;
|
||||
qa = pa->next;
|
||||
pb = LB;
|
||||
qb = pb->next;
|
||||
while (qa != NULL && qb != NULL) // 2. 两表均不空
|
||||
while (qa != NULL && qb != NULL) // 2. 两表均不空
|
||||
{
|
||||
if (qa->exp < qb->exp) // 2.1 LA的幂小
|
||||
{ // pa、qa后移
|
||||
if (qa->exp < qb->exp) // 2.1 LA的幂小
|
||||
{ // pa、qa后移
|
||||
pa = qa; qa = qa->next;
|
||||
}
|
||||
else if (qa->exp > qb->exp) //2.2 LA 幂大
|
||||
else if (qa->exp > qb->exp) //2.2 LA 幂大
|
||||
{
|
||||
pb->next = qb->next; // qb链接到pa之后
|
||||
pb->next = qb->next; // qb链接到pa之后
|
||||
qb->next = qa;
|
||||
pa->next = qb;
|
||||
pa = qb;
|
||||
qb = pb->next;
|
||||
}
|
||||
else // 2.3 LA与LB幂相同
|
||||
else // 2.3 LA与LB幂相同
|
||||
{
|
||||
sum = qa->coef + qb->coef; // 计算系数和
|
||||
if (sum != 0) // 2.3.1 系数和不为0
|
||||
sum = qa->coef + qb->coef; // 计算系数和
|
||||
if (sum != 0) // 2.3.1 系数和不为0
|
||||
{
|
||||
qa->coef = sum; // 2.3.1.1 qa->coef←sum
|
||||
pa = qa; qa = qa->next; // 2.3.1.2 pa,qa后移
|
||||
qa->coef = sum; // 2.3.1.1 qa->coef←sum
|
||||
pa = qa; qa = qa->next; // 2.3.1.2 pa,qa后移
|
||||
pb->next = qb->next;
|
||||
delete qb; // 2.3.1.3删除qb,
|
||||
delete qb; // 2.3.1.3删除qb,
|
||||
qb = pb->next;
|
||||
}
|
||||
else // 2.3.2 系数和为0
|
||||
else // 2.3.2 系数和为0
|
||||
{
|
||||
pa->next = qa->next;
|
||||
delete qa; // 2.3.2.1 删除qa,
|
||||
qa = pa->next; // 2.3.2.2 qa为pa后继;
|
||||
delete qa; // 2.3.2.1 删除qa,
|
||||
qa = pa->next; // 2.3.2.2 qa为pa后继;
|
||||
pb->next = qb->next;
|
||||
delete qb; // 2.3.2.3 删除qb
|
||||
qb = pb->next; // 2.3.2.4 qb为pb的后继
|
||||
delete qb; // 2.3.2.3 删除qb
|
||||
qb = pb->next; // 2.3.2.4 qb为pb的后继
|
||||
}
|
||||
}
|
||||
}//while
|
||||
if (qb != NULL) // 3. LA处理结束,LB未结束
|
||||
pa->next = qb; // 3.1 qb链到qa之后
|
||||
delete pb; // 3.2 删除lb头结点
|
||||
if (qb != NULL) // 3. LA处理结束,LB未结束
|
||||
pa->next = qb; // 3.1 qb链到qa之后
|
||||
delete pb; // 3.2 删除lb头结点
|
||||
LB = NULL;
|
||||
|
||||
}//Add
|
||||
|
||||
|
||||
void DestroyPoly(PolyNode*& L) // 释放链表所占空间
|
||||
void DestroyPoly(PolyNode*& L) // 释放链表所占空间
|
||||
{
|
||||
PolyNode* p;
|
||||
while (L) // 从头结点开始,依次释放结点
|
||||
while (L) // 从头结点开始,依次释放结点
|
||||
{
|
||||
p = L;
|
||||
L = L->next;
|
||||
delete p;
|
||||
}
|
||||
L = NULL; // 头结点指向空
|
||||
L = NULL; // 头结点指向空
|
||||
}
|
||||
|
||||
void SortPoly(PolyNode*& L) // 将多项式按幂升序排序
|
||||
void SortPoly(PolyNode*& L) // 将多项式按幂升序排序
|
||||
{
|
||||
PolyNode* p1, * p2, * q, * r; // 采用插入排序算法
|
||||
p1 = L; p2 = p1->next; // p1是p2的前驱
|
||||
if (p2 == NULL || p2->next == NULL) // 空表或只有1项的多项式,不需要排序
|
||||
PolyNode* p1, * p2, * q, * r; // 采用插入排序算法
|
||||
p1 = L; p2 = p1->next; // p1是p2的前驱
|
||||
if (p2 == NULL || p2->next == NULL) // 空表或只有1项的多项式,不需要排序
|
||||
{
|
||||
cout << "不需要排序!" << endl;
|
||||
cout << "不需要排序!" << endl;
|
||||
return;
|
||||
}
|
||||
r = L->next; // 有序表表尾
|
||||
q = r->next; // q为当前处理项,有序表的后一项
|
||||
while (q) // 未处理完
|
||||
{ // 从首元结点开始查找插入点
|
||||
r = L->next; // 有序表表尾
|
||||
q = r->next; // q为当前处理项,有序表的后一项
|
||||
while (q) // 未处理完
|
||||
{ // 从首元结点开始查找插入点
|
||||
p1 = L; p2 = p1->next;
|
||||
while (q->exp > p2->exp && p2 != q) // 当前结点幂大,插入点后移
|
||||
while (q->exp > p2->exp && p2 != q) // 当前结点幂大,插入点后移
|
||||
{
|
||||
p1 = p2; p2 = p2->next;
|
||||
}
|
||||
if (p2 == q) // 当前项无需移动
|
||||
if (p2 == q) // 当前项无需移动
|
||||
{
|
||||
r = q; // 有序表表尾顺移
|
||||
r = q; // 有序表表尾顺移
|
||||
}
|
||||
else // q插入到p2前面
|
||||
else // q插入到p2前面
|
||||
{
|
||||
r->next = q->next; // 摘除q结点
|
||||
q->next = p1->next; // 在p1后插入结点q
|
||||
r->next = q->next; // 摘除q结点
|
||||
q->next = p1->next; // 在p1后插入结点q
|
||||
p1->next = q;
|
||||
}
|
||||
q = r->next; // 下一个需处理的项
|
||||
q = r->next; // 下一个需处理的项
|
||||
|
||||
}
|
||||
return;
|
||||
@@ -158,81 +158,81 @@ void SortPoly(PolyNode*& L) //
|
||||
|
||||
|
||||
void dispmenu()
|
||||
{//显示主菜单
|
||||
{//显示主菜单
|
||||
cout << endl;
|
||||
cout << "*** 稀疏多项式求和 ***\n";
|
||||
cout << "1-创建多项式A\n";
|
||||
cout << "2-创建多项式B\n";
|
||||
cout << "3-多项式求和A=A+B\n";
|
||||
cout << "4-显示多项式\n";
|
||||
cout << "0-退出\n";
|
||||
cout << "*** 稀疏多项式求和 ***\n";
|
||||
cout << "1-创建多项式A\n";
|
||||
cout << "2-创建多项式B\n";
|
||||
cout << "3-多项式求和A=A+B\n";
|
||||
cout << "4-显示多项式\n";
|
||||
cout << "0-退出\n";
|
||||
}
|
||||
|
||||
//主函数
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int m, n;
|
||||
char c;
|
||||
PolyNode* LA, * LB;
|
||||
system("cls"); // 执行系统命令cls,清屏
|
||||
system("cls"); // 执行系统命令cls,清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~4,0 退出):";
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~4,0 退出):";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: // 创建多项式A
|
||||
case 1: // 创建多项式A
|
||||
InitPoly(LA);
|
||||
cout << "请输入多项式 A 的项数: ";
|
||||
cout << "请输入多项式 A 的项数: ";
|
||||
cin >> m;
|
||||
CreatePoly(LA, m);
|
||||
cout << "创建的多项式 A 为:" << endl;
|
||||
cout << "创建的多项式 A 为:" << endl;
|
||||
DispPoly(LA);
|
||||
SortPoly(LA);
|
||||
cout << "排序后多项式 A 为:" << endl;
|
||||
cout << "排序后多项式 A 为:" << endl;
|
||||
DispPoly(LA);
|
||||
break;
|
||||
case 2: // 创建多项式B
|
||||
case 2: // 创建多项式B
|
||||
InitPoly(LB);
|
||||
cout << "请输入多项式B的项数: ";
|
||||
cout << "请输入多项式B的项数: ";
|
||||
cin >> n;
|
||||
CreatePoly(LB, n);
|
||||
cout << "创建的多项式B为:" << endl;
|
||||
cout << "创建的多项式B为:" << endl;
|
||||
DispPoly(LB);
|
||||
SortPoly(LB);
|
||||
cout << "排序后多项式 B 为:" << endl;
|
||||
cout << "排序后多项式 B 为:" << endl;
|
||||
DispPoly(LB);
|
||||
break;
|
||||
case 3: //多项式求和
|
||||
case 3: //多项式求和
|
||||
cout << "A = ";
|
||||
DispPoly(LA);
|
||||
cout << "B = ";
|
||||
DispPoly(LB);
|
||||
PolyAdd(LA, LB); // 求多项式 LA=LA+LB
|
||||
cout << "A + B = "; // 显示结果
|
||||
PolyAdd(LA, LB); // 求多项式 LA=LA+LB
|
||||
cout << "A + B = "; // 显示结果
|
||||
DispPoly(LA);
|
||||
cout << endl;
|
||||
break;
|
||||
case 4: // 显示多项式
|
||||
cout << "选择要显示的多项式 A 或 B:" << endl;
|
||||
case 4: // 显示多项式
|
||||
cout << "选择要显示的多项式 A 或 B:" << endl;
|
||||
cin >> c;
|
||||
if (c == 'A' || c == 'a')
|
||||
DispPoly(LA);
|
||||
else if (c == 'B' || c == 'b')
|
||||
DispPoly(LB);
|
||||
else
|
||||
cout << "选择错误!" << endl;
|
||||
cout << "选择错误!" << endl;
|
||||
break;
|
||||
case 5: //退出
|
||||
case 5: //退出
|
||||
DestroyPoly(LA);
|
||||
DestroyPoly(LB);
|
||||
cout << "结束运行bye-bye!" << endl;
|
||||
cout << "结束运行bye-bye!" << endl;
|
||||
break;
|
||||
default: //非法选择
|
||||
cout << "非法选择!\n";
|
||||
default: //非法选择
|
||||
cout << "非法选择!\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
138
Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj
Normal file
138
Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.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>{0b0a9a4c-185a-4345-bd4e-1e48565907e2}</ProjectGuid>
|
||||
<RootNamespace>My25ReverseLinkList</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="ReverseLinkList.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h" />
|
||||
</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>
|
||||
<ClCompile Include="ReverseLinkList.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
237
Chapter2/2-5-ReverseLinkList/LinkList.h
Normal file
237
Chapter2/2-5-ReverseLinkList/LinkList.h
Normal file
@@ -0,0 +1,237 @@
|
||||
|
||||
template <class DT>
|
||||
struct LNode //链表结点
|
||||
{
|
||||
DT data; //数据域,存储数据元素值
|
||||
LNode* next; //指针域,指向下一个结点
|
||||
};
|
||||
|
||||
|
||||
//【算法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)
|
||||
{
|
||||
LNode<DT>* p, * s;
|
||||
p = L; //1.工作指针初始化,指向尾结点
|
||||
cout << "依次输入" << n << "个数据元素:" << endl;
|
||||
for (int 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)
|
||||
{
|
||||
LNode<DT>* s;
|
||||
cout << "逆序输入" << n << "个数据元素:" << endl;
|
||||
for (int 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->next; // 从首元结点开始遍历
|
||||
while (p) // 2.依次输出各结点值
|
||||
{
|
||||
cout << p->data << "\t";
|
||||
p = p->next;
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
82
Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp
Normal file
82
Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include<iostream> //cout,cin
|
||||
using namespace std;
|
||||
#include "LinkList.h"
|
||||
|
||||
|
||||
//算法2.25 单链表逆置
|
||||
void ReverseLinkList(LNode<int>*& L)
|
||||
{
|
||||
LNode<int>* p, * q; // 1.设置工作指针
|
||||
p = L->next; // 原链表头结点,作为逆置后表的头结点
|
||||
L->next = NULL;
|
||||
while (p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中
|
||||
{
|
||||
q = p; // 2.1 q取当前结点位置
|
||||
p = p->next; // 2.2 p指向下一个待处理结点
|
||||
q->next = L->next; // 2.3 将q 插入到头结点之后
|
||||
L->next = q;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void dispmenu()
|
||||
{//显示主菜单
|
||||
cout << endl;
|
||||
cout << "1-创建单链表\n";
|
||||
cout << "2-逆置单链表\n";
|
||||
cout << "3-显示单链表\n";
|
||||
cout << "4-退出\n";
|
||||
}
|
||||
|
||||
char pause;
|
||||
|
||||
//主函数
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
//int e;
|
||||
LNode<int>* L;
|
||||
system("cls"); // 执行系统命令cls,清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~4):";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: // 创建单链表
|
||||
InitList(L);
|
||||
cout << "输入要创建元素个数:";
|
||||
cin >> n;
|
||||
cout << endl;
|
||||
CreateList_1(L, n);
|
||||
cout << "创建的单链表为:";
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
break;
|
||||
case 2: // 逆置单链表
|
||||
ReverseLinkList(L);
|
||||
cout << "逆置后的单链表为:";
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
break;
|
||||
case 3: // 显示表
|
||||
DispList(L);
|
||||
cout << endl;
|
||||
cin.get(pause);
|
||||
system("pause");
|
||||
break;
|
||||
case 4: // 退出
|
||||
DestroyList(L);
|
||||
cout << "结束运行" << endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout << "Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 4);
|
||||
return 0;
|
||||
}//end of main function
|
||||
@@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-3-SqListApp", "2-3-SqList
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-4-PolyAdd", "2-4-PolyAdd\2-4-PolyAdd.vcxproj", "{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-5-ReverseLinkList", "2-5-ReverseLinkList\2-5-ReverseLinkList.vcxproj", "{0B0A9A4C-185A-4345-BD4E-1E48565907E2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
@@ -51,6 +53,14 @@ Global
|
||||
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x64.Build.0 = Release|x64
|
||||
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x86.ActiveCfg = Release|Win32
|
||||
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x86.Build.0 = Release|Win32
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x64.Build.0 = Debug|x64
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x86.Build.0 = Debug|Win32
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x64.ActiveCfg = Release|x64
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x64.Build.0 = Release|x64
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x86.ActiveCfg = Release|Win32
|
||||
{0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
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.front=" << 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
|
||||
|
||||
100
Chapter3/SqQueue/SqQueue.h
Normal file
100
Chapter3/SqQueue/SqQueue.h
Normal file
@@ -0,0 +1,100 @@
|
||||
template<class DT>
|
||||
struct SqQueue
|
||||
{
|
||||
DT* base; // 存储空间基地址
|
||||
int front; // 队头指针,指向队首元素
|
||||
int rear; // 队尾指针,指向队尾元素的后面
|
||||
int queuesize; // 队列容量
|
||||
};
|
||||
|
||||
template<class DT> // 算法3.14 初始化队列
|
||||
bool InitQueue(SqQueue<DT>& Q, int m) // 创建容量为m的空队列
|
||||
{
|
||||
Q.base = new DT[m]; // 1. 申请一组连续的内存空间
|
||||
if (!Q.base) exit(1); // 申请失败,退出
|
||||
Q.front = Q.rear = 0; // 2. 申请成功,为队列属性赋值
|
||||
Q.queuesize = m;
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.15 销毁队列
|
||||
void DestroyQueue(SqQueue<DT>& Q) // 销毁循环队列
|
||||
{
|
||||
delete[] Q.base; // 1. 释放循环队列占用的内存空间
|
||||
Q.front = Q.rear = 0; // 2. 为队列属性赋值
|
||||
Q.queuesize = 0;
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.16 入队
|
||||
bool EnQueue(SqQueue<DT>& Q, DT e) // 在队尾插入一个新元素
|
||||
{
|
||||
if ((Q.rear + 1) % Q.queuesize == Q.front) // 1. 队满的情况
|
||||
return false; // 无法入队,返回false
|
||||
Q.base[Q.rear] = e; // 2. 元素e放在队尾指针处
|
||||
Q.rear = (Q.rear + 1) % Q.queuesize; // 队尾指针增1
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.17 出队
|
||||
bool DeQueue(SqQueue<DT>& Q, DT& e) // 删除队头元素
|
||||
{
|
||||
if (Q.front == Q.rear) // 1. 队空的情况
|
||||
return false; // 无法出队,返回false
|
||||
e = Q.base[Q.front]; // 2. 取队头元素,赋值给e
|
||||
Q.front = (Q.front + 1) % Q.queuesize; // 队头指针加1
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT> // 算法3.18 取队头元素
|
||||
bool GetHead(SqQueue<DT> Q, DT& e) // 取队头元素
|
||||
{
|
||||
if (Q.front == Q.rear) // 1. 队空的情况
|
||||
return false; // 无队头元素,返回false
|
||||
e = Q.base[Q.front]; // 2. 取队头元素,赋值给e
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool GetTail(SqQueue<DT> Q, DT& e) // 取队尾元素
|
||||
{
|
||||
if (Q.front == Q.rear) // 1. 队空的情况
|
||||
return false; // 无队尾元素,返回false
|
||||
e = Q.base[(Q.rear - 1 + Q.queuesize) % Q.queuesize]; // 2. 取队尾元素,赋值给e
|
||||
return true; // 3. 返回true
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
int QueueLength(SqQueue<DT> Q) // 测表长
|
||||
{
|
||||
return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool QueueEmpty(SqQueue<DT> Q) // 判队空
|
||||
{
|
||||
return Q.front == Q.rear;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
bool QueueFull(SqQueue<DT> Q) // 判队满
|
||||
{
|
||||
return (Q.rear + 1) % Q.queuesize == Q.front;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
void ClearQueue(SqQueue<DT>& Q) // 清空队
|
||||
{
|
||||
Q.front = Q.rear = 0;
|
||||
}
|
||||
|
||||
template<class DT>
|
||||
void DispQueue(SqQueue<DT> Q) // 显示队
|
||||
{
|
||||
int i = Q.front;
|
||||
while (i != Q.rear)
|
||||
{
|
||||
cout << Q.base[i] << " ";
|
||||
i = (i + 1) % Q.queuesize;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
138
Chapter3/SqQueue/SqQueue.vcxproj
Normal file
138
Chapter3/SqQueue/SqQueue.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>{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>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqQueue.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
27
Chapter3/SqQueue/SqQueue.vcxproj.filters
Normal file
27
Chapter3/SqQueue/SqQueue.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>
|
||||
<ClCompile Include="SqQueue.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqQueue.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</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>
|
||||
203
Chapter4/AddMatrix/AddMatrix.cpp
Normal file
203
Chapter4/AddMatrix/AddMatrix.cpp
Normal file
@@ -0,0 +1,203 @@
|
||||
#include<iostream>//cout,cin
|
||||
#include"process.h"//exit()
|
||||
#include"stdio.h"//EOF,NULL
|
||||
using namespace std;
|
||||
|
||||
struct MTNode // 三元组
|
||||
{
|
||||
int i, j; // 行号,列号
|
||||
int e; // 非零元
|
||||
MTNode* next; // 指向同行下一个结点
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int mu, nu, tu; // 行数、列数、非零元个数
|
||||
MTNode** rops; // 存放各行链表的头指针
|
||||
}LMatrix;
|
||||
|
||||
int cmp(int a, int b) // 列号比较
|
||||
{
|
||||
if (a < b) return -1;
|
||||
else if (a == b) return 0;
|
||||
else return 1;
|
||||
}
|
||||
|
||||
void NodeCopy(MTNode*& s, MTNode* x) // 结点拷贝
|
||||
{
|
||||
s->e = x->e; s->i = x->i; s->j = x->j;
|
||||
}
|
||||
|
||||
void AddNode(MTNode*& lp, MTNode*& lq, MTNode* s) // 表尾添加结点
|
||||
{
|
||||
MTNode* p;
|
||||
p = new MTNode;
|
||||
NodeCopy(p, s);
|
||||
p->next = NULL;
|
||||
if (lp == NULL) // 首元结点
|
||||
{
|
||||
lp = p;
|
||||
lq = p;
|
||||
}
|
||||
else // 非首元结点
|
||||
{
|
||||
lq->next = p;
|
||||
lq = p;
|
||||
}
|
||||
}//
|
||||
|
||||
|
||||
LMatrix MatrixAdd(LMatrix ma, LMatrix mb) // 求矩阵和
|
||||
{
|
||||
LMatrix mc;
|
||||
MTNode* pa, * pb, * pc; // 分别指向被加数、加数、和矩阵行向量首元结点
|
||||
MTNode* s;//
|
||||
int i, sum;
|
||||
int m, n; // 行数,列数
|
||||
int flag = 1;
|
||||
m = ma.mu;
|
||||
n = ma.nu;
|
||||
mc.mu = m; mc.nu = n; mc.tu = 0; mc.rops = NULL;
|
||||
if (mc.rops) delete[] mc.rops;
|
||||
mc.rops = new MTNode * [m];
|
||||
for (i = 0; i < m; i++)
|
||||
mc.rops[i] = NULL; // C行指针向量初始化
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
pa = ma.rops[i];
|
||||
pb = mb.rops[i];
|
||||
pc = mc.rops[i];
|
||||
while (pa && pb) // 被加矩阵、加矩阵行链不空
|
||||
{
|
||||
flag = 1;
|
||||
if (pa->j < pb->j)
|
||||
{
|
||||
s = new MTNode;//
|
||||
NodeCopy(s, pa);
|
||||
s->next = NULL;
|
||||
pa = pa->next;
|
||||
}
|
||||
else if (pa->j == pb->j)
|
||||
{
|
||||
sum = pa->e + pb->e;
|
||||
if (sum == 0) flag = 0;
|
||||
else
|
||||
{
|
||||
s = new MTNode;
|
||||
NodeCopy(s, pa);
|
||||
s->e = sum;
|
||||
s->next = NULL;
|
||||
}
|
||||
pa = pa->next; pb = pb->next;//pa,pb后移
|
||||
}
|
||||
else
|
||||
{
|
||||
s = new MTNode;
|
||||
NodeCopy(s, pb); // 复制pb所指结点
|
||||
pb = pb->next; // pb后移
|
||||
s->next = NULL;
|
||||
}
|
||||
if (flag) // 有新结点生成
|
||||
{
|
||||
mc.tu++;
|
||||
AddNode(mc.rops[i], pc, s);
|
||||
}
|
||||
}//while
|
||||
if (pa) // pa不空,复制pa剩余链到和矩阵中
|
||||
{
|
||||
while (pa)
|
||||
{
|
||||
s = new MTNode;
|
||||
NodeCopy(s, pa); pa = pa->next;
|
||||
AddNode(mc.rops[i], pc, s);
|
||||
}//while
|
||||
}//if(pa)
|
||||
if (pb) // pb不空,复制pb剩余链到和矩阵中
|
||||
{
|
||||
while (pb)
|
||||
{
|
||||
s = new MTNode;
|
||||
NodeCopy(s, pb); pb = pb->next;
|
||||
AddNode(mc.rops[i], pc, s);
|
||||
}//while
|
||||
}//if(pb)
|
||||
}//for
|
||||
return mc;
|
||||
}//MAdd
|
||||
|
||||
void MDisp(LMatrix a)
|
||||
{
|
||||
MTNode* p;
|
||||
int i, j, c = 0;
|
||||
for (i = 0; i < a.mu; i++)
|
||||
{
|
||||
p = a.rops[i];
|
||||
for (j = 0; j < a.nu; j++)
|
||||
{
|
||||
if (p == NULL)
|
||||
cout << '\t' << c;
|
||||
else if (j < p->j)
|
||||
cout << '\t' << c;
|
||||
else
|
||||
{
|
||||
cout << '\t' << p->e;
|
||||
p = p->next;
|
||||
}
|
||||
}//for
|
||||
cout << endl;
|
||||
}//for
|
||||
}//MatrixDisp
|
||||
|
||||
LMatrix MCreate(int d[][3], int m, int n, int k)
|
||||
{ // 由三元组的二维数组生成行向量稀疏存储矩阵
|
||||
LMatrix M = { m,n,k,NULL };
|
||||
int i, r1, r2;
|
||||
MTNode* s, * p; // 工作指针
|
||||
if (M.rops) delete[] M.rops;
|
||||
M.rops = new MTNode * [m];
|
||||
for (i = 0; i < m; i++)
|
||||
M.rops[i] = NULL;
|
||||
r1 = m;
|
||||
p = M.rops[r1];//
|
||||
for (i = 0; i < k; i++) // 扫描非零元数组
|
||||
{
|
||||
s = new MTNode;
|
||||
s->i = d[i][0];
|
||||
s->j = d[i][1];
|
||||
s->e = d[i][2];
|
||||
r2 = s->i; // 非零元所在行
|
||||
if (r2 != r1) // 创建链表第1个结点
|
||||
{
|
||||
M.rops[r2] = s;
|
||||
s->next = NULL;
|
||||
p = s;
|
||||
r1 = r2;
|
||||
}
|
||||
else // 创建链表非首元结点
|
||||
{
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
}//for
|
||||
return M;
|
||||
}//MCreate
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
//MTNode *p;
|
||||
LMatrix ma, mb, mc;
|
||||
int m = 4, n = 6; // 行数,列数
|
||||
int da[5][3] = { {0,1,3},{1,1,2},{1,3,5},{3,0,9},{3,5,1} };
|
||||
int db[4][3] = { {0,2,7},{1,1,6},{1,3,-5},{2,1,4} };
|
||||
ma = MCreate(da, 4, 6, 5); // 构造ma矩阵
|
||||
cout << "ma=" << endl;
|
||||
MDisp(ma); // 显示ma矩阵
|
||||
mb = MCreate(db, 4, 6, 4); // 构造mb矩阵
|
||||
cout << "mb=" << endl;
|
||||
MDisp(mb); // 显示mb矩阵
|
||||
mc = MatrixAdd(ma, mb); // 求mc=ma+mb
|
||||
cout << "mc=ma+mb=" << endl;
|
||||
MDisp(mc); // 输出和矩阵
|
||||
}
|
||||
135
Chapter4/AddMatrix/AddMatrix.vcxproj
Normal file
135
Chapter4/AddMatrix/AddMatrix.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>{01a69829-c675-4cad-99d2-35be3dc4ed2c}</ProjectGuid>
|
||||
<RootNamespace>AddMatrix</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>false</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="AddMatrix.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -15,7 +15,7 @@
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="b.cpp">
|
||||
<ClCompile Include="AddMatrix.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
41
Chapter4/Chapter4.sln
Normal file
41
Chapter4/Chapter4.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}") = "AddMatrix", "AddMatrix\AddMatrix.vcxproj", "{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MatrixTrans", "MatrixTrans\MatrixTrans.vcxproj", "{AE81732D-D011-4A36-9D89-0C258ADB5627}"
|
||||
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
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x64.Build.0 = Debug|x64
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x86.Build.0 = Debug|Win32
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x64.ActiveCfg = Release|x64
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x64.Build.0 = Release|x64
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x86.ActiveCfg = Release|Win32
|
||||
{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x86.Build.0 = Release|Win32
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x64.Build.0 = Debug|x64
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x86.Build.0 = Debug|Win32
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x64.ActiveCfg = Release|x64
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x64.Build.0 = Release|x64
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x86.ActiveCfg = Release|Win32
|
||||
{AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {32AA252A-8427-41AB-8DDD-F9DC7EE96753}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
130
Chapter4/MatrixTrans/MatrixTrans.cpp
Normal file
130
Chapter4/MatrixTrans/MatrixTrans.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct MNode //三元组结点
|
||||
{
|
||||
int i, j; //行号,列号
|
||||
int e; //非零元
|
||||
};
|
||||
|
||||
|
||||
struct TSMatrix
|
||||
{
|
||||
int mu, nu, tu; // 行数、列数、非零元个数
|
||||
MNode* data; // 三元组表
|
||||
};
|
||||
|
||||
TSMatrix MCreate(int d[][3], int m, int n, int k) //由三元组的二维数组生成行向量稀疏存储矩阵
|
||||
{
|
||||
TSMatrix M = { m,n,k,NULL };
|
||||
if (k != 0)
|
||||
M.data = new MNode[k];
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
M.data[i].i = d[i][0];
|
||||
M.data[i].j = d[i][1];
|
||||
M.data[i].e = d[i][2];
|
||||
}
|
||||
return M;
|
||||
}//
|
||||
|
||||
|
||||
void MDisp(TSMatrix a) //矩阵显示
|
||||
{
|
||||
MNode p;
|
||||
int i, j, k = 0, c = 0;
|
||||
p = a.data[k];
|
||||
for (i = 0; i < a.mu; i++)
|
||||
{
|
||||
for (j = 0; j < a.nu; j++)
|
||||
{
|
||||
if (k < a.tu && p.i == i && p.j == j)
|
||||
{
|
||||
cout << '\t' << p.e;
|
||||
k++;
|
||||
if (k < a.tu) p = a.data[k];
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << '\t' << c;
|
||||
}
|
||||
}//for
|
||||
cout << endl;
|
||||
cout << endl;
|
||||
}//for
|
||||
}//MatrixDisp
|
||||
|
||||
//算法4.1 转置算法一
|
||||
void MatrixTrans_1(TSMatrix A, TSMatrix& B)
|
||||
{//
|
||||
int col, p, q;
|
||||
B.mu = A.nu; B.nu = A.mu; B.tu = A.tu; //B的行数、列数、非零元个数分别等于A的列数、行数和非零元个数
|
||||
if (B.tu) // 矩阵B有非零元
|
||||
{
|
||||
q = 0;
|
||||
for (col = 0; col < A.nu; ++col) // 按列扫描A矩阵
|
||||
for (p = 0; p < A.tu; ++p)
|
||||
if (A.data[p].j == col) // col列有非零元
|
||||
{
|
||||
B.data[q].i = A.data[p].j; // 交换行、列号,生成转置元素
|
||||
B.data[q].j = A.data[p].i;
|
||||
B.data[q].e = A.data[p].e;
|
||||
++q;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//算法4.2 快速转置
|
||||
void MatrixTrans_2(TSMatrix A, TSMatrix& B)
|
||||
{
|
||||
int col, i, k, q;
|
||||
int* num, * cpot;
|
||||
B.mu = A.nu; B.nu = A.mu; B.tu = A.tu; //B的行数、列数、非零元个数分别等于A的列数、行数和非零元个数
|
||||
num = new int[B.nu];
|
||||
cpot = new int[B.nu];
|
||||
if (B.tu) // 非零元个数不为零,实施转置
|
||||
{
|
||||
for (col = 0; col < A.nu; col++) //A中每一列非零元个数初始化为0
|
||||
num[col] = 0;
|
||||
for (i = 0; i < A.tu; i++) //求矩阵A中每一列非零元个数
|
||||
num[A.data[i].j]++;
|
||||
cpot[0] = 0; //A中第0列首个非零元在B中的下标
|
||||
for (col = 1; col <= A.nu; ++col) // 求A中每一列首个非零元在B中的下标
|
||||
cpot[col] = cpot[col - 1] + num[col - 1];
|
||||
for (k = 0; k < A.tu; ++k) //扫描A的三元组表
|
||||
{
|
||||
col = A.data[k].j; //当前三元组列号
|
||||
q = cpot[col]; //当前三元组在B中的下标
|
||||
B.data[q].i = A.data[k].j;
|
||||
B.data[q].j = A.data[k].i;
|
||||
B.data[q].e = A.data[k].e;
|
||||
cpot[col]++; // 预置同一列下一个三元组在B中的下标
|
||||
}//for
|
||||
}//if
|
||||
}//
|
||||
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
TSMatrix ma, mb1, mb2;
|
||||
int m1 = 4, n1 = 6, k1 = 6; //被乘矩阵行数,列数,非零元个数
|
||||
int da[6][3] = { {0,2,11},{0,4,12},{1,3,22},
|
||||
{2,1,31}, {2,3,32},{3,0,41} }; //ma阵的非零元
|
||||
ma = MCreate(da, m1, n1, k1); //生成三元组顺序存储的ma阵
|
||||
cout << "ma=" << endl;
|
||||
MDisp(ma); //显示ma阵
|
||||
mb1.data = new MNode[ma.tu];
|
||||
mb2.data = new MNode[ma.tu];
|
||||
cout << "方法一:直接取,顺序存" << endl; //用方法一转置矩阵
|
||||
MatrixTrans_1(ma, mb1);
|
||||
cout << "mb=" << endl; //显示转置矩阵
|
||||
MDisp(mb1);
|
||||
cout << "方法二:顺序取,直接存" << endl; //用方法二转置矩阵
|
||||
MatrixTrans_2(ma, mb2);
|
||||
cout << "mb=" << endl; //显示转置矩阵
|
||||
MDisp(mb2);
|
||||
}//
|
||||
135
Chapter4/MatrixTrans/MatrixTrans.vcxproj
Normal file
135
Chapter4/MatrixTrans/MatrixTrans.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>{ae81732d-d011-4a36-9d89-0c258adb5627}</ProjectGuid>
|
||||
<RootNamespace>MatrixTrans</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="MatrixTrans.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
22
Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters
Normal file
22
Chapter4/MatrixTrans/MatrixTrans.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="MatrixTrans.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
138
Chapter5/BiTree/BiTree.vcxproj
Normal file
138
Chapter5/BiTree/BiTree.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>{904cfcdf-da23-415a-8d68-8e51ad913458}</ProjectGuid>
|
||||
<RootNamespace>BiTree</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>false</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="bitree.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="bitree.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
27
Chapter5/BiTree/BiTree.vcxproj.filters
Normal file
27
Chapter5/BiTree/BiTree.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="bitree.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="bitree.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
129
Chapter5/BiTree/bitree.cpp
Normal file
129
Chapter5/BiTree/bitree.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include<iostream>//cout,cin
|
||||
#include<string>
|
||||
using namespace std;
|
||||
#include"bitree.h"
|
||||
|
||||
//测试参考数据
|
||||
string fbt = "a b d # # e # # c f # # g # #"; // 满二叉树最后一层每个叶结点后有两个#
|
||||
string cbt = "a b d # # e # # c # #"; // 完全二叉树每个叶结点后有两个#
|
||||
string gbt = "a b # d # # c e # # #"; // 一般二叉树
|
||||
string obt = "a b c d # # # # #"; // 左斜树1
|
||||
|
||||
|
||||
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 << "11-求二叉树结点个数\n";
|
||||
cout << "12-显示二叉树\n";
|
||||
cout << "0 -退出\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int level;
|
||||
BTNode<char>* bt;
|
||||
|
||||
system("cls"); // 清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~12,0 退出):";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: // 创建二叉树
|
||||
cout << "测试参考数据:" << endl;
|
||||
cout << "满二叉树:" << fbt << endl;
|
||||
cout << "完全二叉树:" << cbt << endl;
|
||||
cout << "一般二叉树:" << gbt << endl;
|
||||
cout << "左斜二叉树:" << obt << endl;
|
||||
cout << "请按先序序列的顺序输入二叉树,#为空指针域标志:" << endl;
|
||||
CreateBiTree(bt);
|
||||
break;
|
||||
case 2: // 先序递归遍历二叉树
|
||||
cout << "先序遍历序列为:" << endl;
|
||||
PreOrDerBiTree(bt);
|
||||
break;
|
||||
case 3: // 中序递归遍历二叉树
|
||||
cout << "中序遍历序列为:" << endl;
|
||||
InOrDerBiTree(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 4: // 后序递归遍历二叉树
|
||||
cout << "后序遍历序列为:" << endl;
|
||||
PostOrDerBiTree(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 5: // 层序遍历二叉树
|
||||
cout << "层序遍历序列为:" << endl;
|
||||
cout << endl;
|
||||
LevelBiTree(bt);
|
||||
break;
|
||||
case 6: // 先序非递归遍历二叉树
|
||||
cout << "先序非递归遍历序列为:" << endl;
|
||||
PreOrderBiTree_N(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 7: // 中序非递归遍历二叉树
|
||||
cout << "中序非递归遍历序列为:" << endl;
|
||||
InOrderBiTree_N(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 8: // 后序非递归遍历二叉树
|
||||
cout << "后序非递归遍历序列为:" << endl;
|
||||
cout << endl;
|
||||
PostOrderBiTree_N(bt);
|
||||
break;
|
||||
case 9: // 结点查询
|
||||
char e;
|
||||
BTNode<char>* p;
|
||||
cout << "输入要查询的结点值:" << endl;
|
||||
cin >> e;
|
||||
p = Search(bt, e);
|
||||
if (p)
|
||||
{
|
||||
cout << "找到!";
|
||||
cout << p->data << endl;
|
||||
}
|
||||
else
|
||||
cout << "未找到!" << endl;
|
||||
cout << endl;
|
||||
break;
|
||||
case 10: // 求二叉树高度
|
||||
cout << "二叉树高度为:" << Depth(bt) << endl;
|
||||
cout << endl;
|
||||
break;
|
||||
case 11: //求二叉树结点个数
|
||||
cout << "二叉树结点数为:" << NodeCount(bt) << endl;
|
||||
cout << endl;
|
||||
break;
|
||||
case 12: // 显示二叉树
|
||||
cout << "2-显示二叉树" << endl;
|
||||
level = 1;
|
||||
DispBiTree(bt, level);
|
||||
cout << endl;
|
||||
break;
|
||||
case 0: // 退出
|
||||
DestroyBiTree(bt);
|
||||
cout << "结束运行Bye-Bye!" << endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout << "Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
421
Chapter5/BiTree/bitree.h
Normal file
421
Chapter5/BiTree/bitree.h
Normal file
@@ -0,0 +1,421 @@
|
||||
template <class DT>
|
||||
struct BTNode
|
||||
{
|
||||
DT data; //数据域
|
||||
BTNode* lchild; //指向左子树的指针
|
||||
BTNode* rchild; //指向右子树的指针
|
||||
};
|
||||
|
||||
//队列工具
|
||||
template <class DT>
|
||||
struct SqQueue // 顺序队类
|
||||
{
|
||||
BTNode<DT>** base; // 队列首址
|
||||
int front; // 队头指针
|
||||
int rear; // 队尾指针
|
||||
int queuesize; // 队容量
|
||||
};
|
||||
|
||||
//栈
|
||||
template <class DT>
|
||||
struct SqStack // 顺序栈
|
||||
{
|
||||
BTNode<DT>** base; // 栈首址
|
||||
int top; // 栈顶指针
|
||||
int stacksize; // 栈容量
|
||||
};
|
||||
|
||||
//算法5.1 先序遍历递归算法
|
||||
template <class DT>
|
||||
void PreOrDerBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt != NULL)
|
||||
{
|
||||
cout << bt->data << ' '; //输出结点上的数据
|
||||
PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树
|
||||
PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//算法5.2 中序遍历递归算法
|
||||
template <class DT>
|
||||
void InOrDerBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt != NULL)
|
||||
{
|
||||
InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树
|
||||
cout << bt->data << ' '; //输出结点上的数据
|
||||
InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//算法5.3 后序遍历递归算法
|
||||
template <class DT>
|
||||
void PostOrDerBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt != NULL)
|
||||
{
|
||||
PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树
|
||||
PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树
|
||||
cout << bt->data << ' '; //输出结点上的数据
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//算法5.4 层序遍历算法
|
||||
template<class DT>
|
||||
void LevelBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
SqQueue<DT> Q; // 创建一个队
|
||||
int m = 20;
|
||||
InitQueue(Q, m);
|
||||
BTNode<DT>* p;
|
||||
p = bt;
|
||||
if (p) EnQueue(Q, p); // 树非空,入队
|
||||
while (!QueueEmpty(Q)) // 队非空
|
||||
{
|
||||
DeQueue(Q, p); // 出队
|
||||
cout << p->data; // 访问
|
||||
if (p->lchild != NULL) // 有左孩子
|
||||
EnQueue(Q, p->lchild); // 左孩子入队
|
||||
if (p->rchild != NULL) // 有右孩子
|
||||
EnQueue(Q, p->rchild); // 右孩子入队
|
||||
}
|
||||
DestroyQueue(Q); // 销毁队列
|
||||
}
|
||||
|
||||
|
||||
//算法5.5 先序非遍历递归算法
|
||||
template <class DT>
|
||||
void PreOrderBiTree_N(BTNode<DT>* bt)
|
||||
{
|
||||
SqStack<DT> S; // 创建栈
|
||||
int m = 20;
|
||||
InitStack(S, m);
|
||||
BTNode<DT>* p;
|
||||
p = bt;
|
||||
while (p != NULL || !StackEmpty(S)) // 树非空或栈非空
|
||||
{
|
||||
while (p != NULL) // 结点非空
|
||||
{
|
||||
cout << p->data << ' '; // 访问结点
|
||||
Push(S, p); // 入栈
|
||||
p = p->lchild; // 转左子树
|
||||
}
|
||||
if (!StackEmpty(S)) // 栈非空
|
||||
{
|
||||
Pop(S, p); // 出栈
|
||||
p = p->rchild; // 转出栈结点的右子树
|
||||
}
|
||||
}
|
||||
DestroyStack(S); //销毁栈
|
||||
}
|
||||
|
||||
|
||||
//算法5.6 中序非遍历递归算法
|
||||
template <class DT>
|
||||
void InOrderBiTree_N(BTNode<DT>* bt)
|
||||
{
|
||||
SqStack<DT> S; // 创建一个栈
|
||||
int m = 20;
|
||||
InitStack(S, m);
|
||||
BTNode<DT>* p;
|
||||
p = bt;
|
||||
while (p != NULL || !StackEmpty(S)) // 结点非空或栈非空
|
||||
{
|
||||
while (p != NULL) // 结点非空
|
||||
{
|
||||
Push(S, p); // 入栈
|
||||
p = p->lchild; // 转出栈结点右子树
|
||||
}
|
||||
if (!StackEmpty(S)) // 栈非空
|
||||
{
|
||||
Pop(S, p); // 出栈
|
||||
cout << p->data << ' '; // 访问出栈结点
|
||||
p = p->rchild; // 转出栈结点的右子树
|
||||
}
|
||||
}
|
||||
DestroyStack(S); // 销毁栈
|
||||
}
|
||||
|
||||
|
||||
|
||||
//算法5.7 后序非遍历递归算法
|
||||
template <class DT>
|
||||
void PostOrderBiTree_N(BTNode<DT>* bt)
|
||||
{
|
||||
SqStack<DT> S; // 创建一个栈
|
||||
int m = 20;
|
||||
InitStack(S, m);
|
||||
BTNode<DT>* p;
|
||||
BTNode<DT>* r;
|
||||
p = bt;
|
||||
bool flag;
|
||||
do
|
||||
{
|
||||
while (p) // 结点非空
|
||||
{
|
||||
Push(S, p); // 结点入栈
|
||||
p = p->lchild; // 转左子树
|
||||
}
|
||||
r = NULL; // 指向刚被访问点,初值为空
|
||||
flag = true; // true表示处理栈顶结点
|
||||
while (!StackEmpty(S) && flag) // 栈非空且当前处理的是栈顶结点
|
||||
{
|
||||
GetTop(S, p); // 获取栈顶元素
|
||||
if (p->rchild == r) // 如果 当前结点是栈元素的右孩子
|
||||
{
|
||||
cout << p->data << ' '; // 访问栈顶元素
|
||||
Pop(S, p); // 出栈
|
||||
r = p; // r指向被访问结点
|
||||
}
|
||||
else // 否则
|
||||
{
|
||||
p = p->rchild; // 转栈顶元素右孩子
|
||||
flag = false; // 处理非栈顶结点
|
||||
}
|
||||
}
|
||||
} while (!StackEmpty(S)); // 栈非空,循环
|
||||
cout << endl;
|
||||
DestroyStack(S); // 销毁栈
|
||||
}
|
||||
|
||||
|
||||
//算法5.8 创建二叉树
|
||||
template <class DT>
|
||||
void CreateBiTree(BTNode<DT>*& bt)
|
||||
{
|
||||
char ch;
|
||||
cin >> ch; // 输入根结点的数据
|
||||
if (ch == '#') // # 表示指针为空,说明树为空
|
||||
bt = NULL;
|
||||
else
|
||||
{
|
||||
bt = new BTNode<DT>; // 申请内存
|
||||
if (!bt)
|
||||
{
|
||||
cout << "申请内存失败!" << endl;
|
||||
exit(-1); // 申请内存失败退出
|
||||
}
|
||||
bt->data = ch;
|
||||
CreateBiTree(bt->lchild); // 创建根结点左子树
|
||||
CreateBiTree(bt->rchild); // 创建根结点右子树
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//算法5.9 销毁二叉树
|
||||
template <class DT>
|
||||
void DestroyBiTree(BTNode<DT>*& bt)
|
||||
{
|
||||
if (bt) // 树非空
|
||||
{
|
||||
DestroyBiTree(bt->lchild); // 销毁左子树
|
||||
DestroyBiTree(bt->rchild); // 销毁右子树
|
||||
delete bt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//算法5.10 结点查找
|
||||
|
||||
template<class DT>
|
||||
BTNode<DT>* Search(BTNode<DT>* bt, DT e) // 查找值为e的元素
|
||||
{
|
||||
BTNode<DT>* p;
|
||||
if (bt == NULL) // 结点为空,返回
|
||||
return NULL;
|
||||
else if (bt->data == e) // 找到,返回结点指针
|
||||
return bt;
|
||||
else // 结点值不为e
|
||||
{
|
||||
p = Search(bt->lchild, e); // 在左子树上查找
|
||||
if (p != NULL) // 找到
|
||||
return p; // 返回结点指针
|
||||
else // 未找到
|
||||
return Search(bt->rchild, e); // 转右子树上查找
|
||||
}
|
||||
}
|
||||
|
||||
//算法5.11 求树深
|
||||
template <class DT>
|
||||
int Depth(BTNode<DT>* bt)
|
||||
{
|
||||
int hl, hr;
|
||||
if (bt == NULL) // 树空
|
||||
return 0; // 深度为0
|
||||
else // 树非空
|
||||
{
|
||||
|
||||
hl = Depth(bt->lchild); // 求左子树深度
|
||||
hr = Depth(bt->lchild); // 求右子树深度
|
||||
if (hl > hr) // 左子树高
|
||||
return hl + 1; // 树高为左子树高加1
|
||||
else return hr + 1; // 左子树高,树高为左子树高加1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//算法5.12 结点计数
|
||||
template <class DT>
|
||||
int NodeCount(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt == NULL) // 空树,结点数为0
|
||||
return 0;
|
||||
else // 非空树,结点数为左、右子树结点数的和加1
|
||||
return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
|
||||
}
|
||||
|
||||
template <class DT>
|
||||
void DispBiTree(BTNode<DT>* bt, int level) // 显示树
|
||||
{
|
||||
if (bt) //空二叉树不显示
|
||||
{
|
||||
DispBiTree(bt->rchild, level + 1); //显示右子树
|
||||
cout << endl; //显示新行
|
||||
for (int i = 0; i < level - 1; i++)
|
||||
cout << " "; //确保在第level列显示节点
|
||||
cout << bt->data; //显示节点
|
||||
DispBiTree(bt->lchild, level + 1); //显示左子树
|
||||
cout << endl;
|
||||
}//if
|
||||
}
|
||||
|
||||
|
||||
template <class DT>
|
||||
int leftCount(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt->lchild == NULL && bt->rchild == NULL) // 空树,结点数为0
|
||||
return 0;
|
||||
else // 非空树,结点数为左、右子树结点数的和加1
|
||||
return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
|
||||
}
|
||||
|
||||
|
||||
//【算法3.14】 初始化队列
|
||||
template <class DT>
|
||||
void InitQueue(SqQueue<DT>& Q, int m)
|
||||
{
|
||||
Q.base = new BTNode<DT>*[m]; // 申请队列空间
|
||||
if (Q.base == NULL) // 申请空间失败
|
||||
{
|
||||
cout << "未创建成功!";
|
||||
exit(1); // 退出
|
||||
}
|
||||
Q.front = Q.rear = 0; // 设置队列属性
|
||||
Q.queuesize = m;
|
||||
}
|
||||
|
||||
|
||||
//算法3.15】 销毁列列
|
||||
template <class DT>
|
||||
void DestroyQueue(SqQueue<DT>& Q)
|
||||
{
|
||||
delete[] Q.base; // 释放队列空间
|
||||
Q.front = Q.rear = 0; // 设置队列属性
|
||||
Q.queuesize = 0;
|
||||
}
|
||||
|
||||
|
||||
//【算法3.16】 入队
|
||||
template<class DT>
|
||||
bool EnQueue(SqQueue<DT>& Q, BTNode<DT>* e)
|
||||
{
|
||||
if ((Q.rear + 1) % Q.queuesize == Q.front) // 队满
|
||||
return false; // 返回false
|
||||
Q.base[Q.rear] = e; // 出队
|
||||
Q.rear = (Q.rear + 1) % Q.queuesize; // 修改队列属性
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
//【算法3.17】 出队
|
||||
template<class DT>
|
||||
bool DeQueue(SqQueue<DT>& Q, BTNode<DT>*& e)
|
||||
{
|
||||
if (Q.front == Q.rear) // 队空
|
||||
return false;
|
||||
e = Q.base[Q.front];
|
||||
Q.front = (Q.front + 1) % Q.queuesize;
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
// 测队空
|
||||
template<class DT>
|
||||
bool QueueEmpty(SqQueue<DT> Q)
|
||||
{
|
||||
if (Q.front == Q.rear) // 队空
|
||||
return true; // 返回true
|
||||
else // 队不空
|
||||
return false; // 返回false
|
||||
}
|
||||
|
||||
//【算法3.1】 初始化栈
|
||||
template <class DT>
|
||||
void InitStack(SqStack<DT>& S, int m)
|
||||
{
|
||||
S.base = new BTNode<DT>*[m]; // 申请栈空间
|
||||
if (S.base == NULL)
|
||||
{
|
||||
cout << "未创建成功!";
|
||||
exit(1);
|
||||
}
|
||||
S.top = -1; // 空栈
|
||||
S.stacksize = m; // 栈容量为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, BTNode<DT>* e)
|
||||
{
|
||||
if (S.top == S.stacksize - 1) // 栈满,不能插入
|
||||
return false;
|
||||
S.top++;
|
||||
S.base[S.top] = e;
|
||||
return true; // 插入成功,返回true
|
||||
}
|
||||
|
||||
//【算法3.4】 出栈
|
||||
template<class DT>
|
||||
bool Pop(SqStack<DT>& S, BTNode<DT>*& e)
|
||||
{
|
||||
if (S.top == -1) //栈空
|
||||
return false;
|
||||
e = S.base[S.top];
|
||||
S.top--;
|
||||
return true; // 出栈成功,返回true
|
||||
}
|
||||
|
||||
template<class DT> // 获取栈元素
|
||||
bool GetTop(SqStack<DT>& S, BTNode<DT>*& e)
|
||||
{
|
||||
if (S.top == -1) // 栈空,返回false
|
||||
return 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;
|
||||
}
|
||||
51
Chapter5/Chapter5.sln
Normal file
51
Chapter5/Chapter5.sln
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
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}") = "BiTree", "BiTree\BiTree.vcxproj", "{904CFCDF-DA23-415A-8D68-8E51AD913458}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Huffmn", "Huffmn\Huffmn.vcxproj", "{A456A451-7570-4D79-9836-04559D3E944B}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ThrBiTree", "ThrBiTree\ThrBiTree.vcxproj", "{37EF98CA-2156-4ECB-A15D-52041DBAC78C}"
|
||||
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
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x64.Build.0 = Debug|x64
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x86.Build.0 = Debug|Win32
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x64.ActiveCfg = Release|x64
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x64.Build.0 = Release|x64
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x86.ActiveCfg = Release|Win32
|
||||
{904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x86.Build.0 = Release|Win32
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Debug|x64.Build.0 = Debug|x64
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Debug|x86.Build.0 = Debug|Win32
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Release|x64.ActiveCfg = Release|x64
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Release|x64.Build.0 = Release|x64
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Release|x86.ActiveCfg = Release|Win32
|
||||
{A456A451-7570-4D79-9836-04559D3E944B}.Release|x86.Build.0 = Release|Win32
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x64.Build.0 = Debug|x64
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x86.Build.0 = Debug|Win32
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x64.ActiveCfg = Release|x64
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x64.Build.0 = Release|x64
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x86.ActiveCfg = Release|Win32
|
||||
{37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {9F80D9BF-39B0-44C0-A084-A3293924BB63}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -18,16 +18,11 @@
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="app1-4.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{7a2bd8ec-2595-4070-be3a-2bf2140848b2}</ProjectGuid>
|
||||
<RootNamespace>app14</RootNamespace>
|
||||
<ProjectGuid>{a456a451-7570-4d79-9836-04559d3e944b}</ProjectGuid>
|
||||
<RootNamespace>Huffmn</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
@@ -131,6 +126,11 @@
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="HumanTree.cpp">
|
||||
<SDLCheck Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</SDLCheck>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
22
Chapter5/Huffmn/Huffmn.vcxproj.filters
Normal file
22
Chapter5/Huffmn/Huffmn.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="HumanTree.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
165
Chapter5/Huffmn/HumanTree.cpp
Normal file
165
Chapter5/Huffmn/HumanTree.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
#include"iostream"
|
||||
#include<iomanip>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
|
||||
struct HTNode
|
||||
{
|
||||
int weight; // 权值,设为整型
|
||||
int parent; // 双亲位置
|
||||
int lchild; // 左孩子位置
|
||||
int rchild; // 右孩子位置
|
||||
};
|
||||
|
||||
|
||||
void select(HTNode* HT, int k, int& i1, int& i2) // 在前K-1个结点中选择权值最小的两个根结点i和j
|
||||
{
|
||||
int m1, m2;
|
||||
m1 = m2 = 32767; //
|
||||
i1 = i2 = 0;
|
||||
for (int j = 0; j < k; j++)
|
||||
{
|
||||
if (HT[j].weight < m1 && HT[j].parent == -1)
|
||||
{
|
||||
m2 = m1, i2 = i1;
|
||||
m1 = HT[j].weight;
|
||||
i1 = j;
|
||||
}
|
||||
else if (HT[j].weight < m2 && HT[j].parent == -1)
|
||||
{
|
||||
m2 = HT[j].weight;
|
||||
i2 = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 算法5.16 // 构造哈夫曼树
|
||||
void HuffmanTree(HTNode*& HT, int* w, int n)
|
||||
{ // n是叶子结点的个数,w是叶子结点的权值数组
|
||||
HTNode* p;
|
||||
int k, i;
|
||||
int i1, i2;
|
||||
p = HT;
|
||||
for (i = 0; i < 2 * n - 1; i++)
|
||||
{ // 设置初始状态,所有结点的指针为空
|
||||
HT[i].weight = 0;
|
||||
HT[i].parent = -1;
|
||||
HT[i].lchild = -1;
|
||||
HT[i].rchild = -1;
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
{ // 前n个结点的权值分别为个结点的权值
|
||||
HT[i].weight = w[i];
|
||||
}
|
||||
for (k = n; k < 2 * n - 1; k++)
|
||||
{ // 构造最优二叉树
|
||||
select(HT, k, i1, i2); // 在前K-1个结点中选择权值最小的两个根结点i1和i2
|
||||
HT[i1].parent = k;
|
||||
HT[i2].parent = k;
|
||||
HT[k].weight = HT[i1].weight + HT[i2].weight;
|
||||
HT[k].lchild = i1;
|
||||
HT[k].rchild = i2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DispHT(HTNode* HT, int n) // 显示哈夫曼树存储
|
||||
{
|
||||
HTNode* p;
|
||||
p = HT;
|
||||
cout << "k" << setw(7) << "Weight" << setw(7) << "parent"
|
||||
<< setw(7) << "lchild" << setw(7) << "rchild" << endl;
|
||||
for (int k = 0; k < 2 * n - 1; k++)
|
||||
{
|
||||
cout << k << setw(7) << (p + k)->weight << setw(7) << (p + k)->parent
|
||||
<< setw(7) << (p + k)->lchild << setw(7) << (p + k)->rchild << endl;
|
||||
}
|
||||
}
|
||||
|
||||
//算法5.17 // 构建哈夫曼编码
|
||||
void CreateHFCode(HTNode* HT, int n, char** HC)
|
||||
{
|
||||
int i, start, c, f;
|
||||
char* cd;
|
||||
cd = new char[n];
|
||||
cd[n - 1] = '\0';
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
start = n - 1;
|
||||
c = i;
|
||||
f = HT[i].parent;
|
||||
while (f != -1)
|
||||
{
|
||||
if (HT[f].lchild == c)
|
||||
cd[--start] = '0';
|
||||
else
|
||||
cd[--start] = '1';
|
||||
c = f; f = HT[f].parent;
|
||||
}
|
||||
cout << endl;
|
||||
HC[i] = new char[n - start];
|
||||
HC[i] = &cd[start];
|
||||
int j = 0;
|
||||
while (HC[i][j] != '\0') // 显示编码
|
||||
{
|
||||
cout << HC[i][j];
|
||||
j++;
|
||||
}
|
||||
|
||||
}
|
||||
delete cd;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
int op;
|
||||
|
||||
int main()
|
||||
{
|
||||
int* w; // 权值数组
|
||||
int n; // 权值个数
|
||||
int i; // 工作变量
|
||||
HTNode* HT; // 哈夫曼树
|
||||
char** HC;
|
||||
do
|
||||
{
|
||||
cout << "1-输入结点权值" << endl;
|
||||
cout << "2-生成最优二叉树" << endl;
|
||||
cout << "3-求哈夫曼编码" << endl;
|
||||
cout << "4-退出程序" << endl;
|
||||
cout << "请选择操作(1~4):";
|
||||
cout << "\b\b";
|
||||
cin >> op;
|
||||
switch (op)
|
||||
{
|
||||
case 1:
|
||||
cout << "测试案例" << endl;
|
||||
cout << "7,5,2,3,5,6" << endl;
|
||||
cout << "请输入结点的个数:";
|
||||
cin >> n;
|
||||
w = new int[n];
|
||||
cout << "请依次输入权值:" << endl;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
cout << "请输入第" << i + 1 << "个权值:";
|
||||
cin >> w[i];
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
HT = new HTNode[2 * n - 1]; // 申请最优二叉树存储空间
|
||||
HuffmanTree(HT, w, n);
|
||||
cout << "创建的哈夫曼树为:\n";
|
||||
DispHT(HT, n);
|
||||
system("pause");
|
||||
break;
|
||||
case 3:
|
||||
HC = new char* [n]; // 存储哈示曼编码
|
||||
cout << "哈夫曼编码为:\n";
|
||||
CreateHFCode(HT, n, HC);
|
||||
break;
|
||||
case 4:
|
||||
cout << "结束运行,Bye-Bye!" << endl;
|
||||
break;
|
||||
}
|
||||
} while (op != 4);
|
||||
return 0;
|
||||
}
|
||||
222
Chapter5/ThrBiTree/ThrBiTree.cpp
Normal file
222
Chapter5/ThrBiTree/ThrBiTree.cpp
Normal file
@@ -0,0 +1,222 @@
|
||||
#include<iostream>//cout,cin
|
||||
#include<string>
|
||||
using namespace std;
|
||||
|
||||
template<class DT>
|
||||
struct ThrBTNode
|
||||
{
|
||||
DT data; // 数据域
|
||||
int lflag; // 左标志域
|
||||
int rflag; // 右标志域
|
||||
ThrBTNode* lchild; // 左指针域
|
||||
ThrBTNode* rchild; // 右指针域
|
||||
};
|
||||
|
||||
|
||||
template <class DT> // 创建二叉树
|
||||
void CreateBiTree(ThrBTNode<DT>*& bt)
|
||||
{ // 按先序序列的顺序输入二叉树,#为空指针域标志;
|
||||
char ch;
|
||||
cin >> ch; // 输入根结点的数据
|
||||
if (ch == '#') // # 表示指针为空,说明树为空
|
||||
bt = NULL;
|
||||
else
|
||||
{
|
||||
bt = new ThrBTNode<DT>; // 新建结点
|
||||
if (!bt)
|
||||
{
|
||||
cout << "申请内存失败!" << endl;
|
||||
exit(-1); // 申请内存失败退出
|
||||
}
|
||||
bt->lflag = 0; // 非线索
|
||||
bt->rflag = 0; // 非线索
|
||||
bt->data = ch;
|
||||
CreateBiTree(bt->lchild); // 递归创建根结点左子树
|
||||
CreateBiTree(bt->rchild); // 递归创建根结点右子树
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ThrBTNode<char>* pre;
|
||||
|
||||
//算法5.13 中序线索化二叉树
|
||||
template <class DT>
|
||||
void InThread(ThrBTNode<DT>*& p)
|
||||
{
|
||||
|
||||
if (p != NULL) // 结点非空
|
||||
{
|
||||
InThread(p->lchild); // 中序线索化左子树
|
||||
if (p->lchild == NULL) // 结点无左孩子
|
||||
{
|
||||
p->lflag = 1; // 设置前驱线索标识
|
||||
p->lchild = pre; // 左孩子指向结点遍历前驱
|
||||
}
|
||||
if (pre->rchild == NULL) // 结点无右孩子
|
||||
{
|
||||
pre->rflag = 1; // 设置后继线索标识
|
||||
pre->rchild = p; // 右孩子指向结点遍历后继
|
||||
}
|
||||
pre = p; // 前驱指向当前结点
|
||||
InThread(p->rchild); // 中序线索化右子树
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class DT> // 构建线索二叉树
|
||||
ThrBTNode<DT>* CreateInThread(ThrBTNode<DT>*& bt)
|
||||
{
|
||||
ThrBTNode<DT>* root;
|
||||
root = new ThrBTNode<DT>; // 创建头结点
|
||||
root->lflag = 0;
|
||||
root->rflag = 1;
|
||||
root->rchild = bt;
|
||||
if (bt == NULL)
|
||||
root->lchild = root;
|
||||
else
|
||||
{
|
||||
root->lchild = bt;
|
||||
pre = root;
|
||||
InThread(bt); // 中序线索化二叉树
|
||||
pre->rchild = root; // 设置循环链表
|
||||
pre->rflag = 1;
|
||||
root->rchild = pre;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//算法5.15 中序遍历中序线索二叉树
|
||||
template<class DT>
|
||||
void InThrBiTree(ThrBTNode<DT>* bt)
|
||||
{
|
||||
ThrBTNode<DT>* p;
|
||||
p = bt->lchild; // 从根结点开始
|
||||
while (p != bt) // 结点非空
|
||||
{
|
||||
while (p->lflag == 0) // 有左孩子
|
||||
p = p->lchild; // 一路左行
|
||||
cout << p->data; // 访问结点
|
||||
while (p->rflag == 1 && p->rchild != bt) // 有后继线索且非空
|
||||
{
|
||||
p = p->rchild; // 转向后继
|
||||
cout << p->data << " "; // 访问后继结点
|
||||
}
|
||||
p = p->rchild; // 无后继线索,转向右子树
|
||||
}
|
||||
}
|
||||
|
||||
template <class DT> // 显示线索二叉树
|
||||
void DispBiTree(ThrBTNode<DT>* bt, int level)
|
||||
{
|
||||
if (bt) // 空二叉树不显示
|
||||
{
|
||||
DispBiTree(bt->rchild, level + 1); // 显示右子树
|
||||
cout << endl; // 显示新行
|
||||
for (int i = 0; i < level - 1; i++)
|
||||
cout << " "; // 确保在第level列显示节点
|
||||
cout << bt->data; // 显示节点
|
||||
DispBiTree(bt->lchild, level + 1); // 显示左子树
|
||||
cout << endl;
|
||||
}//if
|
||||
}//DisplayBTree
|
||||
|
||||
//算法5.9 销毁二叉树
|
||||
template <class DT>
|
||||
void DestroyThrBiTree(ThrBTNode<DT>*& bt)
|
||||
{
|
||||
if (bt)
|
||||
{
|
||||
DestroyThrBiTree(bt->lchild);
|
||||
DestroyThrBiTree(bt->rchild);
|
||||
delete bt;
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
// 测试数据
|
||||
string fbt = "a b d # # e # # c f # # g # #"; // 满二叉树最后一层每个叶结点后有两个#
|
||||
string cbt = "a b d # # e # # c # #"; // 完全二叉树每个叶结点后有两个#
|
||||
string gbt = "a b # d # # c e # # #"; // 一般二叉树
|
||||
string obt = "a b c d # # # # #"; // 左斜树
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
//char pause;
|
||||
int level;
|
||||
ThrBTNode<char>* bt;
|
||||
|
||||
|
||||
system("cls"); // 清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); //显示主菜单
|
||||
cout << "Enter choice(1~7):";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: //创建二叉树
|
||||
cout << "测试数据参考:" << endl;
|
||||
cout << "满二叉树:" << fbt << endl;
|
||||
cout << "完全二叉树:" << cbt << endl;
|
||||
cout << "一般二叉树:" << gbt << endl;
|
||||
cout << "左斜二叉树:" << obt << endl;
|
||||
cout << "请按先序序列的顺序输入二叉树,#为空指针域标志:" << endl;
|
||||
CreateBiTree(bt);
|
||||
break;
|
||||
case 2://先序线索化二叉树
|
||||
|
||||
//PreThread(bt);
|
||||
//cin.get(pause);
|
||||
//system("pause");
|
||||
break;
|
||||
case 3://先序遍历先线索化二叉树
|
||||
|
||||
//PreOrDerBiTree(bt);
|
||||
//cin.get(pause);
|
||||
//system("pause");
|
||||
//breCreateInThread(bt);
|
||||
case 4: //中序线索化二叉树
|
||||
bt = CreateInThread(bt);
|
||||
cout << "中序线索化成功!";
|
||||
cout << endl;
|
||||
break;
|
||||
case 5: //中序遍历中序线索二叉树
|
||||
cout << "中序遍历序列为:" << endl;
|
||||
cout << endl;
|
||||
InThrBiTree(bt);
|
||||
break;
|
||||
case 6: //显示二叉树
|
||||
cout << "2-显示二叉树" << endl;
|
||||
level = 1;
|
||||
DispBiTree(bt, level);
|
||||
cout << endl;
|
||||
break;
|
||||
case 7://退出
|
||||
cout << "结束运行!" << endl;
|
||||
DestroyThrBiTree(bt);
|
||||
break;
|
||||
default://非法选择
|
||||
cout << "Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 7);
|
||||
|
||||
return 0;
|
||||
}
|
||||
135
Chapter5/ThrBiTree/ThrBiTree.vcxproj
Normal file
135
Chapter5/ThrBiTree/ThrBiTree.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>{37ef98ca-2156-4ecb-a15d-52041dbac78c}</ProjectGuid>
|
||||
<RootNamespace>ThrBiTree</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>false</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="ThrBiTree.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
22
Chapter5/ThrBiTree/ThrBiTree.vcxproj.filters
Normal file
22
Chapter5/ThrBiTree/ThrBiTree.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="ThrBiTree.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
31
Exercise/BiTreeExterned/BiTreeExterned.sln
Normal file
31
Exercise/BiTreeExterned/BiTreeExterned.sln
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34728.123
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BiTreeExterned", "BiTreeExterned.vcxproj", "{67DB3E23-69FE-496E-B980-2F234B1777BA}"
|
||||
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
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x64.Build.0 = Debug|x64
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x86.Build.0 = Debug|Win32
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x64.ActiveCfg = Release|x64
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x64.Build.0 = Release|x64
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x86.ActiveCfg = Release|Win32
|
||||
{67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {313D7A25-E7B0-4ED7-BCA3-EFCA771BB40E}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
138
Exercise/BiTreeExterned/BiTreeExterned.vcxproj
Normal file
138
Exercise/BiTreeExterned/BiTreeExterned.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>{67db3e23-69fe-496e-b980-2f234b1777ba}</ProjectGuid>
|
||||
<RootNamespace>BiTreeExterned</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>false</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="bitree.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="bitree.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
27
Exercise/BiTreeExterned/BiTreeExterned.vcxproj.filters
Normal file
27
Exercise/BiTreeExterned/BiTreeExterned.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>
|
||||
<ClCompile Include="bitree.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="bitree.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
136
Exercise/BiTreeExterned/bitree.cpp
Normal file
136
Exercise/BiTreeExterned/bitree.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include<iostream>//cout,cin
|
||||
#include<string>
|
||||
using namespace std;
|
||||
#include"bitree.h"
|
||||
|
||||
//测试参考数据
|
||||
string fbt = "a b d # # e # # c f # # g # #"; // 满二叉树最后一层每个叶结点后有两个#
|
||||
string cbt = "a b d # # e # # c # #"; // 完全二叉树每个叶结点后有两个#
|
||||
string gbt = "a b # d # # c e # # #"; // 一般二叉树
|
||||
string obt = "a b c d # # # # #"; // 左斜树1
|
||||
|
||||
|
||||
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 << "11-求二叉树结点个数\n";
|
||||
cout << "12-显示二叉树\n";
|
||||
cout << "13-删除以值为x的结点的子树\n";
|
||||
cout << "0 -退出\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int level;
|
||||
BTNode<char>* bt;
|
||||
|
||||
system("cls"); // 清屏
|
||||
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
dispmenu(); // 显示主菜单
|
||||
cout << "Enter choice(1~12,0 退出):";
|
||||
cin >> choice;
|
||||
switch (choice)
|
||||
{
|
||||
case 1: // 创建二叉树
|
||||
cout << "测试参考数据:" << endl;
|
||||
cout << "满二叉树:" << fbt << endl;
|
||||
cout << "完全二叉树:" << cbt << endl;
|
||||
cout << "一般二叉树:" << gbt << endl;
|
||||
cout << "左斜二叉树:" << obt << endl;
|
||||
cout << "请按先序序列的顺序输入二叉树,#为空指针域标志:" << endl;
|
||||
CreateBiTree(bt);
|
||||
break;
|
||||
case 2: // 先序递归遍历二叉树
|
||||
cout << "先序遍历序列为:" << endl;
|
||||
PreOrDerBiTree(bt);
|
||||
break;
|
||||
case 3: // 中序递归遍历二叉树
|
||||
cout << "中序遍历序列为:" << endl;
|
||||
InOrDerBiTree(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 4: // 后序递归遍历二叉树
|
||||
cout << "后序遍历序列为:" << endl;
|
||||
PostOrDerBiTree(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 5: // 层序遍历二叉树
|
||||
cout << "层序遍历序列为:" << endl;
|
||||
cout << endl;
|
||||
LevelBiTree(bt);
|
||||
break;
|
||||
case 6: // 先序非递归遍历二叉树
|
||||
cout << "先序非递归遍历序列为:" << endl;
|
||||
PreOrderBiTree_N(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 7: // 中序非递归遍历二叉树
|
||||
cout << "中序非递归遍历序列为:" << endl;
|
||||
InOrderBiTree_N(bt);
|
||||
cout << endl;
|
||||
break;
|
||||
case 8: // 后序非递归遍历二叉树
|
||||
cout << "后序非递归遍历序列为:" << endl;
|
||||
cout << endl;
|
||||
PostOrderBiTree_N(bt);
|
||||
break;
|
||||
case 9: // 结点查询
|
||||
char e;
|
||||
BTNode<char>* p;
|
||||
cout << "输入要查询的结点值:" << endl;
|
||||
cin >> e;
|
||||
p = Search(bt, e);
|
||||
if (p)
|
||||
{
|
||||
cout << "找到!";
|
||||
cout << p->data << endl;
|
||||
}
|
||||
else
|
||||
cout << "未找到!" << endl;
|
||||
cout << endl;
|
||||
break;
|
||||
case 10: // 求二叉树高度
|
||||
cout << "二叉树高度为:" << Depth(bt) << endl;
|
||||
cout << endl;
|
||||
break;
|
||||
case 11: //求二叉树结点个数
|
||||
cout << "二叉树结点数为:" << NodeCount(bt) << endl;
|
||||
cout << endl;
|
||||
break;
|
||||
case 12: // 显示二叉树
|
||||
cout << "2-显示二叉树" << endl;
|
||||
level = 1;
|
||||
DispBiTree(bt, level);
|
||||
cout << endl;
|
||||
break;
|
||||
case 13:
|
||||
cout << "输入结点的值:";
|
||||
char x;
|
||||
cin >> x;
|
||||
DelChildWithRootX(bt, x);
|
||||
break;
|
||||
case 0: // 退出
|
||||
DestroyBiTree(bt);
|
||||
cout << "结束运行Bye-Bye!" << endl;
|
||||
break;
|
||||
default: // 无效选择
|
||||
cout << "Invalid choice\n";
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
436
Exercise/BiTreeExterned/bitree.h
Normal file
436
Exercise/BiTreeExterned/bitree.h
Normal file
@@ -0,0 +1,436 @@
|
||||
template <class DT>
|
||||
struct BTNode
|
||||
{
|
||||
DT data; //数据域
|
||||
BTNode* lchild; //指向左子树的指针
|
||||
BTNode* rchild; //指向右子树的指针
|
||||
};
|
||||
|
||||
//队列工具
|
||||
template <class DT>
|
||||
struct SqQueue // 顺序队类
|
||||
{
|
||||
BTNode<DT>** base; // 队列首址
|
||||
int front; // 队头指针
|
||||
int rear; // 队尾指针
|
||||
int queuesize; // 队容量
|
||||
};
|
||||
|
||||
//栈
|
||||
template <class DT>
|
||||
struct SqStack // 顺序栈
|
||||
{
|
||||
BTNode<DT>** base; // 栈首址
|
||||
int top; // 栈顶指针
|
||||
int stacksize; // 栈容量
|
||||
};
|
||||
|
||||
//算法5.1 先序遍历递归算法
|
||||
template <class DT>
|
||||
void PreOrDerBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt != NULL)
|
||||
{
|
||||
cout << bt->data << ' '; //输出结点上的数据
|
||||
PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树
|
||||
PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//算法5.2 中序遍历递归算法
|
||||
template <class DT>
|
||||
void InOrDerBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt != NULL)
|
||||
{
|
||||
InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树
|
||||
cout << bt->data << ' '; //输出结点上的数据
|
||||
InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//算法5.3 后序遍历递归算法
|
||||
template <class DT>
|
||||
void PostOrDerBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt != NULL)
|
||||
{
|
||||
PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树
|
||||
PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树
|
||||
cout << bt->data << ' '; //输出结点上的数据
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//算法5.4 层序遍历算法
|
||||
template<class DT>
|
||||
void LevelBiTree(BTNode<DT>* bt)
|
||||
{
|
||||
SqQueue<DT> Q; // 创建一个队
|
||||
int m = 20;
|
||||
InitQueue(Q, m);
|
||||
BTNode<DT>* p;
|
||||
p = bt;
|
||||
if (p) EnQueue(Q, p); // 树非空,入队
|
||||
while (!QueueEmpty(Q)) // 队非空
|
||||
{
|
||||
DeQueue(Q, p); // 出队
|
||||
cout << p->data; // 访问
|
||||
if (p->lchild != NULL) // 有左孩子
|
||||
EnQueue(Q, p->lchild); // 左孩子入队
|
||||
if (p->rchild != NULL) // 有右孩子
|
||||
EnQueue(Q, p->rchild); // 右孩子入队
|
||||
}
|
||||
DestroyQueue(Q); // 销毁队列
|
||||
}
|
||||
|
||||
|
||||
//算法5.5 先序非遍历递归算法
|
||||
template <class DT>
|
||||
void PreOrderBiTree_N(BTNode<DT>* bt)
|
||||
{
|
||||
SqStack<DT> S; // 创建栈
|
||||
int m = 20;
|
||||
InitStack(S, m);
|
||||
BTNode<DT>* p;
|
||||
p = bt;
|
||||
while (p != NULL || !StackEmpty(S)) // 树非空或栈非空
|
||||
{
|
||||
while (p != NULL) // 结点非空
|
||||
{
|
||||
cout << p->data << ' '; // 访问结点
|
||||
Push(S, p); // 入栈
|
||||
p = p->lchild; // 转左子树
|
||||
}
|
||||
if (!StackEmpty(S)) // 栈非空
|
||||
{
|
||||
Pop(S, p); // 出栈
|
||||
p = p->rchild; // 转出栈结点的右子树
|
||||
}
|
||||
}
|
||||
DestroyStack(S); //销毁栈
|
||||
}
|
||||
|
||||
|
||||
//算法5.6 中序非遍历递归算法
|
||||
template <class DT>
|
||||
void InOrderBiTree_N(BTNode<DT>* bt)
|
||||
{
|
||||
SqStack<DT> S; // 创建一个栈
|
||||
int m = 20;
|
||||
InitStack(S, m);
|
||||
BTNode<DT>* p;
|
||||
p = bt;
|
||||
while (p != NULL || !StackEmpty(S)) // 结点非空或栈非空
|
||||
{
|
||||
while (p != NULL) // 结点非空
|
||||
{
|
||||
Push(S, p); // 入栈
|
||||
p = p->lchild; // 转出栈结点右子树
|
||||
}
|
||||
if (!StackEmpty(S)) // 栈非空
|
||||
{
|
||||
Pop(S, p); // 出栈
|
||||
cout << p->data << ' '; // 访问出栈结点
|
||||
p = p->rchild; // 转出栈结点的右子树
|
||||
}
|
||||
}
|
||||
DestroyStack(S); // 销毁栈
|
||||
}
|
||||
|
||||
|
||||
|
||||
//算法5.7 后序非遍历递归算法
|
||||
template <class DT>
|
||||
void PostOrderBiTree_N(BTNode<DT>* bt)
|
||||
{
|
||||
SqStack<DT> S; // 创建一个栈
|
||||
int m = 20;
|
||||
InitStack(S, m);
|
||||
BTNode<DT>* p;
|
||||
BTNode<DT>* r;
|
||||
p = bt;
|
||||
bool flag;
|
||||
do
|
||||
{
|
||||
while (p) // 结点非空
|
||||
{
|
||||
Push(S, p); // 结点入栈
|
||||
p = p->lchild; // 转左子树
|
||||
}
|
||||
r = NULL; // 指向刚被访问点,初值为空
|
||||
flag = true; // true表示处理栈顶结点
|
||||
while (!StackEmpty(S) && flag) // 栈非空且当前处理的是栈顶结点
|
||||
{
|
||||
GetTop(S, p); // 获取栈顶元素
|
||||
if (p->rchild == r) // 如果 当前结点是栈元素的右孩子
|
||||
{
|
||||
cout << p->data << ' '; // 访问栈顶元素
|
||||
Pop(S, p); // 出栈
|
||||
r = p; // r指向被访问结点
|
||||
}
|
||||
else // 否则
|
||||
{
|
||||
p = p->rchild; // 转栈顶元素右孩子
|
||||
flag = false; // 处理非栈顶结点
|
||||
}
|
||||
}
|
||||
} while (!StackEmpty(S)); // 栈非空,循环
|
||||
cout << endl;
|
||||
DestroyStack(S); // 销毁栈
|
||||
}
|
||||
|
||||
|
||||
//算法5.8 创建二叉树
|
||||
template <class DT>
|
||||
void CreateBiTree(BTNode<DT>*& bt)
|
||||
{
|
||||
char ch;
|
||||
cin >> ch; // 输入根结点的数据
|
||||
if (ch == '#') // # 表示指针为空,说明树为空
|
||||
bt = NULL;
|
||||
else
|
||||
{
|
||||
bt = new BTNode<DT>; // 申请内存
|
||||
if (!bt)
|
||||
{
|
||||
cout << "申请内存失败!" << endl;
|
||||
exit(-1); // 申请内存失败退出
|
||||
}
|
||||
bt->data = ch;
|
||||
CreateBiTree(bt->lchild); // 创建根结点左子树
|
||||
CreateBiTree(bt->rchild); // 创建根结点右子树
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//算法5.9 销毁二叉树
|
||||
template <class DT>
|
||||
void DestroyBiTree(BTNode<DT>*& bt)
|
||||
{
|
||||
if (bt) // 树非空
|
||||
{
|
||||
DestroyBiTree(bt->lchild); // 销毁左子树
|
||||
DestroyBiTree(bt->rchild); // 销毁右子树
|
||||
delete bt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//算法5.10 结点查找
|
||||
|
||||
template<class DT>
|
||||
BTNode<DT>* Search(BTNode<DT>* bt, DT e) // 查找值为e的元素
|
||||
{
|
||||
BTNode<DT>* p;
|
||||
if (bt == NULL) // 结点为空,返回
|
||||
return NULL;
|
||||
else if (bt->data == e) // 找到,返回结点指针
|
||||
return bt;
|
||||
else // 结点值不为e
|
||||
{
|
||||
p = Search(bt->lchild, e); // 在左子树上查找
|
||||
if (p != NULL) // 找到
|
||||
return p; // 返回结点指针
|
||||
else // 未找到
|
||||
return Search(bt->rchild, e); // 转右子树上查找
|
||||
}
|
||||
}
|
||||
|
||||
//算法5.11 求树深
|
||||
template <class DT>
|
||||
int Depth(BTNode<DT>* bt)
|
||||
{
|
||||
int hl, hr;
|
||||
if (bt == NULL) // 树空
|
||||
return 0; // 深度为0
|
||||
else // 树非空
|
||||
{
|
||||
|
||||
hl = Depth(bt->lchild); // 求左子树深度
|
||||
hr = Depth(bt->lchild); // 求右子树深度
|
||||
if (hl > hr) // 左子树高
|
||||
return hl + 1; // 树高为左子树高加1
|
||||
else return hr + 1; // 左子树高,树高为左子树高加1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//算法5.12 结点计数
|
||||
template <class DT>
|
||||
int NodeCount(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt == NULL) // 空树,结点数为0
|
||||
return 0;
|
||||
else // 非空树,结点数为左、右子树结点数的和加1
|
||||
return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
|
||||
}
|
||||
|
||||
template <class DT>
|
||||
void DispBiTree(BTNode<DT>* bt, int level) // 显示树
|
||||
{
|
||||
if (bt) //空二叉树不显示
|
||||
{
|
||||
DispBiTree(bt->rchild, level + 1); //显示右子树
|
||||
cout << endl; //显示新行
|
||||
for (int i = 0; i < level - 1; i++)
|
||||
cout << " "; //确保在第level列显示节点
|
||||
cout << bt->data; //显示节点
|
||||
DispBiTree(bt->lchild, level + 1); //显示左子树
|
||||
cout << endl;
|
||||
}//if
|
||||
}
|
||||
|
||||
template <class DT>
|
||||
void DelChildWithRootX(BTNode<DT>*& bt, DT x) {
|
||||
if (bt == NULL) return;
|
||||
else {
|
||||
if (bt->data == x) {
|
||||
DestroyBiTree(bt);
|
||||
bt = NULL;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
DelChildWithRootX(bt->lchild, x);
|
||||
DelChildWithRootX(bt->rchild, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class DT>
|
||||
int leftCount(BTNode<DT>* bt)
|
||||
{
|
||||
if (bt->lchild == NULL && bt->rchild == NULL) // 空树,结点数为0
|
||||
return 0;
|
||||
else // 非空树,结点数为左、右子树结点数的和加1
|
||||
return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
|
||||
}
|
||||
|
||||
|
||||
//【算法3.14】 初始化队列
|
||||
template <class DT>
|
||||
void InitQueue(SqQueue<DT>& Q, int m)
|
||||
{
|
||||
Q.base = new BTNode<DT>*[m]; // 申请队列空间
|
||||
if (Q.base == NULL) // 申请空间失败
|
||||
{
|
||||
cout << "未创建成功!";
|
||||
exit(1); // 退出
|
||||
}
|
||||
Q.front = Q.rear = 0; // 设置队列属性
|
||||
Q.queuesize = m;
|
||||
}
|
||||
|
||||
|
||||
//算法3.15】 销毁列列
|
||||
template <class DT>
|
||||
void DestroyQueue(SqQueue<DT>& Q)
|
||||
{
|
||||
delete[] Q.base; // 释放队列空间
|
||||
Q.front = Q.rear = 0; // 设置队列属性
|
||||
Q.queuesize = 0;
|
||||
}
|
||||
|
||||
|
||||
//【算法3.16】 入队
|
||||
template<class DT>
|
||||
bool EnQueue(SqQueue<DT>& Q, BTNode<DT>* e)
|
||||
{
|
||||
if ((Q.rear + 1) % Q.queuesize == Q.front) // 队满
|
||||
return false; // 返回false
|
||||
Q.base[Q.rear] = e; // 出队
|
||||
Q.rear = (Q.rear + 1) % Q.queuesize; // 修改队列属性
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
//【算法3.17】 出队
|
||||
template<class DT>
|
||||
bool DeQueue(SqQueue<DT>& Q, BTNode<DT>*& e)
|
||||
{
|
||||
if (Q.front == Q.rear) // 队空
|
||||
return false;
|
||||
e = Q.base[Q.front];
|
||||
Q.front = (Q.front + 1) % Q.queuesize;
|
||||
return true; // 删除成功,返回true
|
||||
}
|
||||
|
||||
// 测队空
|
||||
template<class DT>
|
||||
bool QueueEmpty(SqQueue<DT> Q)
|
||||
{
|
||||
if (Q.front == Q.rear) // 队空
|
||||
return true; // 返回true
|
||||
else // 队不空
|
||||
return false; // 返回false
|
||||
}
|
||||
|
||||
//【算法3.1】 初始化栈
|
||||
template <class DT>
|
||||
void InitStack(SqStack<DT>& S, int m)
|
||||
{
|
||||
S.base = new BTNode<DT>*[m]; // 申请栈空间
|
||||
if (S.base == NULL)
|
||||
{
|
||||
cout << "未创建成功!";
|
||||
exit(1);
|
||||
}
|
||||
S.top = -1; // 空栈
|
||||
S.stacksize = m; // 栈容量为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, BTNode<DT>* e)
|
||||
{
|
||||
if (S.top == S.stacksize - 1) // 栈满,不能插入
|
||||
return false;
|
||||
S.top++;
|
||||
S.base[S.top] = e;
|
||||
return true; // 插入成功,返回true
|
||||
}
|
||||
|
||||
//【算法3.4】 出栈
|
||||
template<class DT>
|
||||
bool Pop(SqStack<DT>& S, BTNode<DT>*& e)
|
||||
{
|
||||
if (S.top == -1) //栈空
|
||||
return false;
|
||||
e = S.base[S.top];
|
||||
S.top--;
|
||||
return true; // 出栈成功,返回true
|
||||
}
|
||||
|
||||
template<class DT> // 获取栈元素
|
||||
bool GetTop(SqStack<DT>& S, BTNode<DT>*& e)
|
||||
{
|
||||
if (S.top == -1) // 栈空,返回false
|
||||
return 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;
|
||||
}
|
||||
41
Exercise/Del_e_in_Linear_List/Del_e_in_Linear_List.sln
Normal file
41
Exercise/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
Exercise/Del_e_in_Linear_List/Linked_List/LinkList.h
Normal file
253
Exercise/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
Exercise/Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj
Normal file
138
Exercise/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>
|
||||
@@ -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
Exercise/Del_e_in_Linear_List/Linked_List/main.cpp
Normal file
34
Exercise/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;
|
||||
}
|
||||
@@ -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
Exercise/Del_e_in_Linear_List/Sequential_List/SqList.h
Normal file
175
Exercise/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
Exercise/Del_e_in_Linear_List/Sequential_List/main.cpp
Normal file
34
Exercise/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;
|
||||
}
|
||||
41
Exercise/Intersection_Union/Intersection_Union.sln
Normal file
41
Exercise/Intersection_Union/Intersection_Union.sln
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34701.34
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sequential_List", "Sequential_List\Sequential_List.vcxproj", "{B4E4A6F7-1B97-421C-98BB-79482B2743FE}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Linked_List", "Linked_List\Linked_List.vcxproj", "{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x64.Build.0 = Debug|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x86.Build.0 = Debug|Win32
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x64.ActiveCfg = Release|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x64.Build.0 = Release|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x86.ActiveCfg = Release|Win32
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x86.Build.0 = Release|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x64.Build.0 = Debug|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x86.Build.0 = Debug|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x64.ActiveCfg = Release|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x64.Build.0 = Release|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x86.ActiveCfg = Release|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {413D1D02-E71E-45AC-8680-BA6E93C42BA8}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
253
Exercise/Intersection_Union/Linked_List/LinkList.h
Normal file
253
Exercise/Intersection_Union/Linked_List/LinkList.h
Normal file
@@ -0,0 +1,253 @@
|
||||
|
||||
template <class DT>
|
||||
struct LNode //链表结点
|
||||
{
|
||||
DT data; //数据域,存储数据元素值
|
||||
LNode* next; //指针域,指向下一个结点
|
||||
};
|
||||
|
||||
//算法2.1
|
||||
template <class DT>
|
||||
bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱
|
||||
{
|
||||
int k;
|
||||
k = LocateElem_e(L, e); // 1.获取e的位序k
|
||||
if (k > 1) // 2.位序k大于1
|
||||
{
|
||||
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
|
||||
return true;
|
||||
}
|
||||
else // 3.元素e无前驱
|
||||
return false; // 返回false
|
||||
}
|
||||
|
||||
//【算法2.14】 创建空单链表
|
||||
template <class DT>
|
||||
bool InitList(LNode<DT>*& L)
|
||||
{
|
||||
L = new LNode<DT>; // 1.创建头结点
|
||||
if (!L) exit(1); // 2.创建失败,退出
|
||||
L->next = NULL; // 3.创建成功
|
||||
return true; // 返回true
|
||||
}
|
||||
|
||||
//【算法2.15】 尾插法创建n的元素
|
||||
template <class DT>
|
||||
bool CreateList_1(LNode<DT>*& L, int n)
|
||||
{
|
||||
int i;
|
||||
LNode<DT>* p, * s;
|
||||
p = L; //1.工作指针初始化,指向尾结点
|
||||
cout << "依次输入" << n << "个数据元素:" << endl;
|
||||
for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点
|
||||
{
|
||||
s = new LNode<DT>; // 2.1 新建一个结点s
|
||||
if (!s) // 2.2 创建失败,返回false
|
||||
return false;
|
||||
cin >> s->data; // 2.3 输入结点值
|
||||
s->next = p->next; // 2.4 s 链在表尾
|
||||
p->next = s;
|
||||
p = s; // 2.5 工作指针指向 s
|
||||
}
|
||||
return true; // 3.创建成功,返回true
|
||||
}
|
||||
|
||||
//【算法2.16】 头插法创建n个元素
|
||||
template <class DT>
|
||||
bool CreateList_2(LNode<DT>* (&L), int n)
|
||||
{
|
||||
int i;
|
||||
LNode<DT>* s;
|
||||
cout << "逆序输入" << n << "个数据元素:" << endl;
|
||||
for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
|
||||
{
|
||||
s = new LNode<DT>; // 1.1 新建一个结点 s
|
||||
if (!s) // 1.2 创建失败,返回false
|
||||
return false;
|
||||
cin >> s->data; // 1.3 输入结点值
|
||||
s->next = L->next; // 1.4 s 在头结点后
|
||||
L->next = s;
|
||||
}
|
||||
return true; // 1.创建成功,返回true
|
||||
}
|
||||
|
||||
//【算法2.17】
|
||||
template <class DT>
|
||||
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
|
||||
{
|
||||
LNode<DT>* p;
|
||||
while (L) // 1. 表非空,从头结点开始,依次释放结点
|
||||
{
|
||||
p = L; // 1.1 处理表头结点
|
||||
L = L->next; // 1.2 头指针后移
|
||||
delete p; // 1.3 释放表头结点所占内存
|
||||
}
|
||||
L = NULL; // 2.头指针指向空
|
||||
}
|
||||
|
||||
//【算法2.18】 获取第i个元素
|
||||
template<class DT>
|
||||
bool GetElem_i(LNode<DT>* L, int i, DT& e)
|
||||
{
|
||||
LNode<DT>* p; // 1.初始化
|
||||
p = L->next; // 1.1 设置工作指针,从首结点开始数结点
|
||||
int j = 1; // 1.2 计数器初始化
|
||||
while (p && j < i) // 2.定位到第i个元素结点
|
||||
{
|
||||
p = p->next; j++;
|
||||
}
|
||||
if (!p || j > i) // 3 未找到,返回false
|
||||
return false;
|
||||
else // 4. 找到
|
||||
{
|
||||
e = p->data; // 获取第i个元素值
|
||||
return true; // 返回true
|
||||
}
|
||||
}
|
||||
|
||||
//【算法2.19】 查找值为e的元素位序
|
||||
template<class DT>
|
||||
int LocateElem_e(LNode<DT>* L, DT e)
|
||||
{
|
||||
|
||||
LNode<DT>* p; // 1.初始化从首元开始查找
|
||||
p = L->next; // 1.1从首元开始查找
|
||||
int j = 1; // 1.2 计数器初值
|
||||
while (p && p->data != e) // 2.顺序查找
|
||||
{
|
||||
p = p->next; // 2.1未找到指针后移
|
||||
j++; // 2.2 计数器增1
|
||||
}
|
||||
if (p == NULL) // 3. 判断是否找到
|
||||
return 0; // 3.1末找到,返回0
|
||||
else
|
||||
return j; // 3.2 找到,返回位序
|
||||
}
|
||||
|
||||
//【算法2.20】 插入第i个元素
|
||||
template<class DT>
|
||||
bool InsertElem_i(LNode<DT>*& L, int i, DT e)
|
||||
{
|
||||
|
||||
int j = 0;
|
||||
LNode<DT>* p; // 1.初始化
|
||||
p = L; // 工作指针初始化
|
||||
while (p && j < i - 1) // 2. 定位到插入点前驱
|
||||
{
|
||||
p = p->next;
|
||||
j++;
|
||||
}
|
||||
if (!p || j > i - 1) // 3.判断定位是否成功:
|
||||
return false; // 3.1 定位失败,不能插入
|
||||
else // 3.2 定位成功
|
||||
{
|
||||
LNode<DT>* s;
|
||||
s = new LNode<DT>; // 3.2.1建立新结点
|
||||
s->data = e; // 3.2.2新结点赋值
|
||||
s->next = p->next; // 3.2.3结点S链接到p结点之后
|
||||
p->next = s;
|
||||
return true; // 3.2.4 插入成功,返回true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//【算法2.21】 删除第i个元素
|
||||
template<class DT>
|
||||
bool DeleElem_i(LNode<DT>* (&L), int i)
|
||||
{
|
||||
|
||||
LNode<DT>* p, * q; //1.初始化:设置工作指针
|
||||
p = L; //查找从头结点开始
|
||||
int j = 0; //计数器初始化
|
||||
while (p->next && j < i - 1) //2.p定位到删除点的前驱
|
||||
{
|
||||
p = p->next;
|
||||
j++;
|
||||
}
|
||||
if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
|
||||
return false; //返回false
|
||||
else //4.删除操作
|
||||
{
|
||||
q = p->next; //4.1暂存删除结点位置
|
||||
p->next = q->next; //4.2从链表中摘除删除结点
|
||||
delete q;
|
||||
return true; //4.3删除成功,返回true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//【算法2.22】 修改第i个元素值
|
||||
template<class DT>
|
||||
bool PutElem_i(LNode<DT>* (&L), int i, DT e)
|
||||
{
|
||||
LNode<DT>* p; // 1.初始化:设置工作指针
|
||||
p = L->next; // 从首结点开始,数结点
|
||||
int j = 1; // 计数器初始化
|
||||
while (p && j < i) // 2.查找第i个元素结点
|
||||
{
|
||||
p = p->next; j++;
|
||||
}
|
||||
if (!p || j > i) // 3.元素不存在,返回false
|
||||
return false;
|
||||
else // 4.定位成功
|
||||
{
|
||||
p->data = e; // 修改元素值
|
||||
return true; // 返回true
|
||||
}
|
||||
}
|
||||
|
||||
// 释放链表所占空间
|
||||
template<class DT>
|
||||
void ClearList(LNode<DT>* (&L))
|
||||
{
|
||||
|
||||
LNode<DT>* p;
|
||||
while (L->next) // 从首元结点开始,依次释放结点
|
||||
{
|
||||
p = L->next;
|
||||
L->next = p->next;
|
||||
delete p;
|
||||
}
|
||||
cout << endl << "表已清空!" << endl;
|
||||
}
|
||||
|
||||
|
||||
//【算法2.23】 测表长
|
||||
template<class DT>
|
||||
int ListLength(LNode<DT>* L)
|
||||
{ // 1.初始化
|
||||
int len = 0; // 1.1 结点计数器赋初值0
|
||||
LNode<DT>* p; // 1.2设置工作指针
|
||||
p = L; // 指向头结点
|
||||
while (p->next) // 2.数结点个数。有后继结点,
|
||||
{
|
||||
len++; p = p->next; // 结点数增1,指针后移
|
||||
}
|
||||
return len; // 3.返回表长
|
||||
}
|
||||
|
||||
//
|
||||
template<class DT>
|
||||
bool ListEmpty(LNode<DT>* L) //测表空
|
||||
{
|
||||
if (L->next == NULL)
|
||||
return true; //空表,返回1
|
||||
else
|
||||
return false; //不空,返回0
|
||||
}
|
||||
|
||||
|
||||
//【算法2.24】 遍历表
|
||||
template <class DT>
|
||||
void DispList(LNode<DT>* L) // 显示表内容
|
||||
{
|
||||
LNode<DT>* p; // 1. 设置工作指针
|
||||
p = L; // 从首元结点开始遍历
|
||||
while (p->next) // 2.依次输出各结点值
|
||||
{
|
||||
p = p->next; cout << p->data << "\t";
|
||||
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
138
Exercise/Intersection_Union/Linked_List/Linked_List.vcxproj
Normal file
138
Exercise/Intersection_Union/Linked_List/Linked_List.vcxproj
Normal file
@@ -0,0 +1,138 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{b2eef80b-772e-44e2-856d-c8def3242ee3}</ProjectGuid>
|
||||
<RootNamespace>LinkedList</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -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>
|
||||
80
Exercise/Intersection_Union/Linked_List/main.cpp
Normal file
80
Exercise/Intersection_Union/Linked_List/main.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include "LinkList.h"
|
||||
template <class DT>
|
||||
bool CreateListNoRepeat(LNode<DT>*& L, int n)
|
||||
{
|
||||
int i;
|
||||
LNode<DT>* p, * s;
|
||||
p = L;
|
||||
cout << "依次输入" << n << "个数据元素:" << endl;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
int temp;
|
||||
cin >> temp;
|
||||
if (LocateElem_e(L, temp)) i--;
|
||||
else
|
||||
{
|
||||
s = new LNode<DT>;
|
||||
if (!s) return false;
|
||||
s->data = temp;
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <class DT>
|
||||
void Union(LNode<DT>*& A, LNode<DT>*& B, int length_a, int length_b)
|
||||
{
|
||||
for (int i = 1; i <= length_b; ++i) // as "i = 0" is the head node
|
||||
{
|
||||
int temp;
|
||||
GetElem_i(B, i, temp); // You should manually get the element instead of using B.elem[i]
|
||||
if (LocateElem_e(A, temp) == 0) InsertElem_i(A, ++length_a, temp); // ++length_a to always insert at the end
|
||||
}
|
||||
}
|
||||
template <class DT>
|
||||
void Intersection(LNode<DT>*& A, LNode<DT>*& B, int length_a)
|
||||
{
|
||||
int k = 0;
|
||||
for (int i = 1; i <= length_a; ++i)
|
||||
{
|
||||
int temp;
|
||||
GetElem_i(A, i, temp);
|
||||
if (LocateElem_e(B, temp) == 0)
|
||||
{
|
||||
DeleElem_i(A, i--);
|
||||
length_a--;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
LNode<int>* A;
|
||||
LNode<int>* B;
|
||||
InitList(A);
|
||||
InitList(B);
|
||||
int length_a, length_b;
|
||||
cout << "请输入集合A的元素个数: ";
|
||||
cin >> length_a;
|
||||
CreateListNoRepeat(A, length_a);
|
||||
cout << "请输入集合B的元素个数: ";
|
||||
cin >> length_b;
|
||||
CreateListNoRepeat(B, length_b);
|
||||
cout << "集合A: ";
|
||||
DispList(A);
|
||||
cout << "集合B: ";
|
||||
DispList(B);
|
||||
Union(A, B, length_a, length_b); // You should manually pass the length of the list instead of using A.length
|
||||
cout << "A∪B: ";
|
||||
DispList(A);
|
||||
while (true) if (!DeleElem_i(A, length_a + 1)) break; // Delete elements after the original list A
|
||||
Intersection(A, B, length_a);
|
||||
cout << "A∩B: ";
|
||||
DispList(A);
|
||||
DestroyList(A);
|
||||
DestroyList(B);
|
||||
return 0;
|
||||
}
|
||||
@@ -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>{b4e4a6f7-1b97-421c-98bb-79482b2743fe}</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>
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqList.h" />
|
||||
</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>
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="SqList.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
175
Exercise/Intersection_Union/Sequential_List/SqList.h
Normal file
175
Exercise/Intersection_Union/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;
|
||||
}
|
||||
|
||||
|
||||
64
Exercise/Intersection_Union/Sequential_List/main.cpp
Normal file
64
Exercise/Intersection_Union/Sequential_List/main.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include "SqList.h"
|
||||
template <class DT>
|
||||
bool CreateListNoRepeat(SqList<DT>& L, int n)
|
||||
{
|
||||
int i;
|
||||
if (n > L.size)
|
||||
{
|
||||
cout << "元素个数大于表长,不能创建!" << endl;
|
||||
return false;
|
||||
}
|
||||
cout << "请依次输入" << n << "个元素值:" << endl;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
int temp;
|
||||
cin >> temp;
|
||||
if (LocateElem_e(L, temp) > 0) i--;
|
||||
else L.elem[i - 1] = temp;
|
||||
L.length++;
|
||||
}
|
||||
L.length = n;
|
||||
return true;
|
||||
}
|
||||
template <class DT>
|
||||
void Union(SqList<DT>& A, const SqList<DT>& B)
|
||||
{
|
||||
for (int i = 0; i < B.length; ++i) if (LocateElem_e(A, B.elem[i]) == 0) InsertElem_i(A, A.length + 1, B.elem[i]);
|
||||
}
|
||||
template <class DT>
|
||||
void Intersection(SqList<DT>& A, const SqList<DT>& B)
|
||||
{
|
||||
int k = 0;
|
||||
for (int i = 0; i < A.length; ++i) if (LocateElem_e(B, A.elem[i]) > 0) A.elem[k++] = A.elem[i];
|
||||
A.length = k;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
SqList<int> A;
|
||||
SqList<int> B;
|
||||
InitList(A, 32);
|
||||
InitList(B, 32);
|
||||
int length_a, length_b;
|
||||
cout << "请输入集合A的元素个数: ";
|
||||
cin >> length_a;
|
||||
CreateListNoRepeat(A, length_a);
|
||||
cout << "请输入集合B的元素个数: ";
|
||||
cin >> length_b;
|
||||
CreateListNoRepeat(B, length_b);
|
||||
cout << "集合A: ";
|
||||
DispList(A);
|
||||
cout << "集合B: ";
|
||||
DispList(B);
|
||||
Union(A, B);
|
||||
cout << "A∪B: ";
|
||||
DispList(A);
|
||||
A.length = length_a; // Reset A to its original length, no need to delete the elements
|
||||
Intersection(A, B);
|
||||
cout << "A∩B: ";
|
||||
DispList(A);
|
||||
DestroyList(A);
|
||||
DestroyList(B);
|
||||
return 0;
|
||||
}
|
||||
253
Exercise/LinkListOperation/LinkList.h
Normal file
253
Exercise/LinkListOperation/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;
|
||||
}
|
||||
31
Exercise/LinkListOperation/LinkListOperation.sln
Normal file
31
Exercise/LinkListOperation/LinkListOperation.sln
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
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}") = "LinkListOperation", "LinkListOperation.vcxproj", "{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}"
|
||||
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
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x64.Build.0 = Debug|x64
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Debug|x86.Build.0 = Debug|Win32
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x64.ActiveCfg = Release|x64
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x64.Build.0 = Release|x64
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x86.ActiveCfg = Release|Win32
|
||||
{BAA96EFE-86CD-47EA-8D5B-CAF1CC77BDC6}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {E04AA2D6-0453-4D15-9DFB-2771EC700397}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
138
Exercise/LinkListOperation/LinkListOperation.vcxproj
Normal file
138
Exercise/LinkListOperation/LinkListOperation.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>{baa96efe-86cd-47ea-8d5b-caf1cc77bdc6}</ProjectGuid>
|
||||
<RootNamespace>LinkListOperation</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
Exercise/LinkListOperation/LinkListOperation.vcxproj.filters
Normal file
27
Exercise/LinkListOperation/LinkListOperation.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>
|
||||
104
Exercise/LinkListOperation/main.cpp
Normal file
104
Exercise/LinkListOperation/main.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#include "LinkList.h"
|
||||
template <class DT>
|
||||
bool CreateOrderedList(LNode<DT>*& L, int n)
|
||||
{
|
||||
int i;
|
||||
LNode<DT>* p, * s;
|
||||
p = L;
|
||||
cout << "依次输入" << n << "个数据元素:" << endl;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
s = new LNode<DT>;
|
||||
if (!s) return false;
|
||||
cin >> s->data;
|
||||
if (s->data >= p->data || p == NULL)
|
||||
{
|
||||
s->next = p->next;
|
||||
p->next = s;
|
||||
p = s;
|
||||
}
|
||||
else i--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <class DT>
|
||||
void InsertElementToOrderedList(LNode<DT>*& L, int n, int length)
|
||||
{
|
||||
LNode<DT>* p;
|
||||
p = L->next;
|
||||
for (int i = 1; i <= length; i++)
|
||||
{
|
||||
if ((p->data) > n)
|
||||
{
|
||||
InsertElem_i(L, i, n);
|
||||
break;
|
||||
}
|
||||
else if (p->next == NULL)
|
||||
{
|
||||
InsertElem_i(L, i + 1, n);
|
||||
break;
|
||||
}
|
||||
else p = p->next;
|
||||
}
|
||||
}
|
||||
template <class DT>
|
||||
void MergeTwoOrderedLists(LNode<DT>*& L1, LNode<DT>*& L2)
|
||||
{
|
||||
LNode<DT>* p, * q, * s;
|
||||
p = L1->next;
|
||||
q = L1->next->next;
|
||||
s = L2->next;
|
||||
while (!ListEmpty(L2))
|
||||
{
|
||||
if (q == NULL)
|
||||
{
|
||||
p->next = s;
|
||||
return;
|
||||
}
|
||||
else if ((q->data) >= (s->data) && (p->data) <= (s->data))
|
||||
{
|
||||
LNode<DT>* t;
|
||||
t = s->next;
|
||||
s->next = q;
|
||||
p->next = s;
|
||||
s = t;
|
||||
if (s == NULL) return;
|
||||
p = p->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
p = p->next;
|
||||
q = q->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
// Task 1: Create an ordered list
|
||||
LNode<int>* L;
|
||||
InitList(L);
|
||||
int length;
|
||||
cout << "请输入有序表的长度: ";
|
||||
cin >> length;
|
||||
CreateOrderedList(L, length);
|
||||
DispList(L);
|
||||
// Task 2: Insert a new element and keep the list ordered
|
||||
int n;
|
||||
cout << "请输入插入有序表的元素: ";
|
||||
cin >> n;
|
||||
InsertElementToOrderedList(L, n, length);
|
||||
DispList(L);
|
||||
// Task 3: Merge two ordered linked list
|
||||
LNode<int>* L2;
|
||||
InitList(L2);
|
||||
int length2;
|
||||
cout << "请输入另一个有序表的长度: ";
|
||||
cin >> length2;
|
||||
CreateOrderedList(L2, length2);
|
||||
DispList(L2);
|
||||
MergeTwoOrderedLists(L, L2);
|
||||
DispList(L);
|
||||
return 0;
|
||||
}
|
||||
BIN
Experiment/ExpReportTemplate.dotx
Normal file
BIN
Experiment/ExpReportTemplate.dotx
Normal file
Binary file not shown.
BIN
Experiment/Experiment1/Report.docx
Normal file
BIN
Experiment/Experiment1/Report.docx
Normal file
Binary file not shown.
41
Experiment/Experiment1/Task1/Intersection_Union.sln
Normal file
41
Experiment/Experiment1/Task1/Intersection_Union.sln
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34701.34
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sequential_List", "Sequential_List\Sequential_List.vcxproj", "{B4E4A6F7-1B97-421C-98BB-79482B2743FE}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Linked_List", "Linked_List\Linked_List.vcxproj", "{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x64.Build.0 = Debug|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Debug|x86.Build.0 = Debug|Win32
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x64.ActiveCfg = Release|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x64.Build.0 = Release|x64
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x86.ActiveCfg = Release|Win32
|
||||
{B4E4A6F7-1B97-421C-98BB-79482B2743FE}.Release|x86.Build.0 = Release|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x64.Build.0 = Debug|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Debug|x86.Build.0 = Debug|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x64.ActiveCfg = Release|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x64.Build.0 = Release|x64
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x86.ActiveCfg = Release|Win32
|
||||
{B2EEF80B-772E-44E2-856D-C8DEF3242EE3}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {413D1D02-E71E-45AC-8680-BA6E93C42BA8}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
253
Experiment/Experiment1/Task1/Linked_List/LinkList.h
Normal file
253
Experiment/Experiment1/Task1/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
Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj
Normal file
138
Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj
Normal file
@@ -0,0 +1,138 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{b2eef80b-772e-44e2-856d-c8def3242ee3}</ProjectGuid>
|
||||
<RootNamespace>LinkedList</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="LinkList.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user