diff --git a/.gitignore b/.gitignore index 8ada255..fff7f73 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +!OrigFiles/ + ### C++ template # Prerequisites *.d diff --git a/Chapter1/Chapter1.sln b/Chapter1/Chapter1.sln index 6d89f48..fc2b98b 100644 --- a/Chapter1/Chapter1.sln +++ b/Chapter1/Chapter1.sln @@ -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 diff --git a/Chapter1/b/b.vcxproj b/Chapter1/Chapter1.vcxproj similarity index 97% rename from Chapter1/b/b.vcxproj rename to Chapter1/Chapter1.vcxproj index ae621c9..d56d019 100644 --- a/Chapter1/b/b.vcxproj +++ b/Chapter1/Chapter1.vcxproj @@ -18,14 +18,11 @@ x64 - - - 17.0 Win32Proj - {51ce097c-c445-4a3e-a678-539c70e334df} - b + {38d719c8-7d69-48a6-93dc-234c93f7b6bc} + Chapter1 10.0 @@ -129,6 +126,9 @@ true + + + diff --git a/Chapter1/app1-4/app1-4.vcxproj.filters b/Chapter1/Chapter1.vcxproj.filters similarity index 100% rename from Chapter1/app1-4/app1-4.vcxproj.filters rename to Chapter1/Chapter1.vcxproj.filters diff --git a/Chapter1/app1-4.cpp b/Chapter1/app1-4.cpp new file mode 100644 index 0000000..5524dea --- /dev/null +++ b/Chapter1/app1-4.cpp @@ -0,0 +1,33 @@ +#include//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; +} \ No newline at end of file diff --git a/Chapter1/app1-4/app1-4.cpp b/Chapter1/app1-4/app1-4.cpp deleted file mode 100644 index 244bb33..0000000 --- a/Chapter1/app1-4/app1-4.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// USE B.CPP INSTEAD - -#include -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< -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; -} \ No newline at end of file diff --git a/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj new file mode 100644 index 0000000..202b7b5 --- /dev/null +++ b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {0b0a9a4c-185a-4345-bd4e-1e48565907e2} + My25ReverseLinkList + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj.filters b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj.filters new file mode 100644 index 0000000..a5d78e2 --- /dev/null +++ b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Chapter2/2-5-ReverseLinkList/LinkList.h b/Chapter2/2-5-ReverseLinkList/LinkList.h new file mode 100644 index 0000000..6dea850 --- /dev/null +++ b/Chapter2/2-5-ReverseLinkList/LinkList.h @@ -0,0 +1,237 @@ + +template +struct LNode //链表结点 +{ + DT data; //数据域,存储数据元素值 + LNode* next; //指针域,指向下一个结点 +}; + + +//【算法2.14】 创建空单链表 +template +bool InitList(LNode
*& L) +{ + L = new LNode
; // 1.创建头结点 + if (!L) exit(1); // 2.创建失败,退出 + L->next = NULL; // 3.创建成功 + return true; // 返回true +} + +//【算法2.15】 尾插法创建n的元素 +template +bool CreateList_1(LNode
*& L, int n) +{ + LNode
* p, * s; + p = L; //1.工作指针初始化,指向尾结点 + cout << "依次输入" << n << "个数据元素:" << endl; + for (int i = 1; i <= n; i++) // 2.按元素位序正序创建各结点 + { + s = new LNode
; // 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 +bool CreateList_2(LNode
* (&L), int n) +{ + LNode
* s; + cout << "逆序输入" << n << "个数据元素:" << endl; + for (int i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点 + { + s = new LNode
; // 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 +void DestroyList(LNode
* (&L)) // 释放链表所占空间 +{ + LNode
* p; + while (L) // 1. 表非空,从头结点开始,依次释放结点 + { + p = L; // 1.1 处理表头结点 + L = L->next; // 1.2 头指针后移 + delete p; // 1.3 释放表头结点所占内存 + } + L = NULL; // 2.头指针指向空 +} + +//【算法2.18】 获取第i个元素 +template +bool GetElem_i(LNode
* L, int i, DT& e) +{ + LNode
* 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 +int LocateElem_e(LNode
* L, DT e) +{ + + LNode
* 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 +bool InsertElem_i(LNode
* (&L), int i, DT e) +{ + + int j = 0; + LNode
* 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
* s; + s = new LNode
; // 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 +bool DeleElem_i(LNode
* (&L), int i) +{ + + LNode
* 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 +bool PutElem_i(LNode
* (&L), int i, DT e) +{ + LNode
* 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 +void ClearList(LNode
* (&L)) +{ + + LNode
* p; + while (L->next) // 从首元结点开始,依次释放结点 + { + p = L->next; + L->next = p->next; + delete p; + } + cout << endl << "表已清空!" << endl; +} + + +//【算法2.23】 测表长 +template +int ListLength(LNode
* L) +{ // 1.初始化 + int len = 0; // 1.1 结点计数器赋初值0 + LNode
* p; // 1.2设置工作指针 + p = L; // 指向头结点 + while (p->next) // 2.数结点个数。有后继结点, + { + len++; p = p->next; // 结点数增1,指针后移 + } + return len; // 3.返回表长 +} + +// +template +bool ListEmpty(LNode
* L) //测表空 +{ + if (L->next == NULL) + return true; //空表,返回1 + else + return false; //不空,返回0 +} + + +//【算法2.24】 遍历表 +template +void DispList(LNode
* L) // 显示表内容 +{ + LNode
* p; // 1. 设置工作指针 + p = L->next; // 从首元结点开始遍历 + while (p) // 2.依次输出各结点值 + { + cout << p->data << "\t"; + p = p->next; + + } + cout << endl; +} diff --git a/Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp b/Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp new file mode 100644 index 0000000..976551f --- /dev/null +++ b/Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp @@ -0,0 +1,82 @@ +#include //cout,cin +using namespace std; +#include "LinkList.h" + + +//算法2.25 单链表逆置 +void ReverseLinkList(LNode*& L) +{ + LNode* 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* 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 \ No newline at end of file diff --git a/Chapter2/Chapter2.sln b/Chapter2/Chapter2.sln index 2c77d0e..580c60b 100644 --- a/Chapter2/Chapter2.sln +++ b/Chapter2/Chapter2.sln @@ -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 diff --git a/Chapter3/SqQueue/SqQueue.cpp b/Chapter3/SqQueue/SqQueue.cpp index c771ee7..039a02f 100644 --- a/Chapter3/SqQueue/SqQueue.cpp +++ b/Chapter3/SqQueue/SqQueue.cpp @@ -108,7 +108,7 @@ int main() cout << endl; break; case 10: - cout << "\nQ.fornt=" << Q.front << endl; + cout << "\nQ.front=" << Q.front << endl; cout << "Q.rear=" << Q.rear << endl; break; case 0: // 退出 diff --git a/Chapter3/SqQueue/SqQueue.h b/Chapter3/SqQueue/SqQueue.h new file mode 100644 index 0000000..4024a2a --- /dev/null +++ b/Chapter3/SqQueue/SqQueue.h @@ -0,0 +1,100 @@ +template +struct SqQueue +{ + DT* base; // 存储空间基地址 + int front; // 队头指针,指向队首元素 + int rear; // 队尾指针,指向队尾元素的后面 + int queuesize; // 队列容量 +}; + +template // 算法3.14 初始化队列 +bool InitQueue(SqQueue
& 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 // 算法3.15 销毁队列 +void DestroyQueue(SqQueue
& Q) // 销毁循环队列 +{ + delete[] Q.base; // 1. 释放循环队列占用的内存空间 + Q.front = Q.rear = 0; // 2. 为队列属性赋值 + Q.queuesize = 0; +} + +template // 算法3.16 入队 +bool EnQueue(SqQueue
& 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 // 算法3.17 出队 +bool DeQueue(SqQueue
& 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 // 算法3.18 取队头元素 +bool GetHead(SqQueue
Q, DT& e) // 取队头元素 +{ + if (Q.front == Q.rear) // 1. 队空的情况 + return false; // 无队头元素,返回false + e = Q.base[Q.front]; // 2. 取队头元素,赋值给e + return true; // 3. 返回true +} + +template +bool GetTail(SqQueue
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 +int QueueLength(SqQueue
Q) // 测表长 +{ + return (Q.rear - Q.front + Q.queuesize) % Q.queuesize; +} + +template +bool QueueEmpty(SqQueue
Q) // 判队空 +{ + return Q.front == Q.rear; +} + +template +bool QueueFull(SqQueue
Q) // 判队满 +{ + return (Q.rear + 1) % Q.queuesize == Q.front; +} + +template +void ClearQueue(SqQueue
& Q) // 清空队 +{ + Q.front = Q.rear = 0; +} + +template +void DispQueue(SqQueue
Q) // 显示队 +{ + int i = Q.front; + while (i != Q.rear) + { + cout << Q.base[i] << " "; + i = (i + 1) % Q.queuesize; + } + cout << endl; +} \ No newline at end of file diff --git a/Chapter3/SqQueue/SqQueue.vcxproj b/Chapter3/SqQueue/SqQueue.vcxproj index ed6ea7b..3c08411 100644 --- a/Chapter3/SqQueue/SqQueue.vcxproj +++ b/Chapter3/SqQueue/SqQueue.vcxproj @@ -129,6 +129,9 @@ + + + diff --git a/Chapter3/SqQueue/SqQueue.vcxproj.filters b/Chapter3/SqQueue/SqQueue.vcxproj.filters index fba4700..423f530 100644 --- a/Chapter3/SqQueue/SqQueue.vcxproj.filters +++ b/Chapter3/SqQueue/SqQueue.vcxproj.filters @@ -19,4 +19,9 @@ 源文件 + + + 头文件 + + \ No newline at end of file diff --git a/Chapter4/AddMatrix/AddMatrix.cpp b/Chapter4/AddMatrix/AddMatrix.cpp new file mode 100644 index 0000000..31a7c3e --- /dev/null +++ b/Chapter4/AddMatrix/AddMatrix.cpp @@ -0,0 +1,203 @@ +#include//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); // 输出和矩阵 +} \ No newline at end of file diff --git a/Chapter4/AddMatrix/AddMatrix.vcxproj b/Chapter4/AddMatrix/AddMatrix.vcxproj new file mode 100644 index 0000000..bae4e33 --- /dev/null +++ b/Chapter4/AddMatrix/AddMatrix.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {01a69829-c675-4cad-99d2-35be3dc4ed2c} + AddMatrix + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/Chapter1/b/b.vcxproj.filters b/Chapter4/AddMatrix/AddMatrix.vcxproj.filters similarity index 95% rename from Chapter1/b/b.vcxproj.filters rename to Chapter4/AddMatrix/AddMatrix.vcxproj.filters index a43892b..e873347 100644 --- a/Chapter1/b/b.vcxproj.filters +++ b/Chapter4/AddMatrix/AddMatrix.vcxproj.filters @@ -15,7 +15,7 @@ - + 源文件 diff --git a/Chapter4/Chapter4.sln b/Chapter4/Chapter4.sln new file mode 100644 index 0000000..4a8b82a --- /dev/null +++ b/Chapter4/Chapter4.sln @@ -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 diff --git a/Chapter4/MatrixTrans/MatrixTrans.cpp b/Chapter4/MatrixTrans/MatrixTrans.cpp new file mode 100644 index 0000000..ecd08d9 --- /dev/null +++ b/Chapter4/MatrixTrans/MatrixTrans.cpp @@ -0,0 +1,130 @@ +#include//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); +}// \ No newline at end of file diff --git a/Chapter4/MatrixTrans/MatrixTrans.vcxproj b/Chapter4/MatrixTrans/MatrixTrans.vcxproj new file mode 100644 index 0000000..2dab3a5 --- /dev/null +++ b/Chapter4/MatrixTrans/MatrixTrans.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {ae81732d-d011-4a36-9d89-0c258adb5627} + MatrixTrans + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters b/Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters new file mode 100644 index 0000000..334c1b3 --- /dev/null +++ b/Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/Chapter5/BiTree/BiTree.vcxproj b/Chapter5/BiTree/BiTree.vcxproj new file mode 100644 index 0000000..2c4c165 --- /dev/null +++ b/Chapter5/BiTree/BiTree.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {904cfcdf-da23-415a-8d68-8e51ad913458} + BiTree + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter5/BiTree/BiTree.vcxproj.filters b/Chapter5/BiTree/BiTree.vcxproj.filters new file mode 100644 index 0000000..81b66a3 --- /dev/null +++ b/Chapter5/BiTree/BiTree.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + \ No newline at end of file diff --git a/Chapter5/BiTree/bitree.cpp b/Chapter5/BiTree/bitree.cpp new file mode 100644 index 0000000..c89a44a --- /dev/null +++ b/Chapter5/BiTree/bitree.cpp @@ -0,0 +1,129 @@ +#include//cout,cin +#include +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* 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* 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; +} \ No newline at end of file diff --git a/Chapter5/BiTree/bitree.h b/Chapter5/BiTree/bitree.h new file mode 100644 index 0000000..dd9d543 --- /dev/null +++ b/Chapter5/BiTree/bitree.h @@ -0,0 +1,421 @@ +template +struct BTNode +{ + DT data; //数据域 + BTNode* lchild; //指向左子树的指针 + BTNode* rchild; //指向右子树的指针 +}; + +//队列工具 +template +struct SqQueue // 顺序队类 +{ + BTNode
** base; // 队列首址 + int front; // 队头指针 + int rear; // 队尾指针 + int queuesize; // 队容量 +}; + +//栈 +template +struct SqStack // 顺序栈 +{ + BTNode
** base; // 栈首址 + int top; // 栈顶指针 + int stacksize; // 栈容量 +}; + +//算法5.1 先序遍历递归算法 +template +void PreOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + cout << bt->data << ' '; //输出结点上的数据 + PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树 + PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树 + } + return; +} + +//算法5.2 中序遍历递归算法 +template +void InOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树 + cout << bt->data << ' '; //输出结点上的数据 + InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树 + } + return; +} + +//算法5.3 后序遍历递归算法 +template +void PostOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树 + PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树 + cout << bt->data << ' '; //输出结点上的数据 + } + return; +} + + +//算法5.4 层序遍历算法 +template +void LevelBiTree(BTNode
* bt) +{ + SqQueue
Q; // 创建一个队 + int m = 20; + InitQueue(Q, m); + BTNode
* 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 +void PreOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建栈 + int m = 20; + InitStack(S, m); + BTNode
* 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 +void InOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建一个栈 + int m = 20; + InitStack(S, m); + BTNode
* 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 +void PostOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建一个栈 + int m = 20; + InitStack(S, m); + BTNode
* p; + BTNode
* 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 +void CreateBiTree(BTNode
*& bt) +{ + char ch; + cin >> ch; // 输入根结点的数据 + if (ch == '#') // # 表示指针为空,说明树为空 + bt = NULL; + else + { + bt = new BTNode
; // 申请内存 + if (!bt) + { + cout << "申请内存失败!" << endl; + exit(-1); // 申请内存失败退出 + } + bt->data = ch; + CreateBiTree(bt->lchild); // 创建根结点左子树 + CreateBiTree(bt->rchild); // 创建根结点右子树 + } + return; +} + + +//算法5.9 销毁二叉树 +template +void DestroyBiTree(BTNode
*& bt) +{ + if (bt) // 树非空 + { + DestroyBiTree(bt->lchild); // 销毁左子树 + DestroyBiTree(bt->rchild); // 销毁右子树 + delete bt; + } +} + + +//算法5.10 结点查找 + +template +BTNode
* Search(BTNode
* bt, DT e) // 查找值为e的元素 +{ + BTNode
* 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 +int Depth(BTNode
* 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 +int NodeCount(BTNode
* bt) +{ + if (bt == NULL) // 空树,结点数为0 + return 0; + else // 非空树,结点数为左、右子树结点数的和加1 + return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1; +} + +template +void DispBiTree(BTNode
* 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 +int leftCount(BTNode
* bt) +{ + if (bt->lchild == NULL && bt->rchild == NULL) // 空树,结点数为0 + return 0; + else // 非空树,结点数为左、右子树结点数的和加1 + return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1; +} + + +//【算法3.14】 初始化队列 +template +void InitQueue(SqQueue
& Q, int m) +{ + Q.base = new BTNode
*[m]; // 申请队列空间 + if (Q.base == NULL) // 申请空间失败 + { + cout << "未创建成功!"; + exit(1); // 退出 + } + Q.front = Q.rear = 0; // 设置队列属性 + Q.queuesize = m; +} + + +//算法3.15】 销毁列列 +template +void DestroyQueue(SqQueue
& Q) +{ + delete[] Q.base; // 释放队列空间 + Q.front = Q.rear = 0; // 设置队列属性 + Q.queuesize = 0; +} + + +//【算法3.16】 入队 +template +bool EnQueue(SqQueue
& Q, BTNode
* 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 +bool DeQueue(SqQueue
& Q, BTNode
*& e) +{ + if (Q.front == Q.rear) // 队空 + return false; + e = Q.base[Q.front]; + Q.front = (Q.front + 1) % Q.queuesize; + return true; // 删除成功,返回true +} + +// 测队空 +template +bool QueueEmpty(SqQueue
Q) +{ + if (Q.front == Q.rear) // 队空 + return true; // 返回true + else // 队不空 + return false; // 返回false +} + +//【算法3.1】 初始化栈 +template +void InitStack(SqStack
& S, int m) +{ + S.base = new BTNode
*[m]; // 申请栈空间 + if (S.base == NULL) + { + cout << "未创建成功!"; + exit(1); + } + S.top = -1; // 空栈 + S.stacksize = m; // 栈容量为m +} + + +//算法3.2】 销毁栈 +template +void DestroyStack(SqStack
& S)//析构函数 +{ + delete[] S.base; //释放栈空间 + S.top = -1; + S.stacksize = 0; +} + + +//【算法3.3】 +template +bool Push(SqStack
& S, BTNode
* e) +{ + if (S.top == S.stacksize - 1) // 栈满,不能插入 + return false; + S.top++; + S.base[S.top] = e; + return true; // 插入成功,返回true +} + +//【算法3.4】 出栈 +template +bool Pop(SqStack
& S, BTNode
*& e) +{ + if (S.top == -1) //栈空 + return false; + e = S.base[S.top]; + S.top--; + return true; // 出栈成功,返回true +} + +template // 获取栈元素 +bool GetTop(SqStack
& S, BTNode
*& e) +{ + if (S.top == -1) // 栈空,返回false + return false; + e = S.base[S.top]; // 取栈元素 + return true; // 返回true +} + + +// 测栈空 +template +bool StackEmpty(SqStack
S) +{ + if (S.top == -1) // 栈空,返回true + return true; + else // 栈非空,返回false + return false; +} \ No newline at end of file diff --git a/Chapter5/Chapter5.sln b/Chapter5/Chapter5.sln new file mode 100644 index 0000000..1e0c50a --- /dev/null +++ b/Chapter5/Chapter5.sln @@ -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 diff --git a/Chapter1/app1-4/app1-4.vcxproj b/Chapter5/Huffmn/Huffmn.vcxproj similarity index 96% rename from Chapter1/app1-4/app1-4.vcxproj rename to Chapter5/Huffmn/Huffmn.vcxproj index 5fe8cd1..b8d0d55 100644 --- a/Chapter1/app1-4/app1-4.vcxproj +++ b/Chapter5/Huffmn/Huffmn.vcxproj @@ -18,16 +18,11 @@ x64 - - - true - - 17.0 Win32Proj - {7a2bd8ec-2595-4070-be3a-2bf2140848b2} - app14 + {a456a451-7570-4d79-9836-04559d3e944b} + Huffmn 10.0 @@ -131,6 +126,11 @@ true + + + false + + diff --git a/Chapter5/Huffmn/Huffmn.vcxproj.filters b/Chapter5/Huffmn/Huffmn.vcxproj.filters new file mode 100644 index 0000000..ad4ee75 --- /dev/null +++ b/Chapter5/Huffmn/Huffmn.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/Chapter5/Huffmn/HumanTree.cpp b/Chapter5/Huffmn/HumanTree.cpp new file mode 100644 index 0000000..0a0fc98 --- /dev/null +++ b/Chapter5/Huffmn/HumanTree.cpp @@ -0,0 +1,165 @@ +#include"iostream" +#include +#include +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; +} \ No newline at end of file diff --git a/Chapter5/ThrBiTree/ThrBiTree.cpp b/Chapter5/ThrBiTree/ThrBiTree.cpp new file mode 100644 index 0000000..0680867 --- /dev/null +++ b/Chapter5/ThrBiTree/ThrBiTree.cpp @@ -0,0 +1,222 @@ +#include//cout,cin +#include +using namespace std; + +template +struct ThrBTNode +{ + DT data; // 数据域 + int lflag; // 左标志域 + int rflag; // 右标志域 + ThrBTNode* lchild; // 左指针域 + ThrBTNode* rchild; // 右指针域 +}; + + +template // 创建二叉树 +void CreateBiTree(ThrBTNode
*& bt) +{ // 按先序序列的顺序输入二叉树,#为空指针域标志; + char ch; + cin >> ch; // 输入根结点的数据 + if (ch == '#') // # 表示指针为空,说明树为空 + bt = NULL; + else + { + bt = new ThrBTNode
; // 新建结点 + if (!bt) + { + cout << "申请内存失败!" << endl; + exit(-1); // 申请内存失败退出 + } + bt->lflag = 0; // 非线索 + bt->rflag = 0; // 非线索 + bt->data = ch; + CreateBiTree(bt->lchild); // 递归创建根结点左子树 + CreateBiTree(bt->rchild); // 递归创建根结点右子树 + } + return; +} + +ThrBTNode* pre; + +//算法5.13 中序线索化二叉树 +template +void InThread(ThrBTNode
*& 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 // 构建线索二叉树 +ThrBTNode
* CreateInThread(ThrBTNode
*& bt) +{ + ThrBTNode
* root; + root = new ThrBTNode
; // 创建头结点 + 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 +void InThrBiTree(ThrBTNode
* bt) +{ + ThrBTNode
* 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 // 显示线索二叉树 +void DispBiTree(ThrBTNode
* 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 +void DestroyThrBiTree(ThrBTNode
*& 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* 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; +} \ No newline at end of file diff --git a/Chapter5/ThrBiTree/ThrBiTree.vcxproj b/Chapter5/ThrBiTree/ThrBiTree.vcxproj new file mode 100644 index 0000000..2c70883 --- /dev/null +++ b/Chapter5/ThrBiTree/ThrBiTree.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {37ef98ca-2156-4ecb-a15d-52041dbac78c} + ThrBiTree + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/Chapter5/ThrBiTree/ThrBiTree.vcxproj.filters b/Chapter5/ThrBiTree/ThrBiTree.vcxproj.filters new file mode 100644 index 0000000..8bbed55 --- /dev/null +++ b/Chapter5/ThrBiTree/ThrBiTree.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/Exercise/BiTreeExterned/BiTreeExterned.sln b/Exercise/BiTreeExterned/BiTreeExterned.sln new file mode 100644 index 0000000..0ec47fc --- /dev/null +++ b/Exercise/BiTreeExterned/BiTreeExterned.sln @@ -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 diff --git a/Exercise/BiTreeExterned/BiTreeExterned.vcxproj b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj new file mode 100644 index 0000000..a4fe4ca --- /dev/null +++ b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {67db3e23-69fe-496e-b980-2f234b1777ba} + BiTreeExterned + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Exercise/BiTreeExterned/BiTreeExterned.vcxproj.filters b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj.filters new file mode 100644 index 0000000..1c99778 --- /dev/null +++ b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Exercise/BiTreeExterned/bitree.cpp b/Exercise/BiTreeExterned/bitree.cpp new file mode 100644 index 0000000..580fb66 --- /dev/null +++ b/Exercise/BiTreeExterned/bitree.cpp @@ -0,0 +1,136 @@ +#include//cout,cin +#include +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* 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* 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; +} \ No newline at end of file diff --git a/Exercise/BiTreeExterned/bitree.h b/Exercise/BiTreeExterned/bitree.h new file mode 100644 index 0000000..ef78468 --- /dev/null +++ b/Exercise/BiTreeExterned/bitree.h @@ -0,0 +1,436 @@ +template +struct BTNode +{ + DT data; //数据域 + BTNode* lchild; //指向左子树的指针 + BTNode* rchild; //指向右子树的指针 +}; + +//队列工具 +template +struct SqQueue // 顺序队类 +{ + BTNode
** base; // 队列首址 + int front; // 队头指针 + int rear; // 队尾指针 + int queuesize; // 队容量 +}; + +//栈 +template +struct SqStack // 顺序栈 +{ + BTNode
** base; // 栈首址 + int top; // 栈顶指针 + int stacksize; // 栈容量 +}; + +//算法5.1 先序遍历递归算法 +template +void PreOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + cout << bt->data << ' '; //输出结点上的数据 + PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树 + PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树 + } + return; +} + +//算法5.2 中序遍历递归算法 +template +void InOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树 + cout << bt->data << ' '; //输出结点上的数据 + InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树 + } + return; +} + +//算法5.3 后序遍历递归算法 +template +void PostOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树 + PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树 + cout << bt->data << ' '; //输出结点上的数据 + } + return; +} + + +//算法5.4 层序遍历算法 +template +void LevelBiTree(BTNode
* bt) +{ + SqQueue
Q; // 创建一个队 + int m = 20; + InitQueue(Q, m); + BTNode
* 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 +void PreOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建栈 + int m = 20; + InitStack(S, m); + BTNode
* 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 +void InOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建一个栈 + int m = 20; + InitStack(S, m); + BTNode
* 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 +void PostOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建一个栈 + int m = 20; + InitStack(S, m); + BTNode
* p; + BTNode
* 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 +void CreateBiTree(BTNode
*& bt) +{ + char ch; + cin >> ch; // 输入根结点的数据 + if (ch == '#') // # 表示指针为空,说明树为空 + bt = NULL; + else + { + bt = new BTNode
; // 申请内存 + if (!bt) + { + cout << "申请内存失败!" << endl; + exit(-1); // 申请内存失败退出 + } + bt->data = ch; + CreateBiTree(bt->lchild); // 创建根结点左子树 + CreateBiTree(bt->rchild); // 创建根结点右子树 + } + return; +} + + +//算法5.9 销毁二叉树 +template +void DestroyBiTree(BTNode
*& bt) +{ + if (bt) // 树非空 + { + DestroyBiTree(bt->lchild); // 销毁左子树 + DestroyBiTree(bt->rchild); // 销毁右子树 + delete bt; + } +} + + +//算法5.10 结点查找 + +template +BTNode
* Search(BTNode
* bt, DT e) // 查找值为e的元素 +{ + BTNode
* 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 +int Depth(BTNode
* 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 +int NodeCount(BTNode
* bt) +{ + if (bt == NULL) // 空树,结点数为0 + return 0; + else // 非空树,结点数为左、右子树结点数的和加1 + return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1; +} + +template +void DispBiTree(BTNode
* 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 +void DelChildWithRootX(BTNode
*& 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 +int leftCount(BTNode
* bt) +{ + if (bt->lchild == NULL && bt->rchild == NULL) // 空树,结点数为0 + return 0; + else // 非空树,结点数为左、右子树结点数的和加1 + return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1; +} + + +//【算法3.14】 初始化队列 +template +void InitQueue(SqQueue
& Q, int m) +{ + Q.base = new BTNode
*[m]; // 申请队列空间 + if (Q.base == NULL) // 申请空间失败 + { + cout << "未创建成功!"; + exit(1); // 退出 + } + Q.front = Q.rear = 0; // 设置队列属性 + Q.queuesize = m; +} + + +//算法3.15】 销毁列列 +template +void DestroyQueue(SqQueue
& Q) +{ + delete[] Q.base; // 释放队列空间 + Q.front = Q.rear = 0; // 设置队列属性 + Q.queuesize = 0; +} + + +//【算法3.16】 入队 +template +bool EnQueue(SqQueue
& Q, BTNode
* 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 +bool DeQueue(SqQueue
& Q, BTNode
*& e) +{ + if (Q.front == Q.rear) // 队空 + return false; + e = Q.base[Q.front]; + Q.front = (Q.front + 1) % Q.queuesize; + return true; // 删除成功,返回true +} + +// 测队空 +template +bool QueueEmpty(SqQueue
Q) +{ + if (Q.front == Q.rear) // 队空 + return true; // 返回true + else // 队不空 + return false; // 返回false +} + +//【算法3.1】 初始化栈 +template +void InitStack(SqStack
& S, int m) +{ + S.base = new BTNode
*[m]; // 申请栈空间 + if (S.base == NULL) + { + cout << "未创建成功!"; + exit(1); + } + S.top = -1; // 空栈 + S.stacksize = m; // 栈容量为m +} + + +//算法3.2】 销毁栈 +template +void DestroyStack(SqStack
& S)//析构函数 +{ + delete[] S.base; //释放栈空间 + S.top = -1; + S.stacksize = 0; +} + + +//【算法3.3】 +template +bool Push(SqStack
& S, BTNode
* e) +{ + if (S.top == S.stacksize - 1) // 栈满,不能插入 + return false; + S.top++; + S.base[S.top] = e; + return true; // 插入成功,返回true +} + +//【算法3.4】 出栈 +template +bool Pop(SqStack
& S, BTNode
*& e) +{ + if (S.top == -1) //栈空 + return false; + e = S.base[S.top]; + S.top--; + return true; // 出栈成功,返回true +} + +template // 获取栈元素 +bool GetTop(SqStack
& S, BTNode
*& e) +{ + if (S.top == -1) // 栈空,返回false + return false; + e = S.base[S.top]; // 取栈元素 + return true; // 返回true +} + + +// 测栈空 +template +bool StackEmpty(SqStack
S) +{ + if (S.top == -1) // 栈空,返回true + return true; + else // 栈非空,返回false + return false; +} \ No newline at end of file diff --git a/Del_e_in_Linear_List/Del_e_in_Linear_List.sln b/Exercise/Del_e_in_Linear_List/Del_e_in_Linear_List.sln similarity index 100% rename from Del_e_in_Linear_List/Del_e_in_Linear_List.sln rename to Exercise/Del_e_in_Linear_List/Del_e_in_Linear_List.sln diff --git a/Del_e_in_Linear_List/Linked_List/LinkList.h b/Exercise/Del_e_in_Linear_List/Linked_List/LinkList.h similarity index 100% rename from Del_e_in_Linear_List/Linked_List/LinkList.h rename to Exercise/Del_e_in_Linear_List/Linked_List/LinkList.h diff --git a/Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj b/Exercise/Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj similarity index 100% rename from Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj rename to Exercise/Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj diff --git a/Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj.filters b/Exercise/Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj.filters similarity index 100% rename from Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj.filters rename to Exercise/Del_e_in_Linear_List/Linked_List/Linked_List.vcxproj.filters diff --git a/Del_e_in_Linear_List/Linked_List/main.cpp b/Exercise/Del_e_in_Linear_List/Linked_List/main.cpp similarity index 100% rename from Del_e_in_Linear_List/Linked_List/main.cpp rename to Exercise/Del_e_in_Linear_List/Linked_List/main.cpp diff --git a/Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj b/Exercise/Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj similarity index 100% rename from Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj rename to Exercise/Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj diff --git a/Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj.filters b/Exercise/Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj.filters similarity index 100% rename from Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj.filters rename to Exercise/Del_e_in_Linear_List/Sequential_List/Sequential_List.vcxproj.filters diff --git a/Del_e_in_Linear_List/Sequential_List/SqList.h b/Exercise/Del_e_in_Linear_List/Sequential_List/SqList.h similarity index 100% rename from Del_e_in_Linear_List/Sequential_List/SqList.h rename to Exercise/Del_e_in_Linear_List/Sequential_List/SqList.h diff --git a/Del_e_in_Linear_List/Sequential_List/main.cpp b/Exercise/Del_e_in_Linear_List/Sequential_List/main.cpp similarity index 100% rename from Del_e_in_Linear_List/Sequential_List/main.cpp rename to Exercise/Del_e_in_Linear_List/Sequential_List/main.cpp diff --git a/Intersection_Union/Intersection_Union.sln b/Exercise/Intersection_Union/Intersection_Union.sln similarity index 100% rename from Intersection_Union/Intersection_Union.sln rename to Exercise/Intersection_Union/Intersection_Union.sln diff --git a/Intersection_Union/Linked_List/LinkList.h b/Exercise/Intersection_Union/Linked_List/LinkList.h similarity index 100% rename from Intersection_Union/Linked_List/LinkList.h rename to Exercise/Intersection_Union/Linked_List/LinkList.h diff --git a/Intersection_Union/Linked_List/Linked_List.vcxproj b/Exercise/Intersection_Union/Linked_List/Linked_List.vcxproj similarity index 100% rename from Intersection_Union/Linked_List/Linked_List.vcxproj rename to Exercise/Intersection_Union/Linked_List/Linked_List.vcxproj diff --git a/Intersection_Union/Linked_List/Linked_List.vcxproj.filters b/Exercise/Intersection_Union/Linked_List/Linked_List.vcxproj.filters similarity index 100% rename from Intersection_Union/Linked_List/Linked_List.vcxproj.filters rename to Exercise/Intersection_Union/Linked_List/Linked_List.vcxproj.filters diff --git a/Intersection_Union/Linked_List/main.cpp b/Exercise/Intersection_Union/Linked_List/main.cpp similarity index 100% rename from Intersection_Union/Linked_List/main.cpp rename to Exercise/Intersection_Union/Linked_List/main.cpp diff --git a/Intersection_Union/Sequential_List/Sequential_List.vcxproj b/Exercise/Intersection_Union/Sequential_List/Sequential_List.vcxproj similarity index 100% rename from Intersection_Union/Sequential_List/Sequential_List.vcxproj rename to Exercise/Intersection_Union/Sequential_List/Sequential_List.vcxproj diff --git a/Intersection_Union/Sequential_List/Sequential_List.vcxproj.filters b/Exercise/Intersection_Union/Sequential_List/Sequential_List.vcxproj.filters similarity index 100% rename from Intersection_Union/Sequential_List/Sequential_List.vcxproj.filters rename to Exercise/Intersection_Union/Sequential_List/Sequential_List.vcxproj.filters diff --git a/Intersection_Union/Sequential_List/SqList.h b/Exercise/Intersection_Union/Sequential_List/SqList.h similarity index 100% rename from Intersection_Union/Sequential_List/SqList.h rename to Exercise/Intersection_Union/Sequential_List/SqList.h diff --git a/Intersection_Union/Sequential_List/main.cpp b/Exercise/Intersection_Union/Sequential_List/main.cpp similarity index 100% rename from Intersection_Union/Sequential_List/main.cpp rename to Exercise/Intersection_Union/Sequential_List/main.cpp diff --git a/LinkListOperation/LinkList.h b/Exercise/LinkListOperation/LinkList.h similarity index 100% rename from LinkListOperation/LinkList.h rename to Exercise/LinkListOperation/LinkList.h diff --git a/LinkListOperation/LinkListOperation.sln b/Exercise/LinkListOperation/LinkListOperation.sln similarity index 100% rename from LinkListOperation/LinkListOperation.sln rename to Exercise/LinkListOperation/LinkListOperation.sln diff --git a/LinkListOperation/LinkListOperation.vcxproj b/Exercise/LinkListOperation/LinkListOperation.vcxproj similarity index 100% rename from LinkListOperation/LinkListOperation.vcxproj rename to Exercise/LinkListOperation/LinkListOperation.vcxproj diff --git a/LinkListOperation/LinkListOperation.vcxproj.filters b/Exercise/LinkListOperation/LinkListOperation.vcxproj.filters similarity index 100% rename from LinkListOperation/LinkListOperation.vcxproj.filters rename to Exercise/LinkListOperation/LinkListOperation.vcxproj.filters diff --git a/LinkListOperation/main.cpp b/Exercise/LinkListOperation/main.cpp similarity index 100% rename from LinkListOperation/main.cpp rename to Exercise/LinkListOperation/main.cpp diff --git a/Experiment/ExpReportTemplate.dotx b/Experiment/ExpReportTemplate.dotx new file mode 100644 index 0000000..d92ed53 Binary files /dev/null and b/Experiment/ExpReportTemplate.dotx differ diff --git a/Experiment/Experiment1/Report.docx b/Experiment/Experiment1/Report.docx new file mode 100644 index 0000000..9db8ba5 Binary files /dev/null and b/Experiment/Experiment1/Report.docx differ diff --git a/Experiment/Experiment1/Task1/Intersection_Union.sln b/Experiment/Experiment1/Task1/Intersection_Union.sln new file mode 100644 index 0000000..af7a094 --- /dev/null +++ b/Experiment/Experiment1/Task1/Intersection_Union.sln @@ -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 diff --git a/Experiment/Experiment1/Task1/Linked_List/LinkList.h b/Experiment/Experiment1/Task1/Linked_List/LinkList.h new file mode 100644 index 0000000..7914d52 --- /dev/null +++ b/Experiment/Experiment1/Task1/Linked_List/LinkList.h @@ -0,0 +1,253 @@ + +template +struct LNode //链表结点 +{ + DT data; //数据域,存储数据元素值 + LNode* next; //指针域,指向下一个结点 +}; + +//算法2.1 +template +bool PriorElem_e(LNode
* 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 +bool InitList(LNode
*& L) +{ + L = new LNode
; // 1.创建头结点 + if (!L) exit(1); // 2.创建失败,退出 + L->next = NULL; // 3.创建成功 + return true; // 返回true +} + +//【算法2.15】 尾插法创建n的元素 +template +bool CreateList_1(LNode
*& L, int n) +{ + int i; + LNode
* p, * s; + p = L; //1.工作指针初始化,指向尾结点 + cout << "依次输入" << n << "个数据元素:" << endl; + for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点 + { + s = new LNode
; // 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 +bool CreateList_2(LNode
* (&L), int n) +{ + int i; + LNode
* s; + cout << "逆序输入" << n << "个数据元素:" << endl; + for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点 + { + s = new LNode
; // 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 +void DestroyList(LNode
* (&L)) // 释放链表所占空间 +{ + LNode
* p; + while (L) // 1. 表非空,从头结点开始,依次释放结点 + { + p = L; // 1.1 处理表头结点 + L = L->next; // 1.2 头指针后移 + delete p; // 1.3 释放表头结点所占内存 + } + L = NULL; // 2.头指针指向空 +} + +//【算法2.18】 获取第i个元素 +template +bool GetElem_i(LNode
* L, int i, DT& e) +{ + LNode
* 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 +int LocateElem_e(LNode
* L, DT e) +{ + + LNode
* 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 +bool InsertElem_i(LNode
*& L, int i, DT e) +{ + + int j = 0; + LNode
* 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
* s; + s = new LNode
; // 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 +bool DeleElem_i(LNode
* (&L), int i) +{ + + LNode
* 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 +bool PutElem_i(LNode
* (&L), int i, DT e) +{ + LNode
* 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 +void ClearList(LNode
* (&L)) +{ + + LNode
* p; + while (L->next) // 从首元结点开始,依次释放结点 + { + p = L->next; + L->next = p->next; + delete p; + } + cout << endl << "表已清空!" << endl; +} + + +//【算法2.23】 测表长 +template +int ListLength(LNode
* L) +{ // 1.初始化 + int len = 0; // 1.1 结点计数器赋初值0 + LNode
* p; // 1.2设置工作指针 + p = L; // 指向头结点 + while (p->next) // 2.数结点个数。有后继结点, + { + len++; p = p->next; // 结点数增1,指针后移 + } + return len; // 3.返回表长 +} + +// +template +bool ListEmpty(LNode
* L) //测表空 +{ + if (L->next == NULL) + return true; //空表,返回1 + else + return false; //不空,返回0 +} + + +//【算法2.24】 遍历表 +template +void DispList(LNode
* L) // 显示表内容 +{ + LNode
* p; // 1. 设置工作指针 + p = L; // 从首元结点开始遍历 + while (p->next) // 2.依次输出各结点值 + { + p = p->next; cout << p->data << "\t"; + + + } + cout << endl; +} diff --git a/Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj b/Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj new file mode 100644 index 0000000..26d5576 --- /dev/null +++ b/Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {b2eef80b-772e-44e2-856d-c8def3242ee3} + LinkedList + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj.filters b/Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj.filters new file mode 100644 index 0000000..08fad02 --- /dev/null +++ b/Experiment/Experiment1/Task1/Linked_List/Linked_List.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + \ No newline at end of file diff --git a/Experiment/Experiment1/Task1/Linked_List/main.cpp b/Experiment/Experiment1/Task1/Linked_List/main.cpp new file mode 100644 index 0000000..c089233 --- /dev/null +++ b/Experiment/Experiment1/Task1/Linked_List/main.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; +#include "LinkList.h" +template +bool CreateListNoRepeat(LNode
*& L, int n) +{ + int i; + LNode
* 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
; + if (!s) return false; + s->data = temp; + s->next = p->next; + p->next = s; + p = s; + } + } + return true; +} +template +void Union(LNode
*& A, LNode
*& 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 +void Intersection(LNode
*& A, LNode
*& 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* A; + LNode* 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; +} \ No newline at end of file diff --git a/Experiment/Experiment1/Task1/Sequential_List/Sequential_List.vcxproj b/Experiment/Experiment1/Task1/Sequential_List/Sequential_List.vcxproj new file mode 100644 index 0000000..f467086 --- /dev/null +++ b/Experiment/Experiment1/Task1/Sequential_List/Sequential_List.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {b4e4a6f7-1b97-421c-98bb-79482b2743fe} + SequentialList + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Experiment/Experiment1/Task1/Sequential_List/Sequential_List.vcxproj.filters b/Experiment/Experiment1/Task1/Sequential_List/Sequential_List.vcxproj.filters new file mode 100644 index 0000000..9cefb5b --- /dev/null +++ b/Experiment/Experiment1/Task1/Sequential_List/Sequential_List.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Experiment/Experiment1/Task1/Sequential_List/SqList.h b/Experiment/Experiment1/Task1/Sequential_List/SqList.h new file mode 100644 index 0000000..5dfd6e5 --- /dev/null +++ b/Experiment/Experiment1/Task1/Sequential_List/SqList.h @@ -0,0 +1,175 @@ +template +struct SqList // 顺序表 +{ + DT* elem; // 表首址 + int length; // 表长 + int size; // 表容量 +}; + +//算法2.1 +template +bool PriorElem_e(SqList
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 +bool InitList(SqList
& 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 +bool CreateList(SqList
& 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 +void DestroyList(SqList
& L) +{ + delete[] L.elem; // 1.释放表空间 + L.length = 0; // 2.属性赋值 + L.size = 0; +} + +//【算法2.5】 获取第i个元素值 +template +bool GetElem_i(SqList
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 +int LocateElem_e(SqList
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 +bool InsertElem_i(SqList
& 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 +bool DeleElem_i(SqList
& 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 +bool PutElem(SqList
& 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 +void ClearList(SqList
& L) +{ + L.length = 0; // 空表,表长为0 +} + + +// 测表长 +template +int ListLength(SqList
L) +{ + return L.length; +} + + +template +bool ListEmpty(SqList
L) // 测表空 +{ + if (L.length == 0) // 空表,返回true + return true; + else + return false; // 非空表,返回false +} + +template +bool ListFull(SqList
L) +{ + if (L.length == L.size) // 表满,返回true + return true; + else + return false; // 表不满,返回false +} + +//【算法2.10】 遍历输出 +template +void DispList(SqList
L) +{ + int i; + for (i = 1; i <= L.length; i++) // 依位序输出元素值 + { + cout << L.elem[i - 1] << "\t"; + + } + cout << endl; +} + + diff --git a/Experiment/Experiment1/Task1/Sequential_List/main.cpp b/Experiment/Experiment1/Task1/Sequential_List/main.cpp new file mode 100644 index 0000000..a1fbc2f --- /dev/null +++ b/Experiment/Experiment1/Task1/Sequential_List/main.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; +#include "SqList.h" +template +bool CreateListNoRepeat(SqList
& 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 +void Union(SqList
& A, const SqList
& 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 +void Intersection(SqList
& A, const SqList
& 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 A; + SqList 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; +} \ No newline at end of file diff --git a/Experiment/Experiment1/Task2/LinkList.h b/Experiment/Experiment1/Task2/LinkList.h new file mode 100644 index 0000000..7914d52 --- /dev/null +++ b/Experiment/Experiment1/Task2/LinkList.h @@ -0,0 +1,253 @@ + +template +struct LNode //链表结点 +{ + DT data; //数据域,存储数据元素值 + LNode* next; //指针域,指向下一个结点 +}; + +//算法2.1 +template +bool PriorElem_e(LNode
* 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 +bool InitList(LNode
*& L) +{ + L = new LNode
; // 1.创建头结点 + if (!L) exit(1); // 2.创建失败,退出 + L->next = NULL; // 3.创建成功 + return true; // 返回true +} + +//【算法2.15】 尾插法创建n的元素 +template +bool CreateList_1(LNode
*& L, int n) +{ + int i; + LNode
* p, * s; + p = L; //1.工作指针初始化,指向尾结点 + cout << "依次输入" << n << "个数据元素:" << endl; + for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点 + { + s = new LNode
; // 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 +bool CreateList_2(LNode
* (&L), int n) +{ + int i; + LNode
* s; + cout << "逆序输入" << n << "个数据元素:" << endl; + for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点 + { + s = new LNode
; // 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 +void DestroyList(LNode
* (&L)) // 释放链表所占空间 +{ + LNode
* p; + while (L) // 1. 表非空,从头结点开始,依次释放结点 + { + p = L; // 1.1 处理表头结点 + L = L->next; // 1.2 头指针后移 + delete p; // 1.3 释放表头结点所占内存 + } + L = NULL; // 2.头指针指向空 +} + +//【算法2.18】 获取第i个元素 +template +bool GetElem_i(LNode
* L, int i, DT& e) +{ + LNode
* 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 +int LocateElem_e(LNode
* L, DT e) +{ + + LNode
* 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 +bool InsertElem_i(LNode
*& L, int i, DT e) +{ + + int j = 0; + LNode
* 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
* s; + s = new LNode
; // 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 +bool DeleElem_i(LNode
* (&L), int i) +{ + + LNode
* 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 +bool PutElem_i(LNode
* (&L), int i, DT e) +{ + LNode
* 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 +void ClearList(LNode
* (&L)) +{ + + LNode
* p; + while (L->next) // 从首元结点开始,依次释放结点 + { + p = L->next; + L->next = p->next; + delete p; + } + cout << endl << "表已清空!" << endl; +} + + +//【算法2.23】 测表长 +template +int ListLength(LNode
* L) +{ // 1.初始化 + int len = 0; // 1.1 结点计数器赋初值0 + LNode
* p; // 1.2设置工作指针 + p = L; // 指向头结点 + while (p->next) // 2.数结点个数。有后继结点, + { + len++; p = p->next; // 结点数增1,指针后移 + } + return len; // 3.返回表长 +} + +// +template +bool ListEmpty(LNode
* L) //测表空 +{ + if (L->next == NULL) + return true; //空表,返回1 + else + return false; //不空,返回0 +} + + +//【算法2.24】 遍历表 +template +void DispList(LNode
* L) // 显示表内容 +{ + LNode
* p; // 1. 设置工作指针 + p = L; // 从首元结点开始遍历 + while (p->next) // 2.依次输出各结点值 + { + p = p->next; cout << p->data << "\t"; + + + } + cout << endl; +} diff --git a/Experiment/Experiment1/Task2/LinkListOperation.sln b/Experiment/Experiment1/Task2/LinkListOperation.sln new file mode 100644 index 0000000..33b9616 --- /dev/null +++ b/Experiment/Experiment1/Task2/LinkListOperation.sln @@ -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 diff --git a/Experiment/Experiment1/Task2/LinkListOperation.vcxproj b/Experiment/Experiment1/Task2/LinkListOperation.vcxproj new file mode 100644 index 0000000..b2749c2 --- /dev/null +++ b/Experiment/Experiment1/Task2/LinkListOperation.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {baa96efe-86cd-47ea-8d5b-caf1cc77bdc6} + LinkListOperation + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Experiment/Experiment1/Task2/LinkListOperation.vcxproj.filters b/Experiment/Experiment1/Task2/LinkListOperation.vcxproj.filters new file mode 100644 index 0000000..08fad02 --- /dev/null +++ b/Experiment/Experiment1/Task2/LinkListOperation.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + \ No newline at end of file diff --git a/Experiment/Experiment1/Task2/main.cpp b/Experiment/Experiment1/Task2/main.cpp new file mode 100644 index 0000000..c5f0249 --- /dev/null +++ b/Experiment/Experiment1/Task2/main.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; +#include "LinkList.h" +template +bool CreateOrderedList(LNode
*& L, int n) +{ + int i; + LNode
* p, * s; + p = L; + cout << "依次输入" << n << "个数据元素:" << endl; + for (i = 1; i <= n; i++) + { + s = new LNode
; + 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 +void MergeTwoOrderedLists(LNode
*& L1, LNode
*& L2) +{ + LNode
* 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
* 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() +{ + LNode* L; + InitList(L); + int length; + cout << "请输入有序表的长度: "; + cin >> length; + CreateOrderedList(L, length); + DispList(L); + LNode* L2; + InitList(L2); + int length2; + cout << "请输入另一个有序表的长度: "; + cin >> length2; + CreateOrderedList(L2, length2); + DispList(L2); + MergeTwoOrderedLists(L, L2); + DispList(L); + return 0; +} \ No newline at end of file diff --git a/Experiment/Experiment2/Report.docx b/Experiment/Experiment2/Report.docx new file mode 100644 index 0000000..1d62439 Binary files /dev/null and b/Experiment/Experiment2/Report.docx differ diff --git a/Experiment/Experiment2/Task1/BracketsMatch.cpp b/Experiment/Experiment2/Task1/BracketsMatch.cpp new file mode 100644 index 0000000..e9e7f3f --- /dev/null +++ b/Experiment/Experiment2/Task1/BracketsMatch.cpp @@ -0,0 +1,54 @@ +#include +#include +using namespace std; +#include "LinkStack.h" + +bool match(string exp) +{ + SNode* S; + InitStack(S); + int flag = 1, i = 0; + char ch, e, x; + 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; + } + ch = exp[i++]; + } + if (StackEmpty(S) && flag) return true; + else return false; +} + +int main() +{ + int flag; + string exp; + cout << "请输入待匹配的表达式,以“#”结束:" << endl; + cin >> exp; + flag = match(exp); + if (flag) cout << "括号匹配成功!" << endl; + else cout << "括号匹配失败!" << endl; + return 0; +} + diff --git a/Experiment/Experiment2/Task1/BracketsMatch.sln b/Experiment/Experiment2/Task1/BracketsMatch.sln new file mode 100644 index 0000000..ba87d87 --- /dev/null +++ b/Experiment/Experiment2/Task1/BracketsMatch.sln @@ -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}") = "BracketsMatch", "BracketsMatch.vcxproj", "{BC507152-80AB-4A4F-9006-339728D357CA}" +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 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {12C9DCB1-398A-4F5B-9896-2020AF51DA75} + EndGlobalSection +EndGlobal diff --git a/Experiment/Experiment2/Task1/BracketsMatch.vcxproj b/Experiment/Experiment2/Task1/BracketsMatch.vcxproj new file mode 100644 index 0000000..eac0c8a --- /dev/null +++ b/Experiment/Experiment2/Task1/BracketsMatch.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {bc507152-80ab-4a4f-9006-339728d357ca} + BracketsMatch + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Experiment/Experiment2/Task1/BracketsMatch.vcxproj.filters b/Experiment/Experiment2/Task1/BracketsMatch.vcxproj.filters new file mode 100644 index 0000000..0bab683 --- /dev/null +++ b/Experiment/Experiment2/Task1/BracketsMatch.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + \ No newline at end of file diff --git a/Experiment/Experiment2/Task1/LinkStack.h b/Experiment/Experiment2/Task1/LinkStack.h new file mode 100644 index 0000000..08cf5af --- /dev/null +++ b/Experiment/Experiment2/Task1/LinkStack.h @@ -0,0 +1,92 @@ + +template +struct SNode //结点 +{ + DT data; //数据域,存储数据元素值 + SNode* next;//指针域,指向下一个结点 +}; + + +//【算法3.6】 +template +void InitStack(SNode
*& S)//创建空链栈 +{ + S = NULL; +} + +//【算法3.7】 +template +void DestroyStack(SNode
* (&S))//释放链栈 +{ + SNode
* p; + while (S)//从头结点开始,依次释放结点 + { + p = S; + S = S->next; + delete p; + } + //L=NULL;//头结点指向空 +} + +//【算法3.8】 +template +bool Push(SNode
*& S, DT e) +{ + SNode
* p; + p = new SNode
; + if (!p) return false; //创建失败,结束运行 + p->data = e; // 新结点赋值 + p->next = S; //结点S链接到p结点之后 + S = p; + return true; // 插入成功,返回true +} + +//【算法3.9】 +template +bool Pop(SNode
*& S, DT& e) +{ + SNode
* p; + if (S == NULL) return false; + p = S; + e = p->data; + S = S->next; + delete p; + return true; // 删除成功,返回true +} + + +//【算法3.10】 +template +bool GetTop(SNode
* S, DT& e) +{ + SNode
* p; + if (S == NULL) return false; + p = S; + e = p->data; + return true; // 删除成功,返回true +} + +//测栈空 +template +bool StackEmpty(SNode
* S) +{ + if (S == NULL) + return true; + else + return false; +} + +//显示栈内容 +template +void DispStack(SNode
* S) +{ + SNode
* p; + p = S; + while (p) + { + cout << p->data << "\t"; + p = p->next; + } + cout << endl; +} + diff --git a/Experiment/Experiment2/Task2/Ferry.cpp b/Experiment/Experiment2/Task2/Ferry.cpp new file mode 100644 index 0000000..e22fc4d --- /dev/null +++ b/Experiment/Experiment2/Task2/Ferry.cpp @@ -0,0 +1,77 @@ +#include +#include "LinkQueue.h" +using namespace std; + +struct Car +{ + int no; //车号 + char type; //c:客车,t:货车 +}; + +//求队列长度 +template +int QueueLength(LinkQueue
Q) +{ + int n = 0; + QNode
* p = Q.front->next; + while (p) + { + p = p->next; + n++; + } + return n; +} + +//车辆入队 +void Ferry(LinkQueue& Qc, LinkQueue& Qt) +{ + int i = 0; + Car e; + while (QueueLength(Qc) + QueueLength(Qt) > 0) //队列非空 + { + if (QueueLength(Qc) > 0) //客车队列非空 + { + while ((i < 4) && (QueueLength(Qc) > 0)) //客车数小于4且客车队列非空 + { + DeQueue(Qc, e); //客车出队 + cout << "车号:" << e.no << "\t类型:" << e.type << endl; + i++; + } + i = 0; + } + if (QueueLength(Qt) > 0) //货车队列非空 + { + DeQueue(Qt, e); //货车出队 + cout << "车号:" << e.no << "\t类型:" << e.type << endl; + } + } + cout << "所有车辆已过江!" << endl; +} +int main() +{ + LinkQueue Qc, Qt; + Car e; + int i, n; + InitQueue(Qc); + InitQueue(Qt); + cout << "客车数:"; + cin >> n; + for (i = 1; i <= n; i++) //客车入队 + { + e.no = i; + e.type = 'c'; + EnQueue(Qc, e); + } + cout << "货车数:"; + cin >> n; + for (i = 1; i <= n; i++) //货车入队 + { + e.no = i; + e.type = 't'; + EnQueue(Qt, e); + } + Ferry(Qc, Qt); //车辆过江 + DestroyQueue(Qc); + DestroyQueue(Qt); + return 0; +} \ No newline at end of file diff --git a/Experiment/Experiment2/Task2/Ferry.sln b/Experiment/Experiment2/Task2/Ferry.sln new file mode 100644 index 0000000..34aeaac --- /dev/null +++ b/Experiment/Experiment2/Task2/Ferry.sln @@ -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}") = "Ferry", "Ferry.vcxproj", "{DFE075CA-094D-4856-92A8-0461FC180B13}" +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 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x64.ActiveCfg = Debug|x64 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x64.Build.0 = Debug|x64 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x86.ActiveCfg = Debug|Win32 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Debug|x86.Build.0 = Debug|Win32 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x64.ActiveCfg = Release|x64 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x64.Build.0 = Release|x64 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x86.ActiveCfg = Release|Win32 + {DFE075CA-094D-4856-92A8-0461FC180B13}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B24F9342-2DAA-4663-8993-00F01D577229} + EndGlobalSection +EndGlobal diff --git a/Experiment/Experiment2/Task2/Ferry.vcxproj b/Experiment/Experiment2/Task2/Ferry.vcxproj new file mode 100644 index 0000000..7c6c8b2 --- /dev/null +++ b/Experiment/Experiment2/Task2/Ferry.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {dfe075ca-094d-4856-92a8-0461fc180b13} + Ferry + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Experiment/Experiment2/Task2/Ferry.vcxproj.filters b/Experiment/Experiment2/Task2/Ferry.vcxproj.filters new file mode 100644 index 0000000..d68c47c --- /dev/null +++ b/Experiment/Experiment2/Task2/Ferry.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + \ No newline at end of file diff --git a/Experiment/Experiment2/Task2/LinkQueue.h b/Experiment/Experiment2/Task2/LinkQueue.h new file mode 100644 index 0000000..f3d7a1f --- /dev/null +++ b/Experiment/Experiment2/Task2/LinkQueue.h @@ -0,0 +1,129 @@ +#include +using namespace std; + +template +struct QNode //结点 +{ + DT data; //数据域,存储数据元素值 + QNode* next;//指针域,指向下一个结点 +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//【算法3.19】 +template +void InitQueue(LinkQueue
& Q)//创建空队列 +{ + Q.front = new QNode
; //创建头结点 + if (!Q.front) exit(1); //创建失败,结束运行 + Q.front->next = NULL; + Q.rear = Q.front; +} + +//【算法3.20】 +template +void DestroyQueue(LinkQueue
& Q)//释放链队 +{ + QNode
* p; + while (Q.front)//从头结点开始,依次释放结点 + { + p = Q.front; + Q.front = Q.front->next; + delete p; + } + Q.rear = Q.front = NULL; +} + +template +void ClearQueue(LinkQueue
& Q) // 清空链队 +{ + QNode
* 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 +bool EnQueue(LinkQueue
& Q, DT e) +{ + QNode
* p; + p = new QNode
; // 创建新结点 + if (!p) return false; // 创建失败,结束运行 + p->data = e; // 新结点赋值 + p->next = NULL; // 链在队尾 + Q.rear->next = p; + Q.rear = p; + return true; // 入队成功,返回true +} + +//【算法3.22】 出队 +template +bool DeQueue(LinkQueue
& Q, DT& e) +{ + QNode
* 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 +bool GetHead(LinkQueue
Q, DT& e) +{ + if (Q.front == Q.rear) return false; // 队空,返回false + e = Q.front->next->data; + return true; // 删除成功,返回true +} + +//取队尾元素 +template +bool GetTail(LinkQueue
Q, DT& e) +{ + if (Q.front == Q.rear) // 队空 + return false; // 返回false + e = Q.rear->data; // 获取队尾元素 + return true; // 返回true +} + +//测队空 +template +bool QueueEmpty(LinkQueue
Q) +{ + if (Q.front == Q.rear) // 队空 + return true; //返回true + else //非空 + return false; //返回false +} + +//显示队列内容 +template +void DispQueue(LinkQueue
Q) +{ + QNode
* p; + p = Q.front->next; + while (p) + { + cout << p->data << "\t"; + p = p->next; + } + cout << endl; +} + diff --git a/Experiment/Experiment3/LeafNum.cpp b/Experiment/Experiment3/LeafNum.cpp new file mode 100644 index 0000000..005270b --- /dev/null +++ b/Experiment/Experiment3/LeafNum.cpp @@ -0,0 +1,53 @@ +#include +#include "BiTree.h" +using namespace std; + +//递归方法求解二叉树叶节点个数 +template +int LeafNum_1(BTNode
* bt) { + if (!bt) { + return 0; + } + else if (bt->lchild == NULL && bt->rchild == NULL) { + return 1; + } + else { + return LeafNum_1(bt->lchild) + LeafNum_1(bt->rchild); + } +} + +//非递归方法求解二叉树叶节点个数 +template +int LeafNum_2(BTNode
* bt) { + if (!bt) + return 0; + SqStack
S; + InitStack(S, 20); + BTNode
* p = bt; + int count = 0; + while (p != NULL || !StackEmpty(S)) + { + while (p != NULL) + { + if (p->lchild == NULL && p->rchild == NULL) + count++; + Push(S, p); + p = p->lchild; + } + if (!StackEmpty(S)) + { + Pop(S, p); + p = p->rchild; + } + } + return count; +} + +int main() { + BTNode* bt; + CreateBiTree(bt); + cout << "递归方法求得二叉树叶节点个数为:" << LeafNum_1(bt) << endl; + cout << "非递归方法求得二叉树叶节点个数为:" << LeafNum_2(bt) << endl; + DestroyBiTree(bt); + return 0; +} \ No newline at end of file diff --git a/Experiment/Experiment3/LeafNum.sln b/Experiment/Experiment3/LeafNum.sln new file mode 100644 index 0000000..5c5a71a --- /dev/null +++ b/Experiment/Experiment3/LeafNum.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34928.147 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LeafNum", "LeafNum.vcxproj", "{A1435839-D838-4C8A-8F43-03AE8436D845}" +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 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x64.ActiveCfg = Debug|x64 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x64.Build.0 = Debug|x64 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x86.ActiveCfg = Debug|Win32 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Debug|x86.Build.0 = Debug|Win32 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x64.ActiveCfg = Release|x64 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x64.Build.0 = Release|x64 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x86.ActiveCfg = Release|Win32 + {A1435839-D838-4C8A-8F43-03AE8436D845}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C6AC6B0D-CC6B-4023-9F49-D0756F2B0732} + EndGlobalSection +EndGlobal diff --git a/Experiment/Experiment3/LeafNum.vcxproj b/Experiment/Experiment3/LeafNum.vcxproj new file mode 100644 index 0000000..6aeed9a --- /dev/null +++ b/Experiment/Experiment3/LeafNum.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {a1435839-d838-4c8a-8f43-03ae8436d845} + LeafNum + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Experiment/Experiment3/LeafNum.vcxproj.filters b/Experiment/Experiment3/LeafNum.vcxproj.filters new file mode 100644 index 0000000..0eb6c88 --- /dev/null +++ b/Experiment/Experiment3/LeafNum.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + \ No newline at end of file diff --git a/Experiment/Experiment3/Report.docx b/Experiment/Experiment3/Report.docx new file mode 100644 index 0000000..727e2b8 Binary files /dev/null and b/Experiment/Experiment3/Report.docx differ diff --git a/Experiment/Experiment3/bitree.h b/Experiment/Experiment3/bitree.h new file mode 100644 index 0000000..733bb01 --- /dev/null +++ b/Experiment/Experiment3/bitree.h @@ -0,0 +1,424 @@ +#include +using namespace std; + +template +struct BTNode +{ + DT data; //数据域 + BTNode* lchild; //指向左子树的指针 + BTNode* rchild; //指向右子树的指针 +}; + +//队列工具 +template +struct SqQueue // 顺序队类 +{ + BTNode
** base; // 队列首址 + int front; // 队头指针 + int rear; // 队尾指针 + int queuesize; // 队容量 +}; + +//栈 +template +struct SqStack // 顺序栈 +{ + BTNode
** base; // 栈首址 + int top; // 栈顶指针 + int stacksize; // 栈容量 +}; + +//算法5.1 先序遍历递归算法 +template +void PreOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + cout << bt->data << ' '; //输出结点上的数据 + PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树 + PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树 + } + return; +} + +//算法5.2 中序遍历递归算法 +template +void InOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树 + cout << bt->data << ' '; //输出结点上的数据 + InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树 + } + return; +} + +//算法5.3 后序遍历递归算法 +template +void PostOrDerBiTree(BTNode
* bt) +{ + if (bt != NULL) + { + PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树 + PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树 + cout << bt->data << ' '; //输出结点上的数据 + } + return; +} + + +//算法5.4 层序遍历算法 +template +void LevelBiTree(BTNode
* bt) +{ + SqQueue
Q; // 创建一个队 + int m = 20; + InitQueue(Q, m); + BTNode
* 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 +void PreOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建栈 + int m = 20; + InitStack(S, m); + BTNode
* 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 +void InOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建一个栈 + int m = 20; + InitStack(S, m); + BTNode
* 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 +void PostOrderBiTree_N(BTNode
* bt) +{ + SqStack
S; // 创建一个栈 + int m = 20; + InitStack(S, m); + BTNode
* p; + BTNode
* 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 +void CreateBiTree(BTNode
*& bt) +{ + char ch; + cin >> ch; // 输入根结点的数据 + if (ch == '#') // # 表示指针为空,说明树为空 + bt = NULL; + else + { + bt = new BTNode
; // 申请内存 + if (!bt) + { + cout << "申请内存失败!" << endl; + exit(-1); // 申请内存失败退出 + } + bt->data = ch; + CreateBiTree(bt->lchild); // 创建根结点左子树 + CreateBiTree(bt->rchild); // 创建根结点右子树 + } + return; +} + + +//算法5.9 销毁二叉树 +template +void DestroyBiTree(BTNode
*& bt) +{ + if (bt) // 树非空 + { + DestroyBiTree(bt->lchild); // 销毁左子树 + DestroyBiTree(bt->rchild); // 销毁右子树 + delete bt; + } +} + + +//算法5.10 结点查找 + +template +BTNode
* Search(BTNode
* bt, DT e) // 查找值为e的元素 +{ + BTNode
* 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 +int Depth(BTNode
* 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 +int NodeCount(BTNode
* bt) +{ + if (bt == NULL) // 空树,结点数为0 + return 0; + else // 非空树,结点数为左、右子树结点数的和加1 + return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1; +} + +template +void DispBiTree(BTNode
* 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 +int leftCount(BTNode
* bt) +{ + if (bt->lchild == NULL && bt->rchild == NULL) // 空树,结点数为0 + return 0; + else // 非空树,结点数为左、右子树结点数的和加1 + return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1; +} + + +//【算法3.14】 初始化队列 +template +void InitQueue(SqQueue
& Q, int m) +{ + Q.base = new BTNode
*[m]; // 申请队列空间 + if (Q.base == NULL) // 申请空间失败 + { + cout << "未创建成功!"; + exit(1); // 退出 + } + Q.front = Q.rear = 0; // 设置队列属性 + Q.queuesize = m; +} + + +//算法3.15】 销毁列列 +template +void DestroyQueue(SqQueue
& Q) +{ + delete[] Q.base; // 释放队列空间 + Q.front = Q.rear = 0; // 设置队列属性 + Q.queuesize = 0; +} + + +//【算法3.16】 入队 +template +bool EnQueue(SqQueue
& Q, BTNode
* 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 +bool DeQueue(SqQueue
& Q, BTNode
*& e) +{ + if (Q.front == Q.rear) // 队空 + return false; + e = Q.base[Q.front]; + Q.front = (Q.front + 1) % Q.queuesize; + return true; // 删除成功,返回true +} + +// 测队空 +template +bool QueueEmpty(SqQueue
Q) +{ + if (Q.front == Q.rear) // 队空 + return true; // 返回true + else // 队不空 + return false; // 返回false +} + +//【算法3.1】 初始化栈 +template +void InitStack(SqStack
& S, int m) +{ + S.base = new BTNode
*[m]; // 申请栈空间 + if (S.base == NULL) + { + cout << "未创建成功!"; + exit(1); + } + S.top = -1; // 空栈 + S.stacksize = m; // 栈容量为m +} + + +//算法3.2】 销毁栈 +template +void DestroyStack(SqStack
& S)//析构函数 +{ + delete[] S.base; //释放栈空间 + S.top = -1; + S.stacksize = 0; +} + + +//【算法3.3】 +template +bool Push(SqStack
& S, BTNode
* e) +{ + if (S.top == S.stacksize - 1) // 栈满,不能插入 + return false; + S.top++; + S.base[S.top] = e; + return true; // 插入成功,返回true +} + +//【算法3.4】 出栈 +template +bool Pop(SqStack
& S, BTNode
*& e) +{ + if (S.top == -1) //栈空 + return false; + e = S.base[S.top]; + S.top--; + return true; // 出栈成功,返回true +} + +template // 获取栈元素 +bool GetTop(SqStack
& S, BTNode
*& e) +{ + if (S.top == -1) // 栈空,返回false + return false; + e = S.base[S.top]; // 取栈元素 + return true; // 返回true +} + + +// 测栈空 +template +bool StackEmpty(SqStack
S) +{ + if (S.top == -1) // 栈空,返回true + return true; + else // 栈非空,返回false + return false; +} \ No newline at end of file diff --git a/Experiment/Experiment6/InsertSort.cpp b/Experiment/Experiment6/InsertSort.cpp new file mode 100644 index 0000000..ad104c8 --- /dev/null +++ b/Experiment/Experiment6/InsertSort.cpp @@ -0,0 +1,62 @@ +#include +#include "LinkList.h" +using namespace std; + +template +void InsertSort_LL(LNode
* L) { + if (L == NULL || L->next == NULL) { + return; // 空表或只有一个结点,不需要排序 + } + + LNode
* r = L->next; // 已排序表表尾 + LNode
* q = r->next; // q为当前处理项,r的后继 + + while (q) { + LNode
* p1 = L; + LNode
* p2 = p1->next; + + while (q->data >= p2->data && p2 != q) { // 当前结点数据大于等于p2,插入点后移 + p1 = p2; + p2 = p2->next; + } + + if (p2 == q) { // 当前项无须移动 + r = q; // 有序表表尾顺移 + } + else { // 插入p2前面 + r->next = q->next; // 摘除q结点 + q->next = p1->next; // 在p1后插入结点q + p1->next = q; + } + + q = r->next; // 下一个需处理的项 + } +} + + +int main() { + LNode* L; + int n; + + // 创建单链表 + cout << "请输入单链表的长度:"; + cin >> n; + InitList(L); + CreateList_1(L, n); + + // 显示初始单链表 + cout << "初始单链表:"; + DispList(L); + + // 插入排序 + InsertSort_LL(L); + + // 显示排序后的单链表 + cout << "排序后的单链表:"; + DispList(L); + + // 销毁单链表 + DestroyList(L); + + return 0; +} \ No newline at end of file diff --git a/Experiment/Experiment6/InsertSort.sln b/Experiment/Experiment6/InsertSort.sln new file mode 100644 index 0000000..4e96098 --- /dev/null +++ b/Experiment/Experiment6/InsertSort.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34916.146 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "InsertSort", "InsertSort.vcxproj", "{3CA81F66-07E1-40A0-9984-17556857602C}" +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 + {3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x64.ActiveCfg = Debug|x64 + {3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x64.Build.0 = Debug|x64 + {3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x86.ActiveCfg = Debug|Win32 + {3CA81F66-07E1-40A0-9984-17556857602C}.Debug|x86.Build.0 = Debug|Win32 + {3CA81F66-07E1-40A0-9984-17556857602C}.Release|x64.ActiveCfg = Release|x64 + {3CA81F66-07E1-40A0-9984-17556857602C}.Release|x64.Build.0 = Release|x64 + {3CA81F66-07E1-40A0-9984-17556857602C}.Release|x86.ActiveCfg = Release|Win32 + {3CA81F66-07E1-40A0-9984-17556857602C}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {638C3207-9916-43D8-88EC-BC64BB58E25C} + EndGlobalSection +EndGlobal diff --git a/Experiment/Experiment6/InsertSort.vcxproj b/Experiment/Experiment6/InsertSort.vcxproj new file mode 100644 index 0000000..cdc7b4b --- /dev/null +++ b/Experiment/Experiment6/InsertSort.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {3ca81f66-07e1-40a0-9984-17556857602c} + InsertSort + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Experiment/Experiment6/InsertSort.vcxproj.filters b/Experiment/Experiment6/InsertSort.vcxproj.filters new file mode 100644 index 0000000..a4ce222 --- /dev/null +++ b/Experiment/Experiment6/InsertSort.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Experiment/Experiment6/LinkList.h b/Experiment/Experiment6/LinkList.h new file mode 100644 index 0000000..ef08ad5 --- /dev/null +++ b/Experiment/Experiment6/LinkList.h @@ -0,0 +1,256 @@ +#include +using namespace std; + +template +struct LNode //链表结点 +{ + DT data; //数据域,存储数据元素值 + LNode* next; //指针域,指向下一个结点 +}; + +//算法2.1 +template +bool PriorElem_e(LNode
* 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 +bool InitList(LNode
*& L) +{ + L = new LNode
; // 1.创建头结点 + if (!L) exit(1); // 2.创建失败,退出 + L->next = NULL; // 3.创建成功 + return true; // 返回true +} + +//【算法2.15】 尾插法创建n的元素 +template +bool CreateList_1(LNode
*& L, int n) +{ + int i; + LNode
* p, * s; + p = L; //1.工作指针初始化,指向尾结点 + cout << "依次输入" << n << "个数据元素:" << endl; + for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点 + { + s = new LNode
; // 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 +bool CreateList_2(LNode
* (&L), int n) +{ + int i; + LNode
* s; + cout << "逆序输入" << n << "个数据元素:" << endl; + for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点 + { + s = new LNode
; // 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 +void DestroyList(LNode
* (&L)) // 释放链表所占空间 +{ + LNode
* p; + while (L) // 1. 表非空,从头结点开始,依次释放结点 + { + p = L; // 1.1 处理表头结点 + L = L->next; // 1.2 头指针后移 + delete p; // 1.3 释放表头结点所占内存 + } + L = NULL; // 2.头指针指向空 +} + +//【算法2.18】 获取第i个元素 +template +bool GetElem_i(LNode
* L, int i, DT& e) +{ + LNode
* 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 +int LocateElem_e(LNode
* L, DT e) +{ + + LNode
* 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 +bool InsertElem_i(LNode
*& L, int i, DT e) +{ + + int j = 0; + LNode
* 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
* s; + s = new LNode
; // 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 +bool DeleElem_i(LNode
* (&L), int i) +{ + + LNode
* 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 +bool PutElem_i(LNode
* (&L), int i, DT e) +{ + LNode
* 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 +void ClearList(LNode
* (&L)) +{ + + LNode
* p; + while (L->next) // 从首元结点开始,依次释放结点 + { + p = L->next; + L->next = p->next; + delete p; + } + cout << endl << "表已清空!" << endl; +} + + +//【算法2.23】 测表长 +template +int ListLength(LNode
* L) +{ // 1.初始化 + int len = 0; // 1.1 结点计数器赋初值0 + LNode
* p; // 1.2设置工作指针 + p = L; // 指向头结点 + while (p->next) // 2.数结点个数。有后继结点, + { + len++; p = p->next; // 结点数增1,指针后移 + } + return len; // 3.返回表长 +} + +// +template +bool ListEmpty(LNode
* L) //测表空 +{ + if (L->next == NULL) + return true; //空表,返回1 + else + return false; //不空,返回0 +} + + +//【算法2.24】 遍历表 +template +void DispList(LNode
* L) // 显示表内容 +{ + LNode
* p; // 1. 设置工作指针 + p = L; // 从首元结点开始遍历 + while (p->next) // 2.依次输出各结点值 + { + p = p->next; cout << p->data << "\t"; + + + } + cout << endl; +} +#pragma once diff --git a/Experiment/Experiment6/Report.docx b/Experiment/Experiment6/Report.docx new file mode 100644 index 0000000..34baa08 Binary files /dev/null and b/Experiment/Experiment6/Report.docx differ diff --git a/OrigFiles/1-绪/app1-4.cpp b/OrigFiles/1-绪/app1-4.cpp index e3fd65c..64e694a 100644 --- a/OrigFiles/1-绪/app1-4.cpp +++ b/OrigFiles/1-绪/app1-4.cpp @@ -1,19 +1,32 @@ -#include +#include//cout,cin using namespace std; -void main() +struct stu +{ int xh; // ѧ + float height; // +}; + +int MaxH; // + +//㷨1.1 +int maxHeight(stu student[], int n) { - 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<student[MaxH-1].height) // 2.1 + MaxH=i; // 2.2Ϊ + } + return student[MaxH-1].xh; // 3.ѧѧѧ +} + +int main() +{ + cout<<"ȡ"< -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< + + + + + diff --git a/OrigFiles/2-1/2-1.layout b/OrigFiles/2-1/2-1.layout new file mode 100644 index 0000000..7e6a1d0 --- /dev/null +++ b/OrigFiles/2-1/2-1.layout @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/OrigFiles/2-线性表/2-2-LinkList-单链表/LinkList.h b/OrigFiles/2-1/LinkList.h similarity index 87% rename from OrigFiles/2-线性表/2-2-LinkList-单链表/LinkList.h rename to OrigFiles/2-1/LinkList.h index d311770..9451b06 100644 --- a/OrigFiles/2-线性表/2-2-LinkList-单链表/LinkList.h +++ b/OrigFiles/2-1/LinkList.h @@ -6,20 +6,6 @@ struct LNode // LNode *next; //ָָһ }; -//㷨2.1 -template -bool PriorElem_e(LNode
*L, DT e, DT &pre_e) // ֵΪeԪǰ -{ - int k; - k=LocateElem_e(L,e); // 1.ȡeλk - if(k>1) // 2.λk1 - { - GetElem_i(L,k-1,pre_e); // k-1ԪΪeǰ - return true; - } - else // 3.Ԫeǰ - return false; // false -} //㷨2.14 յ template @@ -35,11 +21,10 @@ bool InitList(LNode
*&L) template bool CreateList_1(LNode
*&L,int n) { - int i; LNode
*p,*s; p=L; //1.ָʼָβ cout<<""<; // 2.1 ½һs if (!s) // 2.2 ʧܣfalse @@ -56,10 +41,9 @@ bool CreateList_1(LNode
*&L,int n) template bool CreateList_2(LNode
*(&L),int n) { - int i; LNode
*s; cout<<""<; // 1.1 ½һ s if (!s) // 1.2 ʧܣfalse @@ -90,7 +74,7 @@ template bool GetElem_i(LNode
*L,int i, DT &e) { LNode
*p; // 1.ʼ - p=L->next; // 1.1 ùָ룬׽㿪ʼ + p=L->next; // 1.1 ùָ룬׽㿪ʼ int j=1; // 1.2 ʼ while(p&&j *L, DT e) //㷨2.20 iԪ template -bool InsertElem_i(LNode
*&L,int i, DT e) +bool InsertElem_i(LNode
*(&L),int i, DT e) { int j=0; @@ -134,7 +118,7 @@ bool InsertElem_i(LNode
*&L,int i, DT e) p=L; // ָʼ while(p && jnext; + p=p->next;// j++; } if(!p||j>i-1) // 3.ж϶λǷɹ @@ -161,7 +145,7 @@ bool DeleElem_i(LNode
*(&L),int i) int j=0; //ʼ while(p->next && jnext; + p=p->next; // j++; } if(!p->next||j>i-1) //3.ɾλòɾ @@ -242,11 +226,11 @@ template void DispList(LNode
*L) // ʾ { LNode
*p; // 1. ùָ - p=L; // Ԫ㿪ʼ - while(p->next) // 2.ֵ + p=L->next; // Ԫ㿪ʼ + while(p) // 2.ֵ { - p=p->next;cout<data<<"\t"; - + cout<data<<"\t"; + p=p->next; } cout< //cout,cin +using namespace std; +#include "LinkList.h" + + +void dispmenu() +{ // ʾ˵ + cout< * L; + system("cls"); // + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~12):"; + cin>>choice; + switch(choice) + { + case 1: // ʼ + InitList(L); + cout<>n; + cout<>n; + cout<>i; + cout<>e; + i=LocateElem_e(L, e); + if(i) + cout<>i; + cout<<"Ԫֵ"<>e; + if(InsertElem_i(L,i,e)) + { + cout<>i; + if(DeleElem_i(L,i)) + { + cout<>i; + cout<<"Ԫֵ"<>e; + if(PutElem_i(L,i,e)) + { + cout< *&L) -{ - LNode *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< * L; system("cls"); // @@ -52,7 +35,7 @@ int main() do { dispmenu(); // ʾ˵ - cout<<"Enter choice(1~120 ˳):"; + cout<<"Enter choice(1~12):"; cin>>choice; switch(choice) { @@ -153,33 +136,17 @@ int main() case 12: //ʾ DispList(L); cout<>e; - if(PriorElem_e(L,e,pre_e)) - cout< +struct LNode // +{ + DT data; //򣬴洢Ԫֵ + LNode *next; //ָָһ +}; + + +//㷨2.14 յ +template +bool InitList(LNode
*&L) +{ + L=new LNode
; // 1.ͷ + if(!L) exit(1); // 2.ʧܣ˳ + L->next=NULL; // 3.ɹ + return true; // true +} + +//㷨2.15 β巨nԪ +template +bool CreateList_1(LNode
*&L,int n) +{ + LNode
*p,*s; + p=L; //1.ָʼָβ + cout<<""<; // 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 +bool CreateList_2(LNode
*(&L),int n) +{ + LNode
*s; + cout<<""<; // 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 +void DestroyList(LNode
*(&L)) // ͷռռ +{ + LNode
*p; + while(L) // 1. ǿ,ͷ㿪ʼͷŽ + { + p=L; // 1.1 ͷ + L=L->next; // 1.2 ͷָ + delete p; // 1.3 ͷűͷռڴ + } + L=NULL; // 2.ͷָָ +} + +//㷨2.18 ȡiԪ +template +bool GetElem_i(LNode
*L,int i, DT &e) +{ + LNode
*p; // 1.ʼ + p=L->next; // 1.1 ùָ룬׽㿪ʼ + int j=1; // 1.2 ʼ + while(p&&jnext;j++; + } + if(!p ||j>i ) // 3 δҵfalse + return false; + else // 4. ҵ + { + e=p->data; // ȡiԪֵ + return true; // true + } +} + +//㷨2.19 ֵΪeԪλ +template +int LocateElem_e(LNode
*L, DT e) +{ + + LNode
*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 +bool InsertElem_i(LNode
*(&L),int i, DT e) +{ + + int j=0; + LNode
*p; // 1.ʼ + p=L; // ָʼ + while(p && jnext;// + j++; + } + if(!p||j>i-1) // 3.ж϶λǷɹ + return false; // 3.1 λʧܣܲ + else // 3.2 λɹ + { + LNode
*s; + s=new LNode
; // 3.2.1½ + s->data=e; // 3.2.2½㸳ֵ + s->next=p->next; // 3.2.3Sӵp֮ + p->next=s; + return true; // 3.2.4 ɹtrue + } + +} + +//㷨2.21 ɾiԪ +template +bool DeleElem_i(LNode
*(&L),int i) +{ + + LNode
*p,*q; //1.ʼùָ + p=L; //Ҵͷ㿪ʼ + int j=0; //ʼ + while(p->next && jnext; // + 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 +bool PutElem_i(LNode
*(&L),int i, DT e) +{ + LNode
*p; // 1.ʼ:ùָ + p=L->next; // ׽㿪ʼ + int j=1; // ʼ + while(p&&jnext;j++; + } + if(!p||j>i) // 3.Ԫز,false + return false; + else // 4.λɹ + { + p->data=e; // ޸Ԫֵ + return true; // true + } +} + + // ͷռռ +template +void ClearList(LNode
*(&L)) +{ + + LNode
*p; + while(L->next) // Ԫ㿪ʼͷŽ + { + p=L->next; + L->next=p->next; + delete p; + } + cout< +int ListLength(LNode
*L) +{ // 1.ʼ + int len=0; // 1.1 ֵ0 + LNode
*p; // 1.2ùָ + p=L; // ָͷ + while(p->next) // 2.к̽㣬 + { + len++;p=p->next; // 1ָ + } + return len; // 3.ر +} + +// +template +bool ListEmpty(LNode
*L) // +{ + if(L->next==NULL) + return true; //ձ1 + else + return false; //գ0 +} + + +//㷨2.24 +template +void DispList(LNode
*L) // ʾ +{ + LNode
*p; // 1. ùָ + p=L->next; // Ԫ㿪ʼ + while(p) // 2.ֵ + { + cout<data<<"\t"; + p=p->next; + + } + cout<//cout,cin using namespace std; +char pause; + struct PolyNode // ʽ { float coef; // ϵ @@ -17,16 +19,18 @@ void InitPoly(PolyNode *&L) bool CreatePoly(PolyNode *&L,int n) // β巨nʽ { - int i; PolyNode *p,*s; p=L; - for(i=1; i<=n; i++) // ʽϵָ + cout<<"ϵָ"<>s->coef>>s->exp; + cout<<""<>s->coef; + cout<<""<>s->exp; s->next=p->next; p->next=s; p=s; @@ -66,18 +70,18 @@ void PolyAdd(PolyNode *&LA,PolyNode *&LB) while (qa!=NULL && qb!=NULL ) // 2. { if (qa->exp < qb->exp ) // 2.1 LAС - { // paqa + {// pa=qa;qa=qa->next; } else if ( qa->exp > qb->exp ) //2.2 LA ݴ { - pb->next =qb->next; // qbӵpa֮ + pb->next =qb->next; qb->next=qa; pa->next=qb; pa=qb; qb=pb->next; } else // 2.3 LALBͬ - { + {// sum=qa->coef+qb->coef; // ϵ if (sum!=0 ) // 2.3.1 ϵͲΪ0 { @@ -86,7 +90,7 @@ void PolyAdd(PolyNode *&LA,PolyNode *&LB) pb->next=qb->next; delete qb; // 2.3.1.3ɾqb qb=pb->next; - } + } else // 2.3.2 ϵΪ0 { pa->next=qa->next; @@ -118,51 +122,14 @@ void DestroyPoly(PolyNode *&L) // L=NULL; // ͷָ } -void SortPoly(PolyNode *&L) // ʽ -{ - PolyNode *p1,*p2,*q,*r; // ò㷨 - p1=L;p2=p1->next; // p1p2ǰ - if(p2==NULL || p2->next==NULL) // ձֻ1ĶʽҪ - { - cout<<"Ҫ!"<next; // β - q=r->next; // qΪǰĺһ - while(q) // δ - { // Ԫ㿪ʼҲ - p1=L;p2=p1->next; - while(q->exp>p2->exp && p2!=q) // ǰݴ󣬲 - { - p1=p2;p2=p2->next; - } - if(p2==q) // ǰƶ - { - r=q; // β˳ - } - else // q뵽p2ǰ - { - r->next=q->next; // ժq - q->next=p1->next; // p1q - p1->next=q; - } - q=r->next; // һ账 - - } - return; -} - - - void dispmenu() {//ʾ˵ cout<>choice; switch(choice) { case 1: // ʽA InitPoly(LA); - cout<<"ʽ A "; + cout<<"ʽ A "<>m; CreatePoly(LA, m ); cout<<"Ķʽ A Ϊ"<>n; CreatePoly(LB, n ); cout<<"ĶʽBΪ"<>c; if(c=='A'||c=='a') DispPoly(LA); @@ -226,13 +183,13 @@ int main() case 5: //˳ DestroyPoly(LA); DestroyPoly(LB); - cout<<"bye-bye!"< +struct LNode // +{ + DT data; //򣬴洢Ԫֵ + LNode *next; //ָָһ +}; + + +//㷨2.14 յ +template +bool InitList(LNode
*&L) +{ + L=new LNode
; // 1.ͷ + if(!L) exit(1); // 2.ʧܣ˳ + L->next=NULL; // 3.ɹ + return true; // true +} + +//㷨2.15 β巨nԪ +template +bool CreateList_1(LNode
*&L,int n) +{ + LNode
*p,*s; + p=L; //1.ָʼָβ + cout<<""<; // 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 +bool CreateList_2(LNode
*(&L),int n) +{ + LNode
*s; + cout<<""<; // 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 +void DestroyList(LNode
*(&L)) // ͷռռ +{ + LNode
*p; + while(L) // 1. ǿ,ͷ㿪ʼͷŽ + { + p=L; // 1.1 ͷ + L=L->next; // 1.2 ͷָ + delete p; // 1.3 ͷűͷռڴ + } + L=NULL; // 2.ͷָָ +} + +//㷨2.18 ȡiԪ +template +bool GetElem_i(LNode
*L,int i, DT &e) +{ + LNode
*p; // 1.ʼ + p=L->next; // 1.1 ùָ룬׽㿪ʼ + int j=1; // 1.2 ʼ + while(p&&jnext;j++; + } + if(!p ||j>i ) // 3 δҵfalse + return false; + else // 4. ҵ + { + e=p->data; // ȡiԪֵ + return true; // true + } +} + +//㷨2.19 ֵΪeԪλ +template +int LocateElem_e(LNode
*L, DT e) +{ + + LNode
*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 +bool InsertElem_i(LNode
*(&L),int i, DT e) +{ + + int j=0; + LNode
*p; // 1.ʼ + p=L; // ָʼ + while(p && jnext;// + j++; + } + if(!p||j>i-1) // 3.ж϶λǷɹ + return false; // 3.1 λʧܣܲ + else // 3.2 λɹ + { + LNode
*s; + s=new LNode
; // 3.2.1½ + s->data=e; // 3.2.2½㸳ֵ + s->next=p->next; // 3.2.3Sӵp֮ + p->next=s; + return true; // 3.2.4 ɹtrue + } + +} + +//㷨2.21 ɾiԪ +template +bool DeleElem_i(LNode
*(&L),int i) +{ + + LNode
*p,*q; //1.ʼùָ + p=L; //Ҵͷ㿪ʼ + int j=0; //ʼ + while(p->next && jnext; // + 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 +bool PutElem_i(LNode
*(&L),int i, DT e) +{ + LNode
*p; // 1.ʼ:ùָ + p=L->next; // ׽㿪ʼ + int j=1; // ʼ + while(p&&jnext;j++; + } + if(!p||j>i) // 3.Ԫز,false + return false; + else // 4.λɹ + { + p->data=e; // ޸Ԫֵ + return true; // true + } +} + + // ͷռռ +template +void ClearList(LNode
*(&L)) +{ + + LNode
*p; + while(L->next) // Ԫ㿪ʼͷŽ + { + p=L->next; + L->next=p->next; + delete p; + } + cout< +int ListLength(LNode
*L) +{ // 1.ʼ + int len=0; // 1.1 ֵ0 + LNode
*p; // 1.2ùָ + p=L; // ָͷ + while(p->next) // 2.к̽㣬 + { + len++;p=p->next; // 1ָ + } + return len; // 3.ر +} + +// +template +bool ListEmpty(LNode
*L) // +{ + if(L->next==NULL) + return true; //ձ1 + else + return false; //գ0 +} + + +//㷨2.24 +template +void DispList(LNode
*L) // ʾ +{ + LNode
*p; // 1. ùָ + p=L->next; // Ԫ㿪ʼ + while(p) // 2.ֵ + { + cout<data<<"\t"; + p=p->next; + + } + cout< //cout,cin +using namespace std; +#include "LinkList.h" + + +//㷨2.25 +void ReverseLinkList(LNode *&L) +{ + LNode *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< * 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< &L, int m) //㷨2.3 template -bool CreateList(SqList
&L,int n) //Ϊn˳ -{ - int i; +bool CreateList(SqList
&L,int n) +{//Ϊn˳ if(n>L.size) { cout<<"Ԫظڱܴ"<>L.elem[i-1]; L.length=n; return true; @@ -61,8 +60,7 @@ bool GetElem_i(SqList
L,int i, DT &e)// template int LocateElem_e(SqList
L, DT e) // ֵ { - int i; - for(i=0; i L, DT e) // template bool InsertElem_i(SqList
&L,int i, DT e) // ڵiλòԪ { - int j; if(L.length>=L.size) //1.ܲ return false; if(i<1 || i>L.length+1) //2.λòܲ return false; - for (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++; @@ -88,12 +85,11 @@ bool InsertElem_i(SqList
&L,int i, DT e) // template bool DeleElem_i(SqList
&L,int i) // ɾiԪ { - int j; if(L.length==0) //1.գɾ return false; if(i<1 || i>L.length) //2.ɾλòܲ return false; - for (j=i; j L) template void DispList(SqList
L) // { - int i; - for(i=0;i//cout,cin +using namespace std; +#include "SqList.h" + +char pause; + +void dispmenu() { + // ʾ˵ + cout << endl; + cout << "1-ϲA=AB\n"; + cout << "2-˳\n"; + cout << "3-ʽ\n"; + cout << "4-˳\n"; +} + +//㷨2.11 +template + +void Union(SqList
&La, SqList
Lb) { // La=LaLa + DT e; + int k; + for (int i = 1; i <= Lb.length; i++) { // ɨLb + GetElem_i(Lb, i, e); // 1. ȡLbĵiԪ + if (!LocateElem_e(La, e)) { // 2.La޴Ԫ + k = La.length + 1; // Laıβ + InsertElem_i(La, k, e); + } + } +} + +//㷨2.12 +template + +void ReverseSqList(SqList
&L) { // ˳Ԫ + DT t; + //if(L.length==0 || L.length==1) // 1.ձֻһԪأ + // return; + for (int i = 0; i < L.length / 2; i++) { // 2.L.elem[i]<-->L.elem[L.length-i-1] + t = L.elem[i]; + L.elem[i] = L.elem[L.length - i - 1]; + L.elem[L.length - i - 1] = t; + } + return; +} + +//㷨2.13 ʽ lc=la+lb +void PolyAdd(SqList la, SqList lb, SqList &lc) { + int i = 0; // 1.ʼôʼλ + while (i < la.length && i < lb.length) { // 2.ʽδ + lc.elem[i] = la.elem[i] + lb.elem[i]; // ͬλϵϵ + i++; + + } + if (la.length > lb.length) { // 3.laδ꣬lbѴ + while (i < la.length) { // lcȡlaʣ + lc.elem[i] = la.elem[i]; + i++; + + } + } else { // 3.lbδ꣬laѴ + while (i < lb.length) { // lcȡlbʣ + lc.elem[i] = lb.elem[i]; + i++; + + } + } +} + +//ʾඥʽ +void DispPoly(float A[], int n) { + int i; + cout << A[0] << "+"; + for (i = 1; i < n - 1; i++) + cout << A[i] << "x^" << i << " + "; + cout << A[i] << "x^" << i; + cout << endl; +} + + + +// +int main() { + //int i; + int na, nb, nc; + SqList La; // A + SqList Lb; // B + SqList Lc; // ˳ + SqList fa; // ʽA + SqList fb; // ʽB + SqList fc; // ͶʽC + + system("cls"); // + + int choice; + do { + dispmenu(); // ʾ˵ + cout << "Enter choice(1~4):"; + cin >> choice; + switch (choice) { + case 1: // 󼯺ϲ + cout << "AB\n"; + cout << "뼯AԪظ\n"; + cin >> na; + cout << "뼯BԪظ\n"; + cin >> nb; + InitList(La, na + nb); // A + CreateList(La, na); + InitList(Lb, nb); // B + CreateList(Lb, nb); + cout << "AΪ" << endl; // ʾA + DispList(La); + cout << "BΪ" << endl; // ʾB + DispList(Lb); + Union(La, Lb); // 󼯺ϲ + cout << "ABΪ:" << endl; // ʾ + DispList(La); + cout << endl; + DestroyList(La); + DestroyList(Lb); + break; + + case 2: // ˳ + cout << "Ҫ˳Ԫظ:"; + cin >> nc; + InitList(Lc, nc); + cout << endl; + CreateList(Lc, nc); + cout << "˳Ϊ" << endl; // ʾA + DispList(Lc); + ReverseSqList(Lc); + cout << "ú˳Ϊ" << endl; // ʾA + DispList(Lc); + cout << endl; + DestroyList(Lc); + break; + + case 3://ʽ + cout << "ʽAB\n"; + cout << "ʽA\n"; + cin >> na; + cout << "ʽB\n"; + cin >> nb; + InitList(fa, na); // ʽA + CreateList(fa, na); + InitList(fb, nb); // ʽB + CreateList(fb, nb); + cout << "ʽ A Ϊ " << endl; // ʾʽA + DispPoly(fa.elem, na); + cout << "ʽ B Ϊ " << endl; // ʾʽB + DispPoly(fb.elem, nb); + nc = (na >= nb) ? na : nb; + InitList(fc, nc); // ʽC + PolyAdd(fa, fb, fc); // ʽ C=A+B + cout << "ʽA + ʽB = " << endl; // ʾ + DispPoly(fc.elem, nc); + cout << endl; + DestroyList(fa); + DestroyList(fb); + DestroyList(fc); + break; + case 4: // ˳ + cout << "" << endl; + break; + default: // Чѡ + cout << "Invalid choice\n"; + break; + } + } while (choice != 4); + + return 0; +}//end of main function diff --git a/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.cpp b/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.cpp deleted file mode 100644 index 3ea49f1..0000000 --- a/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.cpp +++ /dev/null @@ -1,152 +0,0 @@ - -#include //cout,cin -using namespace std; -#include "SqList.h" - -char pause; - - -void dispmenu() { - //ʾ˵ - cout << endl; - cout << "*** ˳ ***\n"; - cout << " 1-ʼ˳\n"; - cout << " 2-˳\n"; - cout << " 3-ȡiԪ\n"; - cout << " 4-ֵԪλ\n"; - cout << " 5-iԪ\n"; - cout << " 6-ɾiԪ\n"; - cout << " 7-޸ĵiԪ\n"; - cout << " 8-ձ\n"; - cout << " 9-\n"; - cout << " 10-\n"; - cout << " 11-\n"; - cout << " 12-ʾ\n"; - cout << " 13-ֵǰ\n"; - cout << " 0-˳\n"; -} - - -// -void main() { - int i; - int e, pre_e; - SqList L; //Ϊ20ԪΪ͵Ŀ˳ - system("cls"); //ִϵͳcls - - int choice; - do { - dispmenu(); //ʾ˵ - cout << "Enter choice(1~120 ˳):"; - cin >> choice; - switch (choice) { - case 1: //ʼԱ - cout << "Ҫ˳ijȣ"; - cin >> i; - cout << endl; - InitList(L, i); - cout << endl << "ɹ" << endl; - break; - case 2: //Ա - cout << "ҪԪظ:"; - cin >> i; - cout << endl; - CreateList(L, i); - cout << "˳ԪΪ\n"; //ʾԪ - DispList(L); - cout << endl; - break; - case 3: //ȡiԪ - cout << "Ԫ:"; - cin >> i; - cout << endl; - if (GetElem_i(L, i, e)) - cout << endl << "" << i << "ԪΪ:" << e << endl; - else - cout << endl << "Ԫزڣ" << endl; - break; - case 4: //ֵ - cout << "ҪѯԪֵ:"; - cin >> e; - i = LocateElem_e(L, e); - if (i) - cout << endl << e << "ǵ" << i << "Ԫ" << endl; - else - cout << endl << "ڴԪأ" << endl; - break; - case 5: // ڵiλòԪ - cout << "λã" << endl; - cin >> i; - cout << "Ԫֵ" << endl; - cin >> e; - if (InsertElem_i(L, i, e)) { - cout << endl << "ɹ" << endl; - DispList(L); - } else - cout << endl << "벻ɹ" << endl;; - break; - case 6: //ɾiԪ - cout << "ɾԪλã" << endl; - cin >> i; - if (DeleElem_i(L, i)) { - cout << endl << "ɾɹ" << endl; - DispList(L); - } else - cout << endl << "ɾʧܣ" << endl; - break; - case 7: //޸ĵiԪصֵ - cout << "޸Ԫλã" << endl; - cin >> i; - cout << "Ԫֵ" << endl; - cin >> e; - if (PutElem(L, i, e)) { - cout << endl << "޸ijɹ" << endl; - DispList(L); - } else - cout << endl << "޸ʧܣ" << endl; - break; - case 8: // ձ - ClearList(L); - break; - case 9: // - cout << "Ϊ" << ListLength(L) << endl; - break; - case 10: // - if (ListEmpty(L)) - cout << endl << "ձ" << endl; - else - cout << endl << "ǿձ" << endl; - break; - case 11: // δ - if (ListFull(L)) - cout << endl << "" << endl; - else - cout << endl << "" << endl; - break; - case 12: //ʾ - DispList(L); - cout << endl; - cin.get(pause); - system("pause"); - break; - case 13: - cout << "˳Ϊ\n"; - DispList(L); - cout << "ǰԪֵ\n"; - cin >> e; - if (PriorElem_e(L, e, pre_e)) - cout << e << "ǰԪΪ" << pre_e << endl; - else - cout << e << "ǰԪأ" << endl; - break; - case 0: //˳ - - cout << "bye-bye!" << endl; - break; - default: //Чѡ - cout << "Чѡ!\n"; - break; - } - } while (choice != 0); -}//end of main function - diff --git a/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.h b/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.h deleted file mode 100644 index 774f16e..0000000 --- a/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.h +++ /dev/null @@ -1,175 +0,0 @@ -template -struct SqList // ˳ -{ - DT *elem; // ַ - int length; // - int size; // -}; - -//㷨2.1 -template -bool PriorElem_e(SqList
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 -bool InitList(SqList
&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 -bool CreateList(SqList
&L,int n) -{ - int i; - if(n>L.size) // 1.Ԫظڱܴ - { - cout<<"Ԫظڱܴ"<>L.elem[i-1]; - L.length=n; // 3.ΪԪظ - return true; -} - -//㷨2.4 ˳ -template -void DestroyList(SqList
&L) -{ - delete [] L.elem; // 1.ͷűռ - L.length=0; // 2.Ըֵ - L.size=0; -} - -//㷨2.5 ȡiԪֵ -template -bool GetElem_i(SqList
L,int i, DT &e) -{ - if(i<1 || i>L.length) // 1.λ򲻺false - { - cout<<"Ԫزڣ"< -int LocateElem_e(SqList
L, DT e) -{ - for(int i=0; i -bool InsertElem_i(SqList
&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 -bool DeleElem_i(SqList
&L,int i) -{ - if(L.length==0) // 1.գɾ - return false; - if(i<1 || i>L.length) // 2.ɾλòܲ - return false; - for (int j=i; j -bool PutElem(SqList
&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.޸ijɹtrue -} - - // ˳ -template -void ClearList(SqList
&L) -{ - L.length=0; // ձΪ0 -} - - - // -template -int ListLength(SqList
L) -{ - return L.length; -} - - -template -bool ListEmpty(SqList
L) // -{ - if(L.length==0) // ձtrue - return true; - else - return false; // ǿձfalse -} - -template -bool ListFull(SqList
L) -{ - if(L.length==L.size) // true - return true; - else - return false; // false -} - -//㷨2.10 -template -void DispList(SqList
L) -{ - int i; - for(i=0;i//cout,cin -using namespace std; -#include "SqList.h" - -char pause; - -void dispmenu() -{ // ʾ˵ - cout<<"\n*** ˳Ӧ ***\n"; - cout<<" 1-ϲA=AB\n"; - cout<<" 2-˳\n"; - cout<<" 3-ʽ\n"; - cout<<" 0-˳\n"; -} - - -//㷨2.11 -template -void Union(SqList
&La, SqList
Lb) // La=LaLa -{ - DT e; - int k,i; - for(i=1;i<=Lb.length;i++) // ɨLb - { - GetElem_i(Lb,i,e); // 1. ȡLbĵiԪ - if(!LocateElem_e(La,e)) // 2.La޴Ԫ - { - k=La.length+1; // Laıβ - InsertElem_i(La,k,e); - } - } -} - -//㷨2.12 -template -void ReverseSqList(SqList
&L) // ˳Ԫ -{ - DT t; - int i; - for(i=0; iL.elem[L.length-i-1] - { - t=L.elem[i]; - L.elem[i]=L.elem[L.length-i-1]; - L.elem[L.length-i-1]=t; - } - return; -} - -//㷨2.13 ʽ lc=la+lb -void PolyAdd(SqList la, SqList lb, SqList &lc) -{ - int i=0; // 1.ʼôʼλ - while(ilb.length) // 3.laδ꣬lbѴ - { - while(i La,Lb; // AB - SqList Lc; // ˳ - SqList fa,fb,fc; // ʽABC - - system("cls"); // - - int choice; - do - { - dispmenu(); // ʾ˵ - cout<<"Enter choice(1~4,0˳):"; - cin>>choice; - switch(choice) - { - case 1: // 󼯺ϲ - cout<<"AB\n"; - cout<<"뼯AԪظ"; - cin>>na; - cout<<"뼯BԪظ"; - cin>>nb; - InitList(La,na+nb); // A - cout<<"AԪ\n"; - CreateList(La,na); - InitList(Lb,nb); // B - cout<<"BԪ\n"; - CreateList(Lb,nb); - cout<<"AΪ"<>nc; - InitList(Lc,nc); - cout<>na; - InitList(fa,na); - cout<<"ʽAϵ\n"; - CreateList(fa,na); - cout<<"\nʽB\n"; // ʽB - cout<<"ʽB"; - cin>>nb; - InitList(fb,nb); - cout<<"ʽBϵ\n"; - CreateList(fb,nb); - cout<<"\nʽ A Ϊ "<=nb)?na:nb; - InitList(fc,nc); // ʽC - PolyAdd(fa,fb,fc); // ʽ C=A+B - cout<<"\nʽA + ʽB = "< //cout,cin +using namespace std; +#include "LinkQueue.h" + + +void dispmenu() +{//ʾ˵ + cout< Q; + system("cls");//ִϵͳcls + + int choice; + do + { + dispmenu();//ʾ˵ + cout<<"Enter choice(1~8):"; + cin>>choice; + switch(choice) + { + case 1://ʼ + InitQueue(Q); + cout<>e; + cout< +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; + if(!p) return false; //ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // + Q.rear->next=p; + Q.rear=p; + return true; // ɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*p; + if(Q.front==Q.rear) return 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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; //ӿ + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + //LNode
*p; + if(Q.front==Q.rear) return false; + e=Q.rear->data; + return true; // ɾɹtrue +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) + return true; + else + return false; +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< #include using namespace std; #include"LinkStack.h" -//㷨3.11 ŵƥ -bool match(string exp) +//ŵƥ +bool Matching() { - //ʽ(ʽ"#")"[""]""("")"Ƿƥ䣬ƥ䣬򷵻true򷵻false + //ʽǷȷƥ䣬ƥ䣬򷵻true򷵻false //ʽԡ# SNode *S; InitStack(S); int flag=1; // DzҽԿѭؽ - char ch; - char e,x; - int i=0; - ch=exp[i++]; // һַ - while(ch!='#' && flag) + char c; + char x; + cin>>c; // һַ + while(c!='#' && flag) { - switch (ch) - { - case '[': - case '(': // ţѹջ + switch (c){ + case '[': + case '(': // ţѹջ cout<<"Žջ"<>c; //һַ }//while - if (StackEmpty(S) && flag ) // ջұ־Ϊtrue,ƥ䣬true + DestroyStack(S); + if (StackEmpty(S) && flag ) return true; - else // Ųƥ,false + else return false; -}//match - - +}//Matching int main() { int flag; - string exp; cout<<"ƥıʽԡ#"<>exp; - flag = match(exp); + flag = Matching(); if(flag) cout<<"ƥɹ"< -#include -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<>person[i].sex; - cin>>person[i].name; - } - cout<<"ߣ"<"<>num; - person=new dancer[num]; - EntranHall(person, num); // 볡 - DancePartner(person,num); // ƥ - return 0; -} - diff --git a/OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp b/OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp similarity index 83% rename from OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp rename to OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp index 42dc18a..2c7f3e2 100644 --- a/OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp +++ b/OrigFiles/3-特殊线性表/3-LinkQueue(链队)/LinkQueue.cpp @@ -10,9 +10,8 @@ void dispmenu() cout<<"3-Ԫس\n"; cout<<"4-ȡͷԪ\n"; cout<<"5-ȡβԪ\n"; - cout<<"6-ն\n"; - cout<<"7-ӿ\n"; - cout<<"8-ʾԪ\n"; + cout<<"6-ӿ\n"; + cout<<"7-ʾԪ\n"; cout<<"0-˳\n"; } @@ -29,7 +28,7 @@ int main() do { dispmenu(); // ʾ˵ - cout<<"ѡ(1~80 ˳):"; + cout<<"ѡ(1~70 ˳):"; cin>>choice; switch(choice) { @@ -37,7 +36,7 @@ int main() InitQueue(Q); cout<>e; cout< * S; + LNode * S; system("cls"); // int choice; do @@ -66,26 +67,24 @@ int main() else cout< -struct SNode // +struct LNode // { - DT data; // 򣬴洢Ԫֵ - SNode *next; // ָָһ + DT data; //򣬴洢Ԫֵ + LNode *next;//ָָһ }; //㷨3.6 template -bool InitStack(SNode
*&S) // ջ +void InitStack(LNode
*&S)//ջ { + //L=new LNode
; //ͷ + //if(!L) exit(1); //ʧܣ S=NULL; - return true; } //㷨3.7 template -void DestroyStack(SNode
*(&S)) // ͷջռڴ +void DestroyStack(LNode
*(&S))//ͷջ { - SNode
*p; - while(S) //ͷ㿪ʼͷŽ + LNode
*p; + while(S)//ͷ㿪ʼͷŽ { p=S; S=S->next; delete p; } - S=NULL; + //L=NULL;//ͷָ } - - //㷨3.8 template -bool Push(SNode
*&S,DT e) +bool Push(LNode
*&S,DT e) { - SNode
*p; - p=new SNode
; - if(!p) - return false; //ʧܣ + LNode
*p; + p=new LNode
; + if(!p) return false; //ʧܣ p->data=e; // ½㸳ֵ p->next=S; //Sӵp֮ S=p; @@ -47,9 +45,9 @@ bool Push(SNode
*&S,DT e) //㷨3.9 template -bool Pop(SNode
*&S,DT &e) +bool Pop(LNode
*&S,DT &e) { - SNode
*p; + LNode
*p; if(S==NULL) return false; p=S; e=p->data; @@ -61,9 +59,9 @@ bool Pop(SNode
*&S,DT &e) //㷨3.10 template -bool GetTop(SNode
*S,DT &e) +bool GetTop(LNode
*S,DT &e) { - SNode
*p; + LNode
*p; if(S==NULL) return false; p=S; e=p->data; @@ -72,7 +70,7 @@ bool GetTop(SNode
*S,DT &e) //ջ template -bool StackEmpty(SNode
*S) +bool StackEmpty(LNode
*S) { if(S==NULL) return true; @@ -82,9 +80,9 @@ bool StackEmpty(SNode
*S) //ʾջ template -void DispStack(SNode
*S) +void DispStack(LNode
*S) { - SNode
*p; + LNode
*p; p=S; while(p) { diff --git a/OrigFiles/3-特殊线性表/3-SqQueue(循环队列)/SqQueue.cpp b/OrigFiles/3-特殊线性表/3-SqQueue(循环队列)/SqQueue.cpp deleted file mode 100644 index bbaa624..0000000 --- a/OrigFiles/3-特殊线性表/3-SqQueue(循环队列)/SqQueue.cpp +++ /dev/null @@ -1,126 +0,0 @@ - -#include//cout,cin -using namespace std; -#include "SqQueue.h" - -char pause; - - -void dispmenu() //˵ -{ - cout< Q; // ԪΪ͵˳ - system("cls"); // - int choice; - do - { - dispmenu(); //ʾ˵ - cout<<"ѡ(1~100 ˳):"; - cin>>choice; - switch(choice) - { - case 1: // ʼ˳ - cout<<"Ҫ˳еij"; - cin>>i; - cout<>e; - cout<>e; if(Push(S,e)) { - cout<<"ջɹջջԪΪ"< S) cout< -void ClearStack(SqStack
&S) -{ - S.top=-1; - -} - -template -int StackFull(SqStack
S) -{ - if(S.top==S.stacksize-1) - return 1; - else - return 0; - -} - - diff --git a/OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/Expresion.cpp b/OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/Expresion.cpp index bdc4d92..4153759 100644 --- a/OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/Expresion.cpp +++ b/OrigFiles/3-特殊线性表/3-ValExpression(表达式计算)/Expresion.cpp @@ -1,6 +1,7 @@ #include //cout,cin using namespace std; #include"SqStack.h" +#include // atoi() char pause; @@ -51,9 +52,9 @@ char Precede(char t1,char t2) // } -bool In(char ch) // ж ch ǷΪ +bool In(char c) // ж c ǷΪ { - switch(ch) + switch(c) { case'+': case'-': @@ -69,21 +70,21 @@ bool In(char ch) // float Operate(float a,char theta,float b) { // ʵʩһ - float ch; + float c; switch(theta) { - case'+':ch=a+b; + case'+':c=a+b; break; - case'-':ch=a-b; + case'-':c=a-b; break; - case'*':ch=a*b; + case'*':c=a*b; break; - case'/':ch=a/b; + case'/':c=a/b; } - return ch; + return c; } -//㷨3.12 + float Val_Exp(char *exp) // ׺ʽֵ { SqStack OP; // ջ @@ -91,82 +92,82 @@ float Val_Exp(char *exp) // InitStack(OP,30); InitStack(OD,30); char theta; - float a,b,result; - char ch,x; // ɼ̽յַ + float a,b,d; + char c,x; // ɼ̽յַ char z[6]; // ŷַ int i; Push(OP,'='); // # DZʽ־ - ch=*exp++; + c=*exp++; GetTop(OP,x); - while(ch!='='||x!='=') + while(c!='='||x!='=') { - if(ch>='0'&&ch<='9'||ch=='.') // ch Dz + if(c>='0'&&c<='9'||c=='.') // c Dz { i=0; do { - z[i]=ch; + z[i]=c; i++; - ch=*exp++; - } while(ch>='0'&&ch<='9'||ch=='.'); + c=*exp++; + } while(c>='0'&&c<='9'||c=='.'); z[i]='\0'; - result=atof(z); // ַתΪʹresult - Push(OD,result); + d=atof(z); // ַתΪʹd + Push(OD,d); } - else if(In(ch)) // 7֮һ - switch(Precede(x,ch)) + else if(In(c)) // 7֮һ + switch(Precede(x,c)) { - case'<':Push(OP,ch); // ջԪȨ - ch=*exp++; + case'<':Push(OP,c); // ջԪȨ + c=*exp++; break; case'=':Pop(OP,x); // Ųһַ - ch=*exp++; + c=*exp++; break; case'>':Pop(OP,theta); // ջջ Pop(OD,b); Pop(OD,a); Push(OD,Operate(a,theta,b)); } - else // chǷǷַ + else // cǷǷַ { cout<<"ERROR3"< OP; InitStack(OP,30); Push(OP,'='); // # DZʽ־ cout<<"׺ʽ"<='0'&&ch<='9')||ch=='.') + if((c>='0'&&c<='9')||c=='.') { - postexp[i++]=ch; - ch=*exp++; + postexp[i++]=c; + c=*exp++; } - if(In(ch)) // 7֮һ + if(In(c)) // 7֮һ { postexp[i++]=' '; GetTop(OP,x); - switch(Precede(x,ch)) + switch(Precede(x,c)) { - case'<':Push(OP,ch); // ջԪȨ - ch=*exp++; + case'<':Push(OP,c); // ջԪȨ + c=*exp++; break; case'=':Pop(OP,x); // Ųһַ - ch=*exp++; + c=*exp++; break; case'>': Pop(OP,postexp[i++]); // ջ @@ -179,43 +180,42 @@ void CreatePostExp(char * exp,char * &postexp) // DestroyStack(OP); } -//㷨3.13 float Val_PostExp(char *postexp) // ׺ʽֵ { int i; char z[6]; - float result=0,d=0,a,b; - char ch; + float v=0,d=0,a,b; + char c; SqStack OD; InitStack(OD,30); - ch=*postexp++; - while(ch!='\0') + c=*postexp++; + while(c!='\0') { - if(ch>='0'&&ch<='9'||ch=='.') // chΪ + if(c>='0'&&c<='9'||c=='.') // cΪ { i=0; do { - z[i++]=ch; - ch=*postexp++; // ȡһ - } while(ch>='0'&&ch<='9'||ch=='.'); + z[i++]=c; + c=*postexp++; // ȡһ + } while(c>='0'&&c<='9'||c=='.'); z[i]='\0'; - d=atof(z); // ַתΪʹresult + d=atof(z); // ַתΪʹd Push(OD,d); // ջ } - if(In(ch)) // chΪ + if(In(c)) // cΪ { - Pop (OD,a); Pop (OD,b); - Push (OD,Operate(b,ch,a)); - ch=*postexp++; + Pop (OD,a); + Push (OD,Operate(a,c,b)); + c=*postexp++; } - ch=*postexp++; + c=*postexp++; } - Pop (OD,result); - DestroyStack(OD); - return result; + Pop (OD,v); + //DestoryStack(OD); + return v; } @@ -224,6 +224,7 @@ void main() { //int i; char exp[20]="(2.2+5)+4*(5-3.1)="; + char *postexp; postexp=new char[30]; *postexp='\0'; @@ -275,7 +276,7 @@ void main() cout<<"\nУBye-Bye!"< + + + + + diff --git a/OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.cpp b/OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.cpp new file mode 100644 index 0000000..23c7bd6 --- /dev/null +++ b/OrigFiles/3-特殊线性表/BracketsMatch(括号匹配)/BracketsMatch.cpp @@ -0,0 +1,107 @@ +/***ջʵƥ***/ +#include +using namespace std; + +#define OK 1 +#define ERROR 0 +#define OVERFLOW -2 +typedef char SElemType; +typedef int Status; +typedef struct SNode{ + int data; + struct SNode *next; +}SNode,*LinkStack; + +Status InitStack(LinkStack &S) +{ + S = NULL; + return OK; +} +bool StackEmpty(LinkStack S) +{ + if(!S) + return true; + return false; +} +Status Push(LinkStack &S,SElemType e) +{ + SNode *p = new SNode; + if(!p) + { + return OVERFLOW; + } + p->data = e; + p->next = S; + S = p; + return OK; +} +Status Pop(LinkStack &S,SElemType &e) +{ + SNode *p; + if(!S) + return ERROR; + e = S->data; + p = S; + S = S->next; + delete p; + return OK; +} +Status GetTop(LinkStack &S,SElemType &e) +{ + if(!S) + return ERROR; + e = S->data; + return OK; +} + +//ŵƥ +Status Matching(LinkStack S) +{ + //ʽǷȷƥ䣬ƥ䣬򷵻true򷵻false + //ʽԡ# + int flag=1; //DzҽԿѭؽ + char c; + SElemType x; + cin>>c; //һַ + while(c!='#' && flag) + { + switch (c){ + case '[': + case '(': //ţѹջ + Push(S,c); + break; + case ')' : //š)ݵǰջԪصֵ + GetTop(S,x); + if (!StackEmpty(S) && x=='(' ) //ջǿջԪǡ(ƥɹ + Pop(S,x); + else + flag=0; //ջջջԪزǡ(Ƿ + break; + case ']' : //š]ݵǰջԪصֵ + GetTop(S,x); + if (!StackEmpty(S) && x=='[' ) //ջԪǡ[ƥɹ + Pop(S,x); + else + flag=0; //ջջջԪزǡ[Ƿ + break; + }//switch + cin>>c; //һַ + }//while + if (StackEmpty(S) && flag ) + return 1; + else + return 0; +}//Matching + +int main() +{ + LinkStack S; + InitStack(S); + cout<<"ƥıʽԡ#"< + + + + + + + + + diff --git a/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cbp b/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cbp new file mode 100644 index 0000000..5c2c7f5 --- /dev/null +++ b/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cbp @@ -0,0 +1,40 @@ + + + + + + diff --git a/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cpp b/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cpp new file mode 100644 index 0000000..d8b7c1f --- /dev/null +++ b/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.cpp @@ -0,0 +1,117 @@ +#include +#include +using namespace std; + +struct dancer +{ string name; + char sex; +}; + +struct Node +{ dancer data; + Node* next; +}*front,*rear; + +struct LinkQueue +{ Node *front; + Node *rear; +}Gentleman, Lady; + +void InitialLinkQueue(LinkQueue& queue) +{ queue.front=new Node; + queue.front->next=NULL; + queue.rear=queue.front; +} + +void DeleteLinkQueue(LinkQueue& queue) +{ Node *p; + while(queue.front!=NULL) + { p=queue.front; + queue.front=queue.front->next; + delete p; + } +} + +void EnQueue(LinkQueue& queue,dancer& value) +{ + Node* s=new Node; + s->data=value; + s->next=queue.rear->next; + queue.rear->next=s; + queue.rear=s; +} + +int IsEmpty(LinkQueue queue) +{ + if (queue.front==queue.rear) return 1; + else return 0; +} + +dancer DeQueue(LinkQueue &queue) +{ dancer x; + Node *p; + if (IsEmpty(queue)) throw "Ϊգ޷У"; + p=queue.front->next; + x=p->data; + queue.front->next=p->next; + if (p==queue.rear) queue.rear=queue.front; + delete p; + return x; +} + +dancer GetHead(LinkQueue queue) +{ if (IsEmpty(queue)) throw "Ϊգ޷ȡöԪأ"; + return queue.front->next->data; +} + +void QueueTranverse(LinkQueue queue) +{ Node *p; + p=queue.front->next; + while(p!=NULL) + { cout<<(p->data).name<<" "; + p=p->next; + } + cout<>num; + + InitialLinkQueue(Gentleman); + InitialLinkQueue(Lady); + + for(int i=0;i>sex; + string name; + cin>>name; + dancer newdancer; + newdancer.name=name; + newdancer.sex=sex; + if(sex=='F') + EnQueue(Lady,newdancer); + if(sex=='M') + EnQueue(Gentleman,newdancer); + } + + while ( (!IsEmpty(Gentleman)) && (!IsEmpty(Lady)) ) + cout<"< + + diff --git a/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.layout b/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.layout new file mode 100644 index 0000000..8f5dcc4 --- /dev/null +++ b/OrigFiles/3-特殊线性表/DancePartner(舞伴问题)/DancePartner.layout @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.cbp b/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.cbp new file mode 100644 index 0000000..1a44a30 --- /dev/null +++ b/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.cbp @@ -0,0 +1,38 @@ + + + + + + diff --git a/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.cpp b/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.cpp new file mode 100644 index 0000000..159642f --- /dev/null +++ b/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.cpp @@ -0,0 +1,147 @@ +/***ջʵֱʽֵ***/ +#include +#include +using namespace std; + +const char oper[7] = {'+','-','*','/','(',')','#'}; +#define OK 1 +#define ERROR 0 +#define OVERFLOW -2 +typedef char SElemType; +typedef int Status; +typedef struct SNode{ + int data; + struct SNode *next; +}SNode,*LinkStack; + +Status InitStack(LinkStack &S) +{ + S = NULL; + return OK; +} +bool StackEmpty(LinkStack S) +{ + if(!S) + return true; + return false; +} +Status Push(LinkStack &S,SElemType e) +{ + SNode *p = new SNode; + if(!p) + { + return OVERFLOW; + } + p->data = e; + p->next = S; + S = p; + return OK; +} +Status Pop(LinkStack &S,SElemType &e) +{ + SNode *p; + if(!S) + return ERROR; + e = S->data; + p = S; + S = S->next; + delete p; + return OK; +} +Status GetTop(LinkStack &S,SElemType &e) +{ + if(!S) + return ERROR; + e = S->data; + return OK; +} +bool In(char ch) +{//жchǷΪ + for(int i = 0;i < 7;i ++) + { + if(ch == oper[i]) + { + return true; + } + } + return false; +} +char Precede(char theta1,char theta2) +{//жȼ + if((theta1 == '('&&theta2 == ')')||(theta1 == '#'&&theta2 == '#')) + { + return '='; + } + else if(theta1 == '('||theta1 == '#'||theta2 == '(' + ||(theta1 == '+'||theta1 == '-')&&(theta2 == '*'||theta2 == '/')) + { + return '<'; + } + else + return '>'; +} +char Operate(char first,char theta,char second) +{// + switch(theta) + { + case '+': + return (first - '0')+(second - '0')+48; + case '-': + return (first - '0')-(second - '0')+48; + case '*': + return (first - '0')*(second - '0')+48; + case '/': + return (first - '0')/(second - '0')+48; + } + return 0; +} + +//ʽֵ +char EvaluateExpression() +{ + // ʽֵ㷨OPTROPNDֱΪջͲջ + // OP Ϊ + LinkStack OPTR,OPND; + char ch,theta,a,b,x,top; + InitStack ( OPTR); + Push (OPTR,'#'); + InitStack ( OPND); + ch = getchar(); + while (ch != '#' || (GetTop(OPTR,top) ,top!= '#') ) + { + if (!In(ch)) + { + Push(OPND, ch); + ch = getchar(); + } // chջ + else + switch (GetTop(OPTR, top),Precede(top,ch)) + { //ȽOPTRջԪغchȨ + case '<': //ǰַchѹOPTRջһַch + Push(OPTR, ch); + ch = getchar(); + break; + case '>': //OPTRջӦ㣬ջ + Pop(OPTR, theta); + Pop(OPND, b); + Pop(OPND, a); + Push(OPND, Operate(a, theta, b)); + break; + case '=': //Ųһַ + Pop(OPTR, x); + ch = getchar(); + break; + } // switch + } // while + GetTop(OPND,ch); + return ch; +} // EvaluateExpression + +int main() +{ + cout<<"Ҫıʽͽ0-9ķΧڣ#"< + + diff --git a/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.layout b/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.layout new file mode 100644 index 0000000..7c0769e --- /dev/null +++ b/OrigFiles/3-特殊线性表/EvaluateExpression(表达式计算)/EvaluateExpression.layout @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/OrigFiles/3-特殊线性表/LinkQueue(链队)/LinkQueue.cpp b/OrigFiles/3-特殊线性表/LinkQueue(链队)/LinkQueue.cpp new file mode 100644 index 0000000..2076a14 --- /dev/null +++ b/OrigFiles/3-特殊线性表/LinkQueue(链队)/LinkQueue.cpp @@ -0,0 +1,97 @@ +#include //cout,cin +using namespace std; +#include "LinkQueue.h" + +void dispmenu() +{//ʾ˵ + cout< Q; + system("cls"); //ִϵͳcls + + int choice; + do + { + dispmenu(); //ʾ˵ + cout<<"Enter choice(1~8):"; + cin>>choice; + switch(choice) + { + case 1: //ʼ + InitQueue(Q); + cout<>e; + cout< &Q)// Q.front=Q.front->next; delete p; } - Q.rear=Q.front=NULL; -} - -template -void ClearQueue(LinkQueue
&Q) // -{ - QNode
*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 diff --git a/OrigFiles/3-特殊线性表/LinkStack(链栈)/LinkStack.cpp b/OrigFiles/3-特殊线性表/LinkStack(链栈)/LinkStack.cpp new file mode 100644 index 0000000..0d403a4 --- /dev/null +++ b/OrigFiles/3-特殊线性表/LinkStack(链栈)/LinkStack.cpp @@ -0,0 +1,88 @@ +#include //cout,cin +using namespace std; +#include "LinkStack.h" + + +void dispmenu() +{//ʾ˵ + cout< * S; + system("cls"); // + int choice; + do + { + dispmenu(); //ʾ˵ + cout<<"Enter choice(1~7):"; + cin>>choice; + switch(choice) + { + case 1: //ʼջ + InitStack(S); + cout<>e; + cout< +struct LNode // +{ + DT data; //򣬴洢Ԫֵ + LNode *next;//ָָһ +}; + + +//㷨3.6 +template +void InitStack(LNode
*&S)//ջ +{ + //L=new LNode
; //ͷ + //if(!L) exit(1); //ʧܣ + S=NULL; +} + +//㷨3.7 +template +void DestroyStack(LNode
*(&S))//ͷջ +{ + LNode
*p; + while(S)//ͷ㿪ʼͷŽ + { + p=S; + S=S->next; + delete p; + } + //L=NULL;//ͷָ +} + +//㷨3.8 +template +bool Push(LNode
*&S,DT e) +{ + LNode
*p; + p=new LNode
; + if(!p) return false; //ʧܣ + p->data=e; // ½㸳ֵ + p->next=S; //Sӵp֮ + S=p; + return true; // ɹtrue +} + +//㷨3.9 +template +bool Pop(LNode
*&S,DT &e) +{ + LNode
*p; + if(S==NULL) return false; + p=S; + e=p->data; + S=S->next; + delete p; + return true; // ɾɹtrue +} + + +//㷨3.10 +template +bool GetTop(LNode
*S,DT &e) +{ + LNode
*p; + if(S==NULL) return false; + p=S; + e=p->data; + return true; // ɾɹtrue +} + +//ջ +template +bool StackEmpty(LNode
*S) +{ + if(S==NULL) + return true; + else + return false; +} + +//ʾջ +template +void DispStack(LNode
*S) +{ + LNode
*p; + p=S; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout<//cout,cin +using namespace std; +#include "SqQueue.h" + +char pause; + + +void dispmenu() +{//ʾ˵ + cout< Q;//Ϊ20ԪΪ͵Ŀ˳ջ + system("cls");//ִϵͳcls + + int choice; + do + { + dispmenu();//ʾ˵ + cout<<"Enter choice(1~9):"; + cin>>choice; + switch(choice) + { + case 1://ʼ˳ + cout<<"Ҫ˳еij"; + cin>>i; + cout<>e; + cout< +struct SqQueue //˳ +{ + DT *base; //ַ + int front; //ͷָ + int rear; //βָ + int queuesize;// +}; + +//ʵ +//㷨3.14 +template +void InitQueue(SqQueue
&Q, int m) +{//һջΪmĿն + Q.base=new DT[m];// пռ + if(Q.base==NULL) + { + cout<<"δɹ"; + exit (1); + } + Q.front=Q.rear=0; + Q.queuesize=m; +} + + +//㷨3.15 +template +void DestroyQueue(SqQueue
&Q)// +{//ͷŶпռ + delete [] Q.base; + Q.front=Q.rear=0; + Q.queuesize=0; +} + + +//㷨3.16 +template +bool EnQueue(SqQueue
&Q,DT e) +{ + if((Q.rear+1)%Q.queuesize==Q.front) // + return false; + Q.base[Q.rear]=e; + Q.rear=(Q.rear+1)% Q.queuesize; + return true; // ɹtrue +} + +//㷨3.17 +template +bool DeQueue(SqQueue
&Q,DT &e) +{ + if(Q.front==Q.rear) //ӿ + return false; + e=Q.base[Q.front]; + Q.front=(Q.front+1)%Q.queuesize; + return true; // ɾɹtrue +} + + +//㷨3.18 +template +bool GetHead(SqQueue
Q,DT &e) +{ + if(Q.front==Q.rear) //ӿ + return false; + e=Q.base[Q.front]; + return true; //true +} + +//ȡβԪ +template +bool GetTail(SqQueue
Q,DT &e) +{ + if(Q.front==Q.rear) //ӿ + return false; + e=Q.base[(Q.rear - 1+Q.queuesize) % Q.queuesize]; + return true; //true +} + +//ӿ +template +bool QueueEmpty(SqQueue
Q) +{ + if(Q.front==Q.rear) + return true; + else + return false; +} + +// +template +bool QueueFull(SqQueue
Q) +{ + if((Q.rear+1)%Q.queuesize==Q.front) // + return true; + else + return false; +} + +//ʾ +template +void DispQueue(SqQueue
Q) +{ + int i=Q.front; + while(i!=Q.rear) + { + cout<//cout,cin +using namespace std; +#include "SqStack.h" + +char pause; + + +void dispmenu() +{ //ʾ˵ + cout< S;//Ϊ20ԪΪ͵Ŀ˳ջ + system("cls"); // + + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~7):"; + cin>>choice; + switch(choice) + { + case 1: // ʼ˳ջ + cout<<"Ҫ˳ջij"; + cin>>i; + cout<>e; + cout< +struct SqStack // ˳ջ +{ + DT *base; // ջַ + int top; // ջָ + int stacksize; // ջ +}; + +//ʵ +//㷨3.1 // ʼջ +template +void InitStack(SqStack
&S, int m) +{ + S.base=new DT[m]; // ջռ + if(S.base==NULL) // ʧܣ˳ + { + cout<<"δɹ"; + exit (1); + } + S.top=-1; // ÿջ + S.stacksize=m; +} + + +//㷨3.2 // ջ +template +void DestroyStack(SqStack
&S) +{ + delete [] S.base; // ͷջռ + S.top=-1; + S.stacksize=0; // ջ +} + + +//㷨3.3 // ջ +template +bool Push(SqStack
&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 +bool Pop(SqStack
&S,DT &e) +{ + if(S.top==-1) // ջ + return false; // false + e=S.base[S.top]; // ȡջԪ + S.top--; // ջָ + return true; // ջɹtrue +} + + +//㷨3.5 // ȡջԪ +template +bool GetTop(SqStack
S,DT &e) +{ + if(S.top==-1) // ջ + return false; // false + e=S.base[S.top]; // ջǿգȡջԪ + return true; // true +} + + // ջ +template +bool StackEmpty(SqStack
S) +{ + if(S.top==-1) // ջtrue + return true; + else // ջfalse + return false; +} + + //ʾջ +template +void DispStack(SqStack
S) +{ + int i=S.top; + while(i>=0) + { + cout<//cout,cin +#include"process.h"//exit() +#include"stdio.h"//EOF,NULL + +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(ae=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;ijj) + { + 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;ij) + cout<<'\t'<e; + p=p->next; + } + }//for + cout<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="<//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 +void DestroyQueue(SqQueue
&Q) +{ + delete [] Q.base; // ͷŶпռ + Q.front=Q.rear=0; // ö + Q.queuesize=0; +} + + +//㷨3.16 +template +bool EnQueue(SqQueue
&Q,BTNode
* 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 +bool DeQueue(SqQueue
&Q,BTNode
* &e) +{ + if(Q.front==Q.rear) // ӿ + return false; + e=Q.base[Q.front]; + Q.front=(Q.front+1)%Q.queuesize; + return true; // ɾɹtrue +} + + // ӿ +template +bool QueueEmpty(SqQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; // true + else // Ӳ + return false; // false +} \ No newline at end of file diff --git a/OrigFiles/5-树和二叉树/5-树和二叉树/5-BiTree(二叉树)/SqStack.h b/OrigFiles/5-树和二叉树/5-树和二叉树/5-BiTree(二叉树)/SqStack.h new file mode 100644 index 0000000..ede3f93 --- /dev/null +++ b/OrigFiles/5-树和二叉树/5-树和二叉树/5-BiTree(二叉树)/SqStack.h @@ -0,0 +1,76 @@ +//ջ +template +struct SqStack // ˳ջ +{ + BTNode
* *base; // ջַ + int top; // ջָ + int stacksize; // ջ +}; + + +//㷨3.1 ʼջ +template +void InitStack(SqStack
&S, int m) +{ + S.base=new BTNode
* [m]; // ջռ + if(S.base==NULL) + { + cout<<"δɹ"; + exit (1); + } + S.top=-1; // ջ + S.stacksize=m; // ջΪm +} + + +//㷨3.2 ջ +template +void DestroyStack(SqStack
&S)// +{ + delete [] S.base; //ͷջռ + S.top=-1; + S.stacksize=0; +} + + +//㷨3.3 +template +bool Push(SqStack
&S,BTNode
* e) +{ + if(S.top==S.stacksize-1) // ջܲ + return false; + S.top++; + S.base[S.top]=e; + return true; // ɹtrue +} + +//㷨3.4 ջ +template +bool Pop(SqStack
&S,BTNode
* &e) +{ + if(S.top==-1) //ջ + return false; + e=S.base[S.top]; + S.top--; + return true; // ջɹtrue +} + +template // ȡջԪ +bool GetTop(SqStack
&S,BTNode
* &e) +{ + if(S.top==-1) // ջգfalse + return false; + e=S.base[S.top]; // ȡջԪ + return true; // true +} + + + // ջ +template +bool StackEmpty(SqStack
S) +{ + if(S.top==-1) // ջգtrue + return true; + else // ջǿգfalse + return false; +} \ No newline at end of file diff --git a/OrigFiles/5-树和二叉树/5-树和二叉树/5-BiTree(二叉树)/bitree.cpp b/OrigFiles/5-树和二叉树/5-树和二叉树/5-BiTree(二叉树)/bitree.cpp new file mode 100644 index 0000000..e715167 --- /dev/null +++ b/OrigFiles/5-树和二叉树/5-树和二叉树/5-BiTree(二叉树)/bitree.cpp @@ -0,0 +1,131 @@ + +#include//cout,cin +#include +using namespace std; +#include"bitree.h" +#include"SqQueue.h" +#include"SqStack.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< *bt; + + system("cls"); // + + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~120 ˳):"; + cin>>choice; + switch(choice) + { + case 1: // + cout<<"Բοݣ"< *p; + cout<<"ҪѯĽֵ"<>e; + p=Search(bt,e); + if(p) + { + cout<<"ҵ"; + cout<data< +struct BTNode +{ + DT data ; // + BTNode* lchild; //ָָ + BTNode* rchild; //ָָ +}; + + +//㷨5.1 ݹ㷨 +template +void PreOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + cout<data<<' '; //ϵ + PreOrDerBiTree(bt->lchild); //ݹĵǰ + PreOrDerBiTree(bt->rchild); //ݹĵǰ + } + return; +} + +//㷨5.2 ݹ㷨 +template +void InOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + InOrDerBiTree(bt->lchild); //ݹĵ + cout<data<<' '; //ϵ + InOrDerBiTree(bt->rchild); //ݹĵ + } + return; +} + +//㷨5.3 ݹ㷨 +template +void PostOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + PostOrDerBiTree(bt->lchild); //ݹĵú + PostOrDerBiTree(bt->rchild); //ݹĵú + cout<data<<' '; //ϵ + } + return; +} + + +//㷨5.4 㷨 +template +void LevelBiTree(BTNode
*bt) +{ + SqQueue
Q; // һ + int m=20; + InitQueue(Q,m); + BTNode
* p; + p=bt; + if(p) EnQueue(Q,p); // ǿգ + while (!QueueEmpty(Q)) // ӷǿ + { + DeQueue(Q,p); // + cout<data; // + if(p->lchild!=NULL) // + EnQueue(Q, p->lchild); // + if(p->rchild!=NULL) // Һ + EnQueue(Q, p->rchild); // Һ + } + DestroyQueue(Q); // ٶ +} + + +//㷨5.5 DZݹ㷨 +template +void PreOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // ջ + int m=20; + InitStack(S, m); + BTNode
* p; + p=bt; + while (p!=NULL || !StackEmpty(S)) // ǿջջǿ + { + while(p!=NULL) // ǿ + { + cout<data<<' '; // ʽ + Push(S,p); // ջ + p=p->lchild; // ת + } + if(!StackEmpty(S)) // ջǿ + { + Pop(S,p); // ջ + p=p->rchild; // תջ + } + } + DestroyStack(S); //ջ +} + + +//㷨5.6 DZݹ㷨 +template +void InOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // һջ + int m=20; + InitStack(S, m); + BTNode
* p; + p=bt; + while (p!=NULL || !StackEmpty(S)) // ǿջջǿ + { + while(p!=NULL) // ǿ + { + Push(S,p); // ջ + p=p->lchild; // תջ + } + if(!StackEmpty(S)) // ջǿ + { + Pop(S,p); // ջ + cout<data<<' '; // ʳջ + p=p->rchild; // תջ + } + } + DestroyStack(S); // ջ +} + + + +//㷨5.7 DZݹ㷨 +template +void PostOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // һջ + int m=20; + InitStack(S, m); + BTNode
* p; + BTNode
* 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<data<<' '; // ջԪ + Pop(S,p); // ջ + r=p; // rָ򱻷ʽ + } + else // + { + p=p->rchild; // תջԪҺ + flag=false; // ջ + } + } + }while(!StackEmpty(S)); // ջǿգѭ + cout< +void CreateBiTree(BTNode
*&bt) +{ + char ch; + cin>>ch; // + if(ch=='#') // # ʾָΪգ˵Ϊ + bt=NULL ; + else + { + bt=new BTNode
; // ڴ + if(!bt) + { + cout<<"ڴʧܣ"<data=ch; + CreateBiTree(bt->lchild); // + CreateBiTree(bt->rchild); // + } + return; +} + + +//㷨5.9 ٶ +template +void DestroyBiTree(BTNode
*&bt) +{ + if(bt) // ǿ + { + DestroyBiTree(bt->lchild); // + DestroyBiTree(bt->rchild); // + delete bt; + } +} + + +//㷨5.10 + +template +BTNode
* Search(BTNode
* bt, DT e) // ֵΪeԪ +{ + BTNode
*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 +int Depth(BTNode
*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 +int NodeCount(BTNode
*bt) +{ + if(bt==NULL) // Ϊ0 + return 0; + else // ǿΪĺͼ1 + return NodeCount(bt->lchild)+NodeCount(bt->rchild)+1; +} + +template +void DispBiTree(BTNode
* bt,int level) // ʾ +{ + if(bt) //նʾ + { DispBiTree(bt->rchild,level+1); //ʾ + cout<data; //ʾڵ + DispBiTree(bt->lchild,level+1); //ʾ + cout< +int leftCount(BTNode
*bt) +{ + if(bt->lchild==NULL & bt->rchild==NULL) // Ϊ0 + return 0; + else // ǿΪĺͼ1 + return NodeCount(bt->lchild)+NodeCount(bt->rchild)+1; +} \ No newline at end of file diff --git a/OrigFiles/5-树和二叉树/5-树和二叉树/5-InThrBiTree(中序线索二叉树)/InThrBiTree.h b/OrigFiles/5-树和二叉树/5-树和二叉树/5-InThrBiTree(中序线索二叉树)/InThrBiTree.h new file mode 100644 index 0000000..b3ab3df --- /dev/null +++ b/OrigFiles/5-树和二叉树/5-树和二叉树/5-InThrBiTree(中序线索二叉树)/InThrBiTree.h @@ -0,0 +1,128 @@ +template +struct ThrBTNode +{ + DT data; // + int lflag; // ־ + int rflag; // ұ־ + ThrBTNode *lchild; // ָ + ThrBTNode *rchild; // ָ +}; + + +template // +void CreateBiTree(ThrBTNode
*&bt) +{ // е˳,#Ϊָ־; + char ch; + cin>>ch; // + if(ch=='#') // # ʾָΪգ˵Ϊ + bt=NULL ; + else + { + bt=new ThrBTNode
; // ½ + if(!bt) + { + cout<<"ڴʧܣ"<lflag=0; // + bt->rflag=0; // + bt->data=ch; + CreateBiTree(bt->lchild); // ݹ鴴 + CreateBiTree(bt->rchild); // ݹ鴴 + } + return; +} + +ThrBTNode *pre; + +//㷨5.13 +template +void InThread(ThrBTNode
*&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 // +ThrBTNode
* CreateInThread(ThrBTNode
*&bt) +{ + ThrBTNode
*root; + root=new ThrBTNode
; // ͷ + 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 +void InThrBiTree( ThrBTNode
* bt) +{ + ThrBTNode
*p; + p=bt->lchild; // Ӹ㿪ʼ + while(p!=bt) // ǿ + { + while (p->lflag==0) // + p=p->lchild; // һ· + cout<data; // ʽ + while(p->rflag==1 && p->rchild!=bt) // кҷǿ + { + p=p->rchild; // ת + cout<data<<" "; // ʺ̽ + } + p=p->rchild; // ޺ת + } +} + +template // ʾ +void DispBiTree(ThrBTNode
* bt,int level) +{ + if(bt) // նʾ + { DispBiTree(bt->rchild,level+1); // ʾ + cout<data; // ʾڵ + DispBiTree(bt->lchild,level+1); // ʾ + cout< +void DestroyThrBiTree(ThrBTNode
*&bt) +{ + if(bt) + { + DestroyThrBiTree(bt->lchild); + DestroyThrBiTree(bt->rchild); + delete bt; + } +} \ No newline at end of file diff --git a/OrigFiles/5-树和二叉树/5-树和二叉树/5-InThrBiTree(中序线索二叉树)/ThrBiTree.cpp b/OrigFiles/5-树和二叉树/5-树和二叉树/5-InThrBiTree(中序线索二叉树)/ThrBiTree.cpp new file mode 100644 index 0000000..26bf2f3 --- /dev/null +++ b/OrigFiles/5-树和二叉树/5-树和二叉树/5-InThrBiTree(中序线索二叉树)/ThrBiTree.cpp @@ -0,0 +1,74 @@ +#include//cout,cin +#include +#include"InThrBiTree.h" +using namespace std; + + +void dispmenu() +{ // ʾ˵ + cout< *bt; + system("cls"); // + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~7):"; + cin>>choice; + switch(choice) + { + case 1: // + cout<<"ݲο"< +struct SqQueue // ˳ +{ + BTNode
* *base; // ַ + int front; // ͷָ + int rear; // βָ + int queuesize; // +}; + +//㷨3.14 ʼ +template +void InitQueue(SqQueue
&Q, int m) +{ + Q.base=new BTNode
*[m]; // пռ + if(Q.base==NULL) // ռʧ + { + cout<<"δɹ"; + exit (1); // ˳ + } + Q.front=Q.rear=0; // ö + Q.queuesize=m; +} + + +//㷨3.15 +template +void DestroyQueue(SqQueue
&Q) +{ + delete [] Q.base; // ͷŶпռ + Q.front=Q.rear=0; // ö + Q.queuesize=0; +} + + +//㷨3.16 +template +bool EnQueue(SqQueue
&Q,BTNode
* 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 +bool DeQueue(SqQueue
&Q,BTNode
* &e) +{ + if(Q.front==Q.rear) // ӿ + return false; + e=Q.base[Q.front]; + Q.front=(Q.front+1)%Q.queuesize; + return true; // ɾɹtrue +} + + // ӿ +template +bool QueueEmpty(SqQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; // true + else // Ӳ + return false; // false +} \ No newline at end of file diff --git a/OrigFiles/5-树和二叉树/5-树和二叉树/BiTree/SqStack.h b/OrigFiles/5-树和二叉树/5-树和二叉树/BiTree/SqStack.h new file mode 100644 index 0000000..ede3f93 --- /dev/null +++ b/OrigFiles/5-树和二叉树/5-树和二叉树/BiTree/SqStack.h @@ -0,0 +1,76 @@ +//ջ +template +struct SqStack // ˳ջ +{ + BTNode
* *base; // ջַ + int top; // ջָ + int stacksize; // ջ +}; + + +//㷨3.1 ʼջ +template +void InitStack(SqStack
&S, int m) +{ + S.base=new BTNode
* [m]; // ջռ + if(S.base==NULL) + { + cout<<"δɹ"; + exit (1); + } + S.top=-1; // ջ + S.stacksize=m; // ջΪm +} + + +//㷨3.2 ջ +template +void DestroyStack(SqStack
&S)// +{ + delete [] S.base; //ͷջռ + S.top=-1; + S.stacksize=0; +} + + +//㷨3.3 +template +bool Push(SqStack
&S,BTNode
* e) +{ + if(S.top==S.stacksize-1) // ջܲ + return false; + S.top++; + S.base[S.top]=e; + return true; // ɹtrue +} + +//㷨3.4 ջ +template +bool Pop(SqStack
&S,BTNode
* &e) +{ + if(S.top==-1) //ջ + return false; + e=S.base[S.top]; + S.top--; + return true; // ջɹtrue +} + +template // ȡջԪ +bool GetTop(SqStack
&S,BTNode
* &e) +{ + if(S.top==-1) // ջգfalse + return false; + e=S.base[S.top]; // ȡջԪ + return true; // true +} + + + // ջ +template +bool StackEmpty(SqStack
S) +{ + if(S.top==-1) // ջգtrue + return true; + else // ջǿգfalse + return false; +} \ No newline at end of file diff --git a/OrigFiles/5-树和二叉树/5-树和二叉树/BiTree/bitree.cpp b/OrigFiles/5-树和二叉树/5-树和二叉树/BiTree/bitree.cpp new file mode 100644 index 0000000..8343879 --- /dev/null +++ b/OrigFiles/5-树和二叉树/5-树和二叉树/BiTree/bitree.cpp @@ -0,0 +1,131 @@ + +#include//cout,cin +#include +using namespace std; +#include"bitree.h" +#include"SqQueue.h" +#include"SqStack.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< *bt; + + system("cls"); // + + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~13):"; + cin>>choice; + switch(choice) + { + case 1: // + cout<<"Բοݣ"< *p; + cout<<"ҪѯĽֵ"<>e; + p=Search(bt,e); + if(p) + { + cout<<"ҵ"; + cout<data< +struct BTNode +{ + DT data ; // + BTNode* lchild; //ָָ + BTNode* rchild; //ָָ +}; + + +//㷨5.1 ݹ㷨 +template +void PreOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + cout<data<<' '; //ϵ + PreOrDerBiTree(bt->lchild); //ݹĵǰ + PreOrDerBiTree(bt->rchild); //ݹĵǰ + } + return; +} + +//㷨5.2 ݹ㷨 +template +void InOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + InOrDerBiTree(bt->lchild); //ݹĵ + cout<data<<' '; //ϵ + InOrDerBiTree(bt->rchild); //ݹĵ + } + return; +} + +//㷨5.3 ݹ㷨 +template +void PostOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + PostOrDerBiTree(bt->lchild); //ݹĵú + PostOrDerBiTree(bt->rchild); //ݹĵú + cout<data<<' '; //ϵ + } + return; +} + + +//㷨5.4 㷨 +template +void LevelBiTree(BTNode
*bt) +{ + SqQueue
Q; // һ + int m=20; + InitQueue(Q,m); + BTNode
* p; + p=bt; + if(p) EnQueue(Q,p); // ǿգ + while (!QueueEmpty(Q)) // ӷǿ + { + DeQueue(Q,p); // + cout<data; // + if(p->lchild!=NULL) // + EnQueue(Q, p->lchild); // + if(p->rchild!=NULL) // Һ + EnQueue(Q, p->rchild); // Һ + } + DestroyQueue(Q); // ٶ +} + + +//㷨5.5 DZݹ㷨 +template +void PreOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // ջ + int m=20; + InitStack(S, m); + BTNode
* p; + p=bt; + while (p!=NULL || !StackEmpty(S)) // ǿջջǿ + { + while(p!=NULL) // ǿ + { + cout<data<<' '; // ʽ + Push(S,p); // ջ + p=p->lchild; // ת + } + if(!StackEmpty(S)) // ջǿ + { + Pop(S,p); // ջ + p=p->rchild; // תջ + } + } + DestroyStack(S); //ջ +} + + +//㷨5.6 DZݹ㷨 +template +void InOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // һջ + int m=20; + InitStack(S, m); + BTNode
* p; + p=bt; + while (p!=NULL || !StackEmpty(S)) // ǿջջǿ + { + while(p!=NULL) // ǿ + { + Push(S,p); // ջ + p=p->lchild; // תջ + } + if(!StackEmpty(S)) // ջǿ + { + Pop(S,p); // ջ + cout<data<<' '; // ʳջ + p=p->rchild; // תջ + } + } + DestroyStack(S); // ջ +} + + + +//㷨5.7 DZݹ㷨 +template +void PostOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // һջ + int m=20; + InitStack(S, m); + BTNode
* p; + BTNode
* 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<data<<' '; // ջԪ + Pop(S,p); // ջ + r=p; // rָ򱻷ʽ + } + else // + { + p=p->rchild; // תջԪҺ + flag=false; // ջ + } + } + }while(!StackEmpty(S)); // ջǿգѭ + cout< +void CreateBiTree(BTNode
*&bt) +{ + char ch; + cin>>ch; // + if(ch=='#') // # ʾָΪգ˵Ϊ + bt=NULL ; + else + { + bt=new BTNode
; // ڴ + if(!bt) + { + cout<<"ڴʧܣ"<data=ch; + CreateBiTree(bt->lchild); // + CreateBiTree(bt->rchild); // + } + return; +} + + +//㷨5.9 ٶ +template +void DestroyBiTree(BTNode
*&bt) +{ + if(bt) // ǿ + { + DestroyBiTree(bt->lchild); // + DestroyBiTree(bt->rchild); // + delete bt; + } +} + + +//㷨5.10 + +template +BTNode
* Search(BTNode
* bt, DT e) // ֵΪeԪ +{ + BTNode
*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 +int Depth(BTNode
*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 +int NodeCount(BTNode
*bt) +{ + if(bt==NULL) // Ϊ0 + return 0; + else // ǿΪĺͼ1 + return NodeCount(bt->lchild)+NodeCount(bt->rchild)+1; +} + +template +void DispBiTree(BTNode
* bt,int level) // ʾ +{ + if(bt) //նʾ + { DispBiTree(bt->rchild,level+1); //ʾ + cout<data; //ʾڵ + DispBiTree(bt->lchild,level+1); //ʾ + cout< +#include +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ѡȨֵСij +{ + int m1,m2; + m1=m2=32767; // + i1=i2=0; + for(int j=0;jweight<parent + <lchild<rchild<>op; + switch(op) + { + case 1: + cout<<"԰"<>n; + w=new int[n]; + cout<<"Ȩֵ"<>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!"<//cout,cin +#include +using namespace std; + +template +struct ThrBTNode +{ + DT data; // + int lflag; // ־ + int rflag; // ұ־ + ThrBTNode *lchild; // ָ + ThrBTNode *rchild; // ָ +}; + + +template // +void CreateBiTree(ThrBTNode
*&bt) +{ // е˳,#Ϊָ־; + char ch; + cin>>ch; // + if(ch=='#') // # ʾָΪգ˵Ϊ + bt=NULL ; + else + { + bt=new ThrBTNode
; // ½ + if(!bt) + { + cout<<"ڴʧܣ"<lflag=0; // + bt->rflag=0; // + bt->data=ch; + CreateBiTree(bt->lchild); // ݹ鴴 + CreateBiTree(bt->rchild); // ݹ鴴 + } + return; +} + +ThrBTNode *pre; + +//㷨5.13 +template +void InThread(ThrBTNode
*&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 // +ThrBTNode
* CreateInThread(ThrBTNode
*&bt) +{ + ThrBTNode
*root; + root=new ThrBTNode
; // ͷ + 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 +void InThrBiTree( ThrBTNode
* bt) +{ + ThrBTNode
*p; + p=bt->lchild; // Ӹ㿪ʼ + while(p!=bt) // ǿ + { + while (p->lflag==0) // + p=p->lchild; // һ· + cout<data; // ʽ + while(p->rflag==1 && p->rchild!=bt) // кҷǿ + { + p=p->rchild; // ת + cout<data<<" "; // ʺ̽ + } + p=p->rchild; // ޺ת + } +} + +template // ʾ +void DispBiTree(ThrBTNode
* bt,int level) +{ + if(bt) // նʾ + { DispBiTree(bt->rchild,level+1); // ʾ + cout<data; // ʾڵ + DispBiTree(bt->lchild,level+1); // ʾ + cout< +void DestroyThrBiTree(ThrBTNode
*&bt) +{ + if(bt) + { + DestroyThrBiTree(bt->lchild); + DestroyThrBiTree(bt->rchild); + delete bt; + } +} + +void dispmenu() +{ // ʾ˵ + cout< *bt; + + + system("cls"); // + + int choice; + do + { + dispmenu(); //ʾ˵ + cout<<"Enter choice(1~7):"; + cin>>choice; + switch(choice) + { + case 1: // + cout<<"ݲο"< +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "Mgraph.h" +#include +using namespace std; + +// ͼ + +void DispMenu() +{ + cout<<"\n ѡҪIJ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDG(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺ͼΪ"<>v; + InsertVex(G,v); + cout<<"붥ӦߺͼΪ"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ"<>u>>v; + if(InsertArc(G,u,v)) + cout<<"ߺͼΪ"<>u>>v; + DeleteArc(G,u,v); + cout<<"ߺͼΪ"< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͱ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateUDG(MGraph
&G) +{ + int i,j,k; + DT v1,v2; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 ߵڽӵ + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==0 ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ"<>w; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w); + else + cout<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w) // ֵΪvwӱ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + { + cout<<"\n㲻ڻͬܲ룡"< +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ߲ڣfalse + { + cout<<"߲ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "Mgraph.h" +#include +using namespace std; + +// + +void DispMenu() +{ + cout<<"\n ѡҪIJ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // + CreateUDN(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺"<>v; + InsertVex(G,v); + cout<<"붥Ӧߺ"<>v; + DeleteVex(G,v); + cout<<"ɾ"<>u>>v>>weight; + if(InsertArc(G,u,v,weight)) // ɹʾ + cout<<"ߺ"<>u>>v; + DeleteArc(G,u,v); + cout<<"ߺ"< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // ϢȨֵΪ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͱ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateUDN(MGraph
&G) +{ + int i,j,k,weight; + DT v1,v2; + cout<<"Ķ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ı "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 ߵڽӵͱȨֵ + cin>>weight; + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) // ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==INF ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j,weight; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵͱߵȨֵ"<>w>>weight; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w,weight); + else + cout<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w,int weight) // ֵΪvwӱ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + { + cout<<"\n㲻ڣͬܲ룡"< +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ߲ڣfalse + { + cout<<"߲ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // v + cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "Mgraph.h" +#include +using namespace std; + +// ͼ + +void DispMenu() +{ + cout<<"\n ѡҪIJ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateDG(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺ͼΪ"<>v; + InsertVex(G,v); + cout<<"붥ӦĻͼ"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ"<>u>>v; + if(InsertArc(G,u,v)) + cout<<"뻡ͼ"<>u>>v; + DeleteArc(G,u,v); + cout<<"ɾͼ"< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͻ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateDG(MGraph
&G) +{ + int i,j,k; + DT v1,v2; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼĻ "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 뻡ڽӵͻȨֵ + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==0 ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"뻡βͷ"<>v>>w; + InsertArc(G,v,w); + cout<<"Y/N)?"<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w) // ֵΪvwӻ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + { + cout<<"\n㲻ڻ򲻺ܲ룡"< +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ڣfalse + { + cout<<"ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "Mgraph.h" +#include +using namespace std; + +// + +void DispMenu() +{ + cout<<"\n ѡҪIJ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // + CreateDN(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺Ϊ"<>v; + InsertVex(G,v); + cout<<"붥ӦߺΪ"<>v; + DeleteVex(G,v); + cout<<"ɾΪ"<>u>>v>>weight; + if(InsertArc(G,u,v,weight)) + cout<<"ߺΪ"<>u>>v; + DeleteArc(G,u,v); + cout<<"ߺΪ"< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͻ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateDN(MGraph
&G) +{ + int i,j,k,weight; + DT v1,v2; + cout<<"Ķ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"Ļ "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 뻡ڽӵͻȨֵ + cin>>weight; + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==INF ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j,weight; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"½ĻβͷȨֵ"<>v>>w>>weight; + InsertArc(G,v,w,weight); + cout<<"Y/N)?"<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w,int weight) // ֵΪvwӻ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + { + cout<<"\n㲻ڻ򲻺ܲ룡"< +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ڣfalse + { + cout<<"ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +#include "LinkQueue.h" +#include "ALGraph.h" +#include +using namespace std; + +void DispMenu() +{ + cout<<"\n ѡ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDG(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޸ĺͼ"<>v; + InsertVex(G,v); + cout<<"붥Ӧߺͼ:"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ:"<>u>>v; + InsertArc(G,u,v); + cout<<"ߺͼ:"<>u>>v; + DeleteArc(G,u,v); + cout<<"ɾͼ:"< +struct VNode{ + DT data; // Ϣ + ArcNode *firstarc; // ָһöָ +}; + +template +struct ALGraph{ + VNode
vertices[MAX_VEXNUM]; // 㼯 + int vexnum; // + int arcnum; // +}; + +template +void DispG(ALGraph
G) +{ + int i; + ArcNode *p; + cout<adjvex) // ʱһ߱ + { + cout<<"("<adjvex].data<<")"<<'\t'; + } + p = p->nextarc; + } + } + cout< +int LocateVex(ALGraph
G, DT v) +{ + for(int i=0;i +void CreateUDG(ALGraph
&G) +{ + int i,j,k; + DT v1,v2; + ArcNode *p; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vertices[i].data; + G.vertices[i].firstarc = NULL; + } + for(k=0;k>v1>>v2; + i = LocateVex(G,v1); + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"<adjvex = j; + p->nextarc = G.vertices[i].firstarc; // v1ͷ±߽ + G.vertices[i].firstarc = p; + p = new ArcNode; // һµı + p->adjvex = i; + p->nextarc = G.vertices[j].firstarc; // v2ͷ±߽ + G.vertices[j].firstarc = p; + } +} + + +template +void DestroyGraph(ALGraph
G) // ٱ߽ +{ + int i; + ArcNode *p,*q; + for(i = 0;inextarc; + delete p; // ɾ߽ + p = q; + } + } + G.arcnum = 0; + G.vexnum = 0; +} + +template +bool GetVex(ALGraph
G, int k,DT &v) // ȡ k ֵ +{ + if(k<0||k>=G.vexnum) // 㲻ڣfalse + return false; + v=G.vertices[k].data; // ڣȡ k ֵ + return true; // true +} + +template +bool PutVex(ALGraph
&G, DT &u,DT v) // ޸Ķ u ֵ +{ + int k = LocateVex(G,u); + if(k<0) // u + return false; + G.vertices[k].data = v; // öuֵ + return true; +} + +template +int FirstAdjVex(ALGraph
G, int u) // 󶥵 u ĵһڽӵ +{ + ArcNode * p; + if(u<0 || u>=G.vexnum) // u + return -1; + p = G.vertices[u].firstarc; // uڣڽӵ㣬p Ϊһڽӵλ + if(p) + { + return p->adjvex; + } + else + { + return -1; + } +} + +template +int NextAdjVex(ALGraph
G, int u,int w) // 󶥵 u w һڽӵ +{ + ArcNode *p; + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum ) // + return -1; + p = G.vertices[u].firstarc; // u ı + while( p && (p->adjvex!=w)) // w ߽ + { + p = p->nextarc; + } + if(!p||!p->nextarc) // ûҵ w w һ + return -1; + else // 򣬷һڽӵλ + { + return p->nextarc->adjvex; + } +} + +template +bool InsertVex(ALGraph
&G, DT v) // Ӷ +{ + char ans; + DT w; + int j; + if(G.vexnum > MAX_VEXNUM) // Ϊඥ¶ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ:"<>w; + j=LocateVex(G,w); + if(j<0) + cout<<"㲻ڣܴߣ"<>ans; + } + return true; +} + + +template +bool InsertArc(ALGraph
&G, DT v,DT w) // ӱ(v,w) +{ + ArcNode *p; + int i,j; + i = LocateVex(G,v); + j = LocateVex(G,w); + if(i<0||j<0 || i==j) // 㲻ڻ˵ͬ,ܲ + { + cout<<"\n㲻ڻͬܲ룡"<adjvex==j) + { + cout<<"ߴڣܲ룡"<nextarc; + } + G.arcnum++; // 1 + p = new ArcNode; + p->adjvex = j; + p->nextarc = G.vertices[i].firstarc; // (v,w)߽ڵiͷ + G.vertices[i].firstarc = p; + p = new ArcNode; + p->adjvex = i; // (w,v)߽ڵjͷ + p->nextarc = G.vertices[j].firstarc; + G.vertices[j].firstarc = p; + return true; +} + +template +bool DeleteArc(ALGraph
&G, DT v,DT w) // ɾ(v,w) +{ + ArcNode *p,*q; + int i,j; + cout<<"Hello DeleteArc!"<adjvex!=j) // p p ָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p&&p->adjvex ==j) // ҵ(v,w) + { + if(p == G.vertices[i].firstarc) // 1 ߽ + { + G.vertices[i].firstarc = p->nextarc; + } + else // ǵ 1 ߽ + { + q->nextarc = p->nextarc; + } + delete p; + G.arcnum--; + p = G.vertices[j].firstarc; + while(p&&p->adjvex!=i) // pqָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p == G.vertices[j].firstarc) // 1 ߽ + { + G.vertices[j].firstarc = p->nextarc; + } + else // ǵ1߽ + { + q->nextarc = p->nextarc; + } + delete p; + } + cout<<"Bye-bye DeleteArc!"< +bool DeleteVex(ALGraph
&G, DT v) // ɾv +{ + int i,j; + ArcNode *p; + DT w; + i = LocateVex(G,v); + if(i<0) + { + cout<<"㲻ڣ"<adjvex; + GetVex(G,j,w); + DeleteArc(G,v,w); + p=G.vertices[i].firstarc; + } + for(j=i+1;j +void DFS(ALGraph
G, int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ʶѷ + cout<=0;w=NextAdjVex(G,v,w)) // vڽӵw + { + if(!visited[w]) // δʣDFS + DFS(G,w); + } +} + +// 㷨6.9 +template +void DFSTraverse(ALGraph
G) // ڽӱ洢ͼȱ +{ + int i ; + for(i = 0;i +void BFS(ALGraph
G, int v) // ͨͼĹȱ +{ + int w; + ArcNode *p; + LinkQueue Q; + InitQueue(Q); + cout<adjvex; + if(!visited[w]) + { + cout<nextarc; + } + } +} + + +template +bool BFSTraverse(ALGraph
G) // ڽӱ洢ͼĹȱ +{ + int i; + for(i = 0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "ALGraph.h" +#include +using namespace std; + +void DispMenu() +{ + cout<<"\n ѡ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDN(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޸ĺͼ"<>v; + InsertVex(G,v); + cout<<"붥Ӧߺͼ"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ:"<>u>>v>>weight; + InsertArc(G,u,v,weight); + cout<<"ߺͼΪ"<>u>>v; + DeleteArc(G,u,v); + cout<<"ߺͼΪ"< +struct VNode{ + DT data; // Ϣ + ArcNode *firstarc; // ָһöָ +}; + +template +struct ALGraph{ + VNode
vertices[MAX_VEXNUM]; // 㼯 + int vexnum; // + int arcnum; // +}; + +template +void DispG(ALGraph
G) +{ + int i; + ArcNode *p; + cout<adjvex) // ʱһ߱ + { + cout<<"("<adjvex].data<<","<weight<<")"<<'\t'; + } + p = p->nextarc; + } + } + cout< +int LocateVex(ALGraph
G, DT v) +{ + for(int i=0;i +void CreateUDN(ALGraph
&G) +{ + int i,j,k,weight; + DT v1,v2; + ArcNode *p; + cout<<"Ķ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ı "; + cin>>G.arcnum ; + cout<<""<>G.vertices[i].data; + G.vertices[i].firstarc = NULL; + } + for(k=0;k>v1>>v2>>weight; + i = LocateVex(G,v1); + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"<adjvex = j; + p->weight=weight; + p->nextarc = G.vertices[i].firstarc; // v1ͷ±߽ + G.vertices[i].firstarc = p; + p = new ArcNode; // һµı + p->adjvex = i; + p->weight=weight; + p->nextarc = G.vertices[j].firstarc; // v2ͷ±߽ + G.vertices[j].firstarc = p; + } +} + + +template +void DestroyGraph(ALGraph
G) // ٱ߽ +{ + int i; + ArcNode *p,*q; + for(i = 0;inextarc; + delete p; // ɾ߽ + p = q; + } + } + G.arcnum = 0; + G.vexnum = 0; +} + +template +bool GetVex(ALGraph
G, int k,DT &v) // ȡ k ֵ +{ + if(k<0||k>=G.vexnum) // 㲻ڣfalse + return false; + v=G.vertices[k].data; // ڣȡ k ֵ + return true; // true +} + +template +bool PutVex(ALGraph
&G, DT &u,DT v) // ޸Ķ u ֵ +{ + int k = LocateVex(G,u); + if(k<0) // u + return false; + G.vertices[k].data = v; // öuֵ + return true; +} + +template +int FirstAdjVex(ALGraph
G, int u) // 󶥵 u ĵһڽӵ +{ + ArcNode * p; + if(u<0 || u>=G.vexnum) // u + return -1; + p = G.vertices[u].firstarc; // uڣڽӵ㣬p Ϊһڽӵλ + if(p) + { + return p->adjvex; + } + else + { + return -1; + } +} + +template +int NextAdjVex(ALGraph
G, int u,int w) // 󶥵 u w һڽӵ +{ + ArcNode *p; + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum ) // + return -1; + p = G.vertices[u].firstarc; // u ı + while( p && (p->adjvex!=w)) // w ߽ + { + p = p->nextarc; + } + if(!p||!p->nextarc) // ûҵ w w һ + return -1; + else // 򣬷һڽӵλ + { + return p->nextarc->adjvex; + } +} + +template +bool InsertVex(ALGraph
&G, DT v) // Ӷ +{ + int j,weight; + char ans; + DT w; + if(G.vexnum > MAX_VEXNUM) // Ϊඥ¶ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵͱȨֵ:"<>w>>weight; + j=LocateVex(G,w); + if(j<0) + cout<<"㲻ڣܴߣ"<>ans; + }; + return true; +} + + +template +bool InsertArc(ALGraph
&G, DT v,DT w,int weight) // ӱ(v,w) +{ + ArcNode *p; + int i,j; + i = LocateVex(G,v); + j = LocateVex(G,w); + if(i<0||j<0 || i==j) // 㲻ڻ˵ͬ,ܲ + { + cout<<"\n㲻ڻͬܲ룡"<adjvex==j) + { + cout<<"ߴڣܲ룡"<nextarc; + } + G.arcnum++; // 1 + p = new ArcNode; + p->adjvex = j; + p->weight=weight; + p->nextarc = G.vertices[i].firstarc; // (v,w)߽ڵiͷ + G.vertices[i].firstarc = p; + p = new ArcNode; + p->adjvex = i; // (w,v)߽ڵjͷ + p->weight=weight; + p->nextarc = G.vertices[j].firstarc; + G.vertices[j].firstarc = p; + return true; +} + +template +bool DeleteArc(ALGraph
&G, DT v,DT w) // ɾ(v,w) +{ + ArcNode *p,*q; + int i,j; + cout<<"Hello DeleteArc!"<adjvex!=j) // p p ָIJǴɾ߽ + { + q = p; + p = p->nextarc; + } + if(p&&p->adjvex ==j) // ҵ(v,w) + { + if(p == G.vertices[i].firstarc) // 1 ߽ + { + G.vertices[i].firstarc = p->nextarc; + } + else // ǵ 1 ߽ + { + q->nextarc = p->nextarc; + } + delete p; + G.arcnum--; + p = G.vertices[j].firstarc; + while(p&&p->adjvex!=i) // pqָIJǴɾ߽ + { + q = p; + p = p->nextarc; + } + if(p == G.vertices[j].firstarc) // 1 ߽ + { + G.vertices[j].firstarc = p->nextarc; + } + else // ǵ1߽ + { + q->nextarc = p->nextarc; + } + delete p; + } + cout<<"Bye-bye DeleteArc!"< +bool DeleteVex(ALGraph
&G, DT v) // ɾv +{ + int i,j; + ArcNode *p; + DT w; + i = LocateVex(G,v); + if(i<0) + { + cout<<"㲻ڣ"<adjvex; + GetVex(G,j,w); + DeleteArc(G,v,w); + p=G.vertices[i].firstarc; + } + for(j=i+1;j +void DFS(ALGraph
G, int v) // ͨȱ +{ + int w; + visited[v] = true; // ʶѷ + cout<=0;w=NextAdjVex(G,v,w)) // vڽӵw + { + if(!visited[w]) // δʣDFS + DFS(G,w); + } +} + +// 㷨6.9 +template +void DFSTraverse(ALGraph
G) // ڽӱ洢ȱ +{ + int i ; + for(i = 0;i +void BFS(ALGraph
G, int v) // ͨĹȱ +{ + int w; + ArcNode *p; + LinkQueue Q; + InitQueue(Q); + cout<adjvex; + if(!visited[w]) + { + cout<nextarc; + } + } +} + + +template +bool BFSTraverse(ALGraph
G) // ڽӱ洢Ĺȱ +{ + int i; + for(i = 0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "ALGraph.h" +#include +using namespace std; + + + +void DispMenu() +{ + cout<<"\n ѡ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateDN(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺ͼΪ"<>v; + InsertVex(G,v); + cout<<"붥ӦͼΪ"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ:"<>u>>v; + InsertArc(G,u,v); + cout<<"뻡ͼΪ"<>u>>v; + DeleteArc(G,u,v); + cout<<"㻡ͼΪ"< +struct VNode{ + DT data; // Ϣ + ArcNode *firstarc; // ָһöָ +}; + +template +struct ALGraph{ + VNode
vertices[MAX_VEXNUM]; // 㼯 + int vexnum; // + int arcnum; // +}; + +template +void DispG(ALGraph
G) +{ + int i; + ArcNode *p; + cout<adjvex].data<<")"<<'\t'; + p = p->nextarc; + } + } + cout< +int LocateVex(ALGraph
G, DT v) +{ + for(int i=0;i +void CreateDN(ALGraph
&G) +{ + int i,j,k; + DT v1,v2; + ArcNode *p; + cout<<"Ķ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"Ļ "; + cin>>G.arcnum ; + cout<<""<>G.vertices[i].data; + G.vertices[i].firstarc = NULL; + } + for(k=0;k>v1>>v2; + i = LocateVex(G,v1); + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"<adjvex = j; + p->nextarc = G.vertices[i].firstarc; // v1ͷ» + G.vertices[i].firstarc = p; + } +} + + +template +void DestroyGraph(ALGraph
G) // ٻ +{ + int i; + ArcNode *p,*q; + for(i = 0;inextarc; + delete p; // ɾ + p = q; + } + } + G.arcnum = 0; + G.vexnum = 0; +} + +template +bool GetVex(ALGraph
G, int k,DT &v) // ȡ k ֵ +{ + if(k<0||k>=G.vexnum) // 㲻ڣfalse + return false; + v=G.vertices[k].data; // ڣȡ k ֵ + return true; // true +} + +template +bool PutVex(ALGraph
&G, DT &u,DT v) // ޸Ķ u ֵ +{ + int k = LocateVex(G,u); + if(k<0) // u + return false; + G.vertices[k].data = v; // öuֵ + return true; +} + +template +int FirstAdjVex(ALGraph
G, int u) // 󶥵 u ĵһڽӵ +{ + ArcNode * p; + if(u<0 || u>=G.vexnum) // u + return -1; + p = G.vertices[u].firstarc; // uڣڽӵ㣬p Ϊһڽӵλ + if(p) + { + return p->adjvex; + } + else + { + return -1; + } +} + +template +int NextAdjVex(ALGraph
G, int u,int w) // 󶥵 u w һڽӵ +{ + ArcNode *p; + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum ) // + return -1; + p = G.vertices[u].firstarc; // u Ļ + while( p && (p->adjvex!=w)) // w + { + p = p->nextarc; + } + if(!p||!p->nextarc) // ûҵ w w һ + return -1; + else // 򣬷һڽӵλ + { + return p->nextarc->adjvex; + } +} + +template +bool InsertVex(ALGraph
&G, DT v) // Ӷ +{ + int j; + char ans; + DT w; + if(G.vexnum > MAX_VEXNUM) // Ϊඥ¶ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"뻡:"<>v>>w; + InsertArc(G,v,w); + cout<<"Y/N)?"<>ans; + }; + return true; +} + + +template +bool InsertArc(ALGraph
&G, DT v,DT w) // ӻ(v,w) +{ + ArcNode *p; + int i,j; + i = LocateVex(G,v); + j = LocateVex(G,w); + if(i<0||j<0 || i==j) // 㲻ڻ˵ͬ,ܲ + { + cout<<"\n㲻ڻͬܲ룡"<adjvex==j) + { + cout<<"ڣܲ룡"<nextarc; + } + G.arcnum++; // 1 + p = new ArcNode; + p->adjvex = j; + p->nextarc = G.vertices[i].firstarc; // (v,w)ڵiͷ + G.vertices[i].firstarc = p; + return true; +} + +template +bool DeleteArc(ALGraph
&G, DT v,DT w) // ɾ(v,w) +{ + ArcNode *p,*q; + int i,j; + cout<<"Hello DeleteArc!"<adjvex!=j) // p p ָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p&&p->adjvex ==j) // ҵ(v,w) + { + if(p == G.vertices[i].firstarc) // 1 + { + G.vertices[i].firstarc = p->nextarc; + } + else // ǵ 1 + { + q->nextarc = p->nextarc; + } + delete p; + G.arcnum--; + } +//cout<<"Bye-bye DeleteArc!"< +bool DeleteVex(ALGraph
&G, DT v) // ɾv +{ + int i,j; + ArcNode *p; + DT w; + i = LocateVex(G,v); + if(i<0) + { + cout<<"㲻ڣ"<adjvex; + GetVex(G,j,w); + DeleteArc(G,v,w); + p=G.vertices[i].firstarc; + } + for(j=i+1;j +void DFS(ALGraph
G, int v) // ͨȱ +{ + int w; + visited[v] = true; // ʶѷ + cout<=0;w=NextAdjVex(G,v,w)) // vڽӵw + { + if(!visited[w]) // δʣDFS + DFS(G,w); + } +} + +// 㷨6.9 +template +void DFSTraverse(ALGraph
G) // ڽӱ洢ȱ +{ + int i ; + for(i = 0;i +void BFS(ALGraph
G, int v) // ͨĹȱ +{ + int w; + ArcNode *p; + LinkQueue Q; + InitQueue(Q); + cout<adjvex; + if(!visited[w]) + { + cout<nextarc; + } + } +} + + +template +bool BFSTraverse(ALGraph
G) // ڽӱ洢Ĺȱ +{ + int i; + for(i = 0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "ALGraph.h" +#include +using namespace std; + +void DispMenu() +{ + cout<<"\n ѡ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDN(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺ͼΪ"<>v; + InsertVex(G,v); + cout<<"붥ӦͼΪ"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ:"<>u>>v>>weight; + InsertArc(G,u,v,weight); + cout<<"뻡ͼΪ"<>u>>v; + DeleteArc(G,u,v); + cout<<"㻡ͼΪ"< +struct VNode{ + DT data; // Ϣ + ArcNode *firstarc; // ָһöָ +}; + +template +struct ALGraph{ + VNode
vertices[MAX_VEXNUM]; // 㼯 + int vexnum; // + int arcnum; // +}; + +template +void DispG(ALGraph
G) +{ + int i; + ArcNode *p; + cout<adjvex].data<<","<weight<<")"<<'\t'; + p = p->nextarc; + } + } + cout< +int LocateVex(ALGraph
G, DT v) +{ + for(int i=0;i +void CreateUDN(ALGraph
&G) +{ + int i,j,k,weight; + DT v1,v2; + ArcNode *p; + cout<<"Ķ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"Ļ "; + cin>>G.arcnum ; + cout<<""<>G.vertices[i].data; + G.vertices[i].firstarc = NULL; + } + for(k=0;k>v1>>v2>>weight; + i = LocateVex(G,v1); + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"<adjvex = j; + p->weight=weight; + p->nextarc = G.vertices[i].firstarc; // v1ͷ» + G.vertices[i].firstarc = p; + } +} + + +template +void DestroyGraph(ALGraph
G) // ٻ +{ + int i; + ArcNode *p,*q; + for(i = 0;inextarc; + delete p; // ɾ + p = q; + } + } + G.arcnum = 0; + G.vexnum = 0; +} + +template +bool GetVex(ALGraph
G, int k,DT &v) // ȡ k ֵ +{ + if(k<0||k>=G.vexnum) // 㲻ڣfalse + return false; + v=G.vertices[k].data; // ڣȡ k ֵ + return true; // true +} + +template +bool PutVex(ALGraph
&G, DT &u,DT v) // ޸Ķ u ֵ +{ + int k = LocateVex(G,u); + if(k<0) // u + return false; + G.vertices[k].data = v; // öuֵ + return true; +} + +template +int FirstAdjVex(ALGraph
G, int u) // 󶥵 u ĵһڽӵ +{ + ArcNode * p; + if(u<0 || u>=G.vexnum) // u + return -1; + p = G.vertices[u].firstarc; // uڣڽӵ㣬p Ϊһڽӵλ + if(p) + { + return p->adjvex; + } + else + { + return -1; + } +} + +template +int NextAdjVex(ALGraph
G, int u,int w) // 󶥵 u w һڽӵ +{ + ArcNode *p; + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum ) // + return -1; + p = G.vertices[u].firstarc; // u Ļ + while( p && (p->adjvex!=w)) // w + { + p = p->nextarc; + } + if(!p||!p->nextarc) // ûҵ w w һ + return -1; + else // 򣬷һڽӵλ + { + return p->nextarc->adjvex; + } +} + +template +bool InsertVex(ALGraph
&G, DT v) // Ӷ +{ + int j,weight; + char ans; + DT w; + if(G.vexnum > MAX_VEXNUM) // Ϊඥ¶ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"뻡Ȩֵ:"<>v>>w>>weight; + InsertArc(G,v,w,weight); + cout<<"Y/N)?"<>ans; + }; + return true; +} + + +template +bool InsertArc(ALGraph
&G, DT v,DT w,int weight) // ӻ(v,w) +{ + ArcNode *p; + int i,j; + i = LocateVex(G,v); + j = LocateVex(G,w); + if(i<0||j<0 || i==j) // 㲻ڻ˵ͬ,ܲ + { + cout<<"\n㲻ڻͬܲ룡"<adjvex==j) + { + cout<<"ڣܲ룡"<nextarc; + } + G.arcnum++; // 1 + p = new ArcNode; + p->adjvex = j; + p->weight=weight; + p->nextarc = G.vertices[i].firstarc; // (v,w)ڵiͷ + G.vertices[i].firstarc = p; + return true; +} + +template +bool DeleteArc(ALGraph
&G, DT v,DT w) // ɾ(v,w) +{ + ArcNode *p,*q; + int i,j; + cout<<"Hello DeleteArc!"<adjvex!=j) // p p ָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p&&p->adjvex ==j) // ҵ(v,w) + { + if(p == G.vertices[i].firstarc) // 1 + { + G.vertices[i].firstarc = p->nextarc; + } + else // ǵ 1 + { + q->nextarc = p->nextarc; + } + delete p; + G.arcnum--; + } +//cout<<"Bye-bye DeleteArc!"< +bool DeleteVex(ALGraph
&G, DT v) // ɾv +{ + int i,j; + ArcNode *p; + DT w; + i = LocateVex(G,v); + if(i<0) + { + cout<<"㲻ڣ"<adjvex; + GetVex(G,j,w); + DeleteArc(G,v,w); + p=G.vertices[i].firstarc; + } + for(j=i+1;j +void DFS(ALGraph
G, int v) // ͨȱ +{ + int w; + visited[v] = true; // ʶѷ + cout<=0;w=NextAdjVex(G,v,w)) // vڽӵw + { + if(!visited[w]) // δʣDFS + DFS(G,w); + } +} + +// 㷨6.9 +template +void DFSTraverse(ALGraph
G) // ڽӱ洢ȱ +{ + int i ; + for(i = 0;i +void BFS(ALGraph
G, int v) // ͨĹȱ +{ + int w; + ArcNode *p; + LinkQueue Q; + InitQueue(Q); + cout<adjvex; + if(!visited[w]) + { + cout<nextarc; + } + } +} + + +template +bool BFSTraverse(ALGraph
G) // ڽӱ洢Ĺȱ +{ + int i; + for(i = 0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< + +#include + +#include "MGraph.h" +#include "LinkQueue.h" +using namespace std; + +void DispMenu() +{ + cout<<"\nѡҪIJ"< +bool IsConected(MGraph
G, int i, int j) +{ + int k; + for(k=0;k +bool IsGraphConected(MGraph
G) +{ + int i,flag=1; + for(i=0;i G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDG(G); + cout<>u>>v; + i=LocateVex(G,u); + j=LocateVex(G,v); + if(i==-1 ||j==-1 || i==j) // 㲻 + cout<<"\n㲻ڻͬ "< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͱ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateUDG(MGraph
&G) +{ + int i,j,k; + DT v1,v2; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 ߵڽӵ + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==0 ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ"<>w; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w); + else + cout<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w) // ֵΪvwӱ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + return false; // ܲߣfalse + G.arcs[i][j]=1; + G.arcs[j][i]=1; + G.arcnum++; + return true; +} + +template +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ߲ڣfalse + { + cout<<"߲ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + //cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +#include +#include "ALGraph.h" +#include "LinkQueue.h" +using namespace std; + +void DispMenu()ͼͨϢ +{ + cout<<"\nѡҪIJ"< +void DFS3(ALGraph
G,int v) // ӱ洢ͼȱ +{ + int w; + ArcNode* p; + visited[v]=true; // ʱ־ + cout<adjvex; // δʹ + if(!visited[w]) + DFS3(G,w); // DFS + p=p->nextarc; // 㣬DFS + } +} + +template +void ConnectVex(ALGraph
G) // ͼͨϢ +{ + int k,num=0; // numͨ + for(k=0;k +int Maxdist( ALGraph
G, int v) +{ + int i,w; + ArcNode *p; + LinkQueue Q; // һ + InitQueue(Q); + for(i=0;iadjvex; + if(!visited[w]) // δ + { + visited[w]=true; // ʱ־ + EnQueue(Q,w); // + } + p=p->nextarc; // һڽӵ + } + } + return v; +} + + +bool visited[MAX_VEXNUM]={false}; + +void main() +{ + char u,v; + int k; + ALGraph G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDG(G); + cout<>v; + k=LocateVex(G,v); + if(k==-1) + cout<<"\n"< +struct VNode{ + DT data; // Ϣ + ArcNode *firstarc; // ָһöָ +}; + +template +struct ALGraph{ + VNode
vertices[MAX_VEXNUM]; //㼯 + int vexnum;// + int arcnum;// +}; + +template +void DispG(ALGraph
G) +{ + int i; + ArcNode *p; + cout<adjvex) //ʱһ߱ + { + cout<<"("<adjvex].data<<")"<<'\t'; + } + p = p->nextarc; + } + } + cout< +int LocateVex(ALGraph
G, DT v) +{ + for(int i=0;i +void CreateUDG(ALGraph
&G) +{ + int i,j,k; + DT v1,v2; + ArcNode *p; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vertices[i].data; + G.vertices[i].firstarc = NULL; + } + for(k=0;k>v1>>v2; + i = LocateVex(G,v1); + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"<adjvex = j; + p->nextarc = G.vertices[i].firstarc; // v1ͷ±߽ + G.vertices[i].firstarc = p; + p = new ArcNode; // һµı + p->adjvex = i; + p->nextarc = G.vertices[j].firstarc; // v2ͷ±߽ + G.vertices[j].firstarc = p; + } +} + + +template +void DestroyGraph(ALGraph
G) // ٱ߽ +{ + int i; + ArcNode *p,*q; + for(i = 0;inextarc; + delete p; // ɾ߽ + p = q; + } + } + G.arcnum = 0; + G.vexnum = 0; +} + +template +bool GetVex(ALGraph
G, int k,DT &v) // ȡ k ֵ +{ + if(k<0||k>=G.vexnum) // 㲻ڣfalse + return false; + v=G.vertices[k].data; // ڣȡ k ֵ + return true; // true +} + +template +bool PutVex(ALGraph
&G, DT &u,DT v) // ޸Ķ u ֵ +{ + int k = LocateVex(G,u); + if(k<0) // u + return false; + G.vertices[k].data = v; // öuֵ + return true; +} + +template +int FirstAdjVex(ALGraph
G, int u) // 󶥵 u ĵһڽӵ +{ + ArcNode * p; + if(u<0 || u>=G.vexnum) // u + return -1; + p = G.vertices[u].firstarc; // uڣڽӵ㣬p Ϊһڽӵλ + if(p) + { + return p->adjvex; + } + else + { + return -1; + } +} + +template +int NextAdjVex(ALGraph
G, int u,int w) // 󶥵 u w һڽӵ +{ + ArcNode *p; + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum ) // + return -1; + p = G.vertices[u].firstarc; // u ı + while( p && (p->adjvex!=w)) // w ߽ + { + p = p->nextarc; + } + if(!p||!p->nextarc) // ûҵ w w һ + return -1; + else // 򣬷һڽӵλ + { + return p->nextarc->adjvex; + } +} + +template +bool InsertVex(ALGraph
&G, DT v) // Ӷ +{ + int j; + char ans; + DT w; + if(G.vexnum > MAX_VEXNUM) // Ϊඥ¶ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ:"<>w; + j=LocateVex(G,w); + if(j>=0) // ڣӱ + InsertArc(G,v,w); + else + cout<>ans; + }; + return true; +} + + +template +bool InsertArc(ALGraph
&G, DT v,DT w) // ӱ(v,w) +{ + ArcNode *p; + int i,j; + i = LocateVex(G,v); + j = LocateVex(G,w); + if(i<0||j<0 || i==j) // 㲻ڻ˵ͬ,ܲ + { + cout<<"\n㲻ڻͬܲ룡"<adjvex==j) + { + cout<<"ߴڣܲ룡"<nextarc; + } + G.arcnum++; // 1 + p = new ArcNode; + p->adjvex = j; + p->nextarc = G.vertices[i].firstarc; // (v,w)߽ڵiͷ + G.vertices[i].firstarc = p; + p = new ArcNode; + p->adjvex = i; // (w,v)߽ڵjͷ + p->nextarc = G.vertices[j].firstarc; + G.vertices[j].firstarc = p; + return true; +} + +template +bool DeleteArc(ALGraph
&G, DT v,DT w) // ɾ(v,w) +{ + ArcNode *p,*q; + int i,j; + cout<<"Hello DeleteArc!"<adjvex!=j) // p p ָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p&&p->adjvex ==j) // ҵ(v,w) + { + if(p == G.vertices[i].firstarc) // 1 ߽ + { + G.vertices[i].firstarc = p->nextarc; + } + else // ǵ 1 ߽ + { + q->nextarc = p->nextarc; + } + delete p; + G.arcnum--; + p = G.vertices[j].firstarc; + while(p&&p->adjvex!=i) // pqָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p == G.vertices[j].firstarc) // 1 ߽ + { + G.vertices[j].firstarc = p->nextarc; + } + else // ǵ1߽ + { + q->nextarc = p->nextarc; + } + delete p; + } + cout<<"Bye-bye DeleteArc!"< +bool DeleteVex(ALGraph
&G, DT v) // ɾv +{ + int i,j; + ArcNode *p; + DT w; + i = LocateVex(G,v); + if(i<0) + { + cout<<"㲻ڣ"<adjvex; + GetVex(G,j,w); + DeleteArc(G,v,w); + p=G.vertices[i].firstarc; + } + for(j=i+1;j +void DFS(ALGraph
G, int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ʶѷ + cout<=0;w=NextAdjVex(G,v,w)) // vڽӵw + { + if(!visited[w]) // δʣDFS + DFS(G,w); + } +} + +// 㷨6.9 +template +void DFSTraverse(ALGraph
G) // ڽӱ洢ͼȱ +{ + int i ; + for(i = 0;i +void BFS(ALGraph
G, int v) // ͨͼĹȱ +{ + int w; + ArcNode *p; + LinkQueue Q; + InitQueue(Q); + cout<adjvex; + if(!visited[w]) + { + cout<nextarc; + } + } +} + + +template +bool BFSTraverse(ALGraph
G) // ڽӱ洢ͼĹȱ +{ + int i; + for(i = 0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include +#include "ALGraph.h" +#include "LinkQueue.h" +using namespace std; + +void DispMenu()ͼͨϢ +{ + cout<<"\nѡҪIJ"< +void DFS3(ALGraph
G,int v) // ӱ洢ͼȱ +{ + int w; + ArcNode* p; + visited[v]=true; // ʱ־ + cout<adjvex; // δʹ + if(!visited[w]) + DFS3(G,w); // DFS + p=p->nextarc; // 㣬DFS + } +} + +template +void ConnectVex(ALGraph
G) // ͼͨϢ +{ + int k,num=0; // numͨ + for(k=0;k +int Maxdist( ALGraph
G, int v) +{ + int i,w; + ArcNode *p; + LinkQueue Q; // һ + InitQueue(Q); + for(i=0;iadjvex; + if(!visited[w]) // δ + { + visited[w]=true; // ʱ־ + EnQueue(Q,w); // + } + p=p->nextarc; // һڽӵ + } + } + return v; +} + + +bool visited[MAX_VEXNUM]={false}; + +void main() +{ + char u,v; + int k; + ALGraph G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDG(G); + cout<>v; + k=LocateVex(G,v); + if(k==-1) + cout<<"\n"< +struct VNode{ + DT data; // Ϣ + ArcNode *firstarc; // ָһöָ +}; + +template +struct ALGraph{ + VNode
vertices[MAX_VEXNUM]; //㼯 + int vexnum;// + int arcnum;// +}; + +template +void DispG(ALGraph
G) +{ + int i; + ArcNode *p; + cout<adjvex) //ʱһ߱ + { + cout<<"("<adjvex].data<<")"<<'\t'; + } + p = p->nextarc; + } + } + cout< +int LocateVex(ALGraph
G, DT v) +{ + for(int i=0;i +void CreateUDG(ALGraph
&G) +{ + int i,j,k; + DT v1,v2; + ArcNode *p; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vertices[i].data; + G.vertices[i].firstarc = NULL; + } + for(k=0;k>v1>>v2; + i = LocateVex(G,v1); + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"<adjvex = j; + p->nextarc = G.vertices[i].firstarc; // v1ͷ±߽ + G.vertices[i].firstarc = p; + p = new ArcNode; // һµı + p->adjvex = i; + p->nextarc = G.vertices[j].firstarc; // v2ͷ±߽ + G.vertices[j].firstarc = p; + } +} + + +template +void DestroyGraph(ALGraph
G) // ٱ߽ +{ + int i; + ArcNode *p,*q; + for(i = 0;inextarc; + delete p; // ɾ߽ + p = q; + } + } + G.arcnum = 0; + G.vexnum = 0; +} + +template +bool GetVex(ALGraph
G, int k,DT &v) // ȡ k ֵ +{ + if(k<0||k>=G.vexnum) // 㲻ڣfalse + return false; + v=G.vertices[k].data; // ڣȡ k ֵ + return true; // true +} + +template +bool PutVex(ALGraph
&G, DT &u,DT v) // ޸Ķ u ֵ +{ + int k = LocateVex(G,u); + if(k<0) // u + return false; + G.vertices[k].data = v; // öuֵ + return true; +} + +template +int FirstAdjVex(ALGraph
G, int u) // 󶥵 u ĵһڽӵ +{ + ArcNode * p; + if(u<0 || u>=G.vexnum) // u + return -1; + p = G.vertices[u].firstarc; // uڣڽӵ㣬p Ϊһڽӵλ + if(p) + { + return p->adjvex; + } + else + { + return -1; + } +} + +template +int NextAdjVex(ALGraph
G, int u,int w) // 󶥵 u w һڽӵ +{ + ArcNode *p; + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum ) // + return -1; + p = G.vertices[u].firstarc; // u ı + while( p && (p->adjvex!=w)) // w ߽ + { + p = p->nextarc; + } + if(!p||!p->nextarc) // ûҵ w w һ + return -1; + else // 򣬷һڽӵλ + { + return p->nextarc->adjvex; + } +} + +template +bool InsertVex(ALGraph
&G, DT v) // Ӷ +{ + int j; + char ans; + DT w; + if(G.vexnum > MAX_VEXNUM) // Ϊඥ¶ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ:"<>w; + j=LocateVex(G,w); + if(j>=0) // ڣӱ + InsertArc(G,v,w); + else + cout<>ans; + }; + return true; +} + + +template +bool InsertArc(ALGraph
&G, DT v,DT w) // ӱ(v,w) +{ + ArcNode *p; + int i,j; + i = LocateVex(G,v); + j = LocateVex(G,w); + if(i<0||j<0 || i==j) // 㲻ڻ˵ͬ,ܲ + { + cout<<"\n㲻ڻͬܲ룡"<adjvex==j) + { + cout<<"ߴڣܲ룡"<nextarc; + } + G.arcnum++; // 1 + p = new ArcNode; + p->adjvex = j; + p->nextarc = G.vertices[i].firstarc; // (v,w)߽ڵiͷ + G.vertices[i].firstarc = p; + p = new ArcNode; + p->adjvex = i; // (w,v)߽ڵjͷ + p->nextarc = G.vertices[j].firstarc; + G.vertices[j].firstarc = p; + return true; +} + +template +bool DeleteArc(ALGraph
&G, DT v,DT w) // ɾ(v,w) +{ + ArcNode *p,*q; + int i,j; + cout<<"Hello DeleteArc!"<adjvex!=j) // p p ָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p&&p->adjvex ==j) // ҵ(v,w) + { + if(p == G.vertices[i].firstarc) // 1 ߽ + { + G.vertices[i].firstarc = p->nextarc; + } + else // ǵ 1 ߽ + { + q->nextarc = p->nextarc; + } + delete p; + G.arcnum--; + p = G.vertices[j].firstarc; + while(p&&p->adjvex!=i) // pqָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p == G.vertices[j].firstarc) // 1 ߽ + { + G.vertices[j].firstarc = p->nextarc; + } + else // ǵ1߽ + { + q->nextarc = p->nextarc; + } + delete p; + } + cout<<"Bye-bye DeleteArc!"< +bool DeleteVex(ALGraph
&G, DT v) // ɾv +{ + int i,j; + ArcNode *p; + DT w; + i = LocateVex(G,v); + if(i<0) + { + cout<<"㲻ڣ"<adjvex; + GetVex(G,j,w); + DeleteArc(G,v,w); + p=G.vertices[i].firstarc; + } + for(j=i+1;j +void DFS(ALGraph
G, int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ʶѷ + cout<=0;w=NextAdjVex(G,v,w)) // vڽӵw + { + if(!visited[w]) // δʣDFS + DFS(G,w); + } +} + +// 㷨6.9 +template +void DFSTraverse(ALGraph
G) // ڽӱ洢ͼȱ +{ + int i ; + for(i = 0;i +void BFS(ALGraph
G, int v) // ͨͼĹȱ +{ + int w; + ArcNode *p; + LinkQueue Q; + InitQueue(Q); + cout<adjvex; + if(!visited[w]) + { + cout<nextarc; + } + } +} + + +template +bool BFSTraverse(ALGraph
G) // ڽӱ洢ͼĹȱ +{ + int i; + for(i = 0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< + +#include + +#include "MGraph.h" +#include "LinkQueue.h" +using namespace std; + +void DispMenu() +{ + cout<<"\nѡҪIJ"< +bool IsConected(MGraph
G, int i, int j) +{ + int k; + for(k=0;k +bool IsGraphConected(MGraph
G) +{ + int i,flag=1; + for(i=0;i G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // ͼ + CreateUDG(G); + cout<>u>>v; + i=LocateVex(G,u); + j=LocateVex(G,v); + if(i==-1 ||j==-1 || i==j) // 㲻 + cout<<"\n㲻ڻͬ "< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͱ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateUDG(MGraph
&G) +{ + int i,j,k; + DT v1,v2; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 ߵڽӵ + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==0 ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ"<>w; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w); + else + cout<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w) // ֵΪvwӱ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + return false; // ܲߣfalse + G.arcs[i][j]=1; + G.arcs[j][i]=1; + G.arcnum++; + return true; +} + +template +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ߲ڣfalse + { + cout<<"߲ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + //cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͱ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateUDN(MGraph
&G) +{ + int i,j,k,weight; + DT v1,v2; + cout<<"Ķ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ı "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 ߵڽӵͱȨֵ + cin>>weight; + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==INF ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j,weight; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵͱߵȨֵ"<>w>>weight; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w,weight); + else + cout<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w,int weight) // ֵΪvwӱ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + { + cout<<"\n㲻ڣͬܲ룡"< +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ߲ڣfalse + { + cout<<"߲ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +#include "LinkQueue.h" +#include "Mgraph.h" +#include +using namespace std; + +const MAX_ARCNUM=50; + +// С +//㷨6.18 Prim㷨 +struct CEdge // ѡߴ洢 +{ + int adjvex; // Uмϵ + int lowcost; // СߵȨֵ +}; + +int minEdge(CEdge closeEdge[],int n) +{ + int i, k=0; + int min = INF; + for ( i=0; i +bool Prim_MST(MGraph
G, DT v) // ӶvʼС +{ + CEdge closeEdge[MAX_VEXNUM]; + int i,j,k,mincost = 0; + k=LocateVex(G,v); + if(k==-1) // 㲻 + { + cout<<"㲻ڣ"<edge[j+1].cost) // 2.1Ԫ򣬻λ + { + temp=edge[j];edge[j]=edge[j+1];edge[j+1]=temp; // R[j]<-->R[j+1] + exchange=true; // 2.2 ־Ϊtrue + } + } + } +} + +int parent[MAX_VEXNUM]; +void Connect(int &x,int &y) // ͨʶ +{ + if(x +bool Kruskal_MST(MGraph
G) // ɴ +{ + int mincost=0,Num_Edge=0; + char u,v; + int i,j,k=0; + Edge edge[MAX_ARCNUM]; + for(i=0;i G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: // + CreateUDN(G); + cout<>v; + Prim_MST(G,v); + break; + case 3: // Kruskal㷨 + Kruskal_MST(G); + cout< +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͻ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateDN(MGraph
&G) +{ + int i,j,k,weight; + DT v1,v2; + cout<<"Ķ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"Ļ "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 뻡ڽӵͻȨֵ + cin>>weight; + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjvex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjvex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==INF ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j,weight; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵͻȨֵ"<>w>>weight; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w,weight); + else + cout<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w,int weight) // ֵΪvwӻ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + { + cout<<"\n㲻ڻѴڣܲ룡"< +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ڣfalse + { + cout<<"ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS2(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; // һ + InitQueue(Q); + cout<=0;w=NextAdjvex(G,v,w)) // vڽӵ + if(!visited[w]) // δ + { + cout< +void BFSTraverse(MGraph
G) // ȱ +{ + int i; + for(i=0;i +#include "LinkQueue.h" +#include "Mgraph.h" +#include +using namespace std; + +const MAX_ARCNUM=50; + +//㷨6.20 Dijkstra㷨 +template +void ShortestPath_DIJ(MGraph
G, int v0) // ӶvʼС +{ + int v,i,w,min; + bool S[MAX_VEXNUM]={false}; // SеĶ + int D[MAX_VEXNUM]; // Դ㵽ľ + int P[MAX_VEXNUM]={-1}; // Դ㻥·Ϣ + for(v=0;v +void ShortestPath_Floyd(MGraph
G) // ɴ +{ + int k,i,j; + int D[MAX_VEXNUM][MAX_VEXNUM]; // D[i][j]ʾiͶj̾ + int P[MAX_VEXNUM][MAX_VEXNUM]; // ̾·Ϣ + for(i=0;i G,G1,G2; + G1.vexnum=5; // Dijkstra㷨ͼ + G1.arcnum=7; + G1.vexs[0]='A';G1.vexs[1]='B';G1.vexs[2]='C'; + G1.vexs[3]='D';G1.vexs[4]='E'; + for(i=0;i>choice; + switch(choice) + { + case 1: // + f=true; + CreateDN(G); + cout<>w; + v0=LocateVex(G1,w); + if(v0==-1) + cout<<"㲻ڣ"< +#include "LinkQueue.h" +#include "ALGraph.h" +#include +using namespace std; + +void DispMenu() +{ + cout<<"ѡҪIJ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: + CreateUDG(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵ֵ: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺ͼΪ"<>v; + InsertVex(G,v); + cout<<"붥ӦߺͼΪ"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ:"<>u>>v; + InsertArc(G,u,v); + cout<<"ߺͼΪ"<>u>>v; + DeleteArc(G,u,v); + cout<<"ߺͼΪ"< +struct VNode{ + DT data; //Ϣ + ArcNode *firstarc;//ָһöָ +}; + +template +struct ALGraph{ + VNode
vertices[MAX_VEXNUM];//㼯 + int vexnum;// + int arcnum;// +}; + +template +void DispG(ALGraph
G) +{ + int i; + ArcNode *p; + cout<adjvex) //ʱһ߱ + { + cout<<"("<adjvex].data<<")"<<'\t'; + } + p = p->nextarc; + } + } + cout< +int LocateVex(ALGraph
G, DT v) +{ + for(int i=0;i +void CreateUDG(ALGraph
&G) +{ + int i,j,k; + DT v1,v2; + ArcNode *p; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vertices[i].data; + G.vertices[i].firstarc = NULL; + } + //cout<<"ÿڽӵ㣺 "<>v1>>v2; + i = LocateVex(G,v1); + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"<adjvex = j; + p->nextarc = G.vertices[i].firstarc; //ڱͷ + G.vertices[i].firstarc = p; + p = new ArcNode; //һµĻ + p->adjvex = i; + p->nextarc = G.vertices[j].firstarc; //ڱͷ + G.vertices[j].firstarc = p; + } +} + + +template +void DestroyGraph(ALGraph
G) +{ + int i; + ArcNode *p,*q; + for(i = 0;inextarc; + delete p;//ɾ + p = q; + } + } + G.arcnum = 0; + G.vexnum = 0; +} + +template +bool GetVex(ALGraph
G, int k,DT &v) +{ + if(k<0||k>=G.vexnum) //㲻 + return false; + v=G.vertices[k].data; + return true; +} + +template +bool PutVex(ALGraph
&G, DT &u,DT v) +{ + int k = LocateVex(G,u); + if(k<0) //ö㲻 + return false; + G.vertices[k].data = v; + return true; +} + +template +int FirstAdjVex(ALGraph
G, int u) +{ + ArcNode * p; + if(u<0 || u>=G.vexnum) // 㲻 + return -1; + p = G.vertices[u].firstarc;//pָ±Ϊiĵһڽӵ + if(p) + { + return p->adjvex; + } + else + { + return -1; + } +} + +template +int NextAdjVex(ALGraph
G, int u,int w) +{ + ArcNode *p; + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum ) // + return -1; + p = G.vertices[u].firstarc; + while(p &&(p->adjvex!=w)) + //pָ򶥵w + { + p = p->nextarc; + } + if(!p||!p->nextarc) //ûҵwwһ + return -1; + else + //ҵwwһ + { + return p->nextarc->adjvex; + } +} + +template +bool InsertVex(ALGraph
&G, DT v) +{ + int j; + char ans; + DT w; + if(G.vexnum > MAX_VEXNUM) + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ:"<>w; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w); + else + cout<>ans; + }; + return true; +} + + +template +bool InsertArc(ALGraph
&G, DT v,DT w) +{ + ArcNode *p; + int i,j; + i = LocateVex(G,v); + j = LocateVex(G,w); + if(i<0||j<0 || i==j) // 㲻ڻ˵ͬ,ܲ + { + cout<<"\n㲻ڻͬܲ룡"<adjvex==j) + { + cout<<"ߴڣܲ룡"<nextarc; + } + G.arcnum++; + p = new ArcNode; + p->adjvex = j; + p->nextarc = G.vertices[i].firstarc; //(v,w)߽ڵiͷ + G.vertices[i].firstarc = p; + p = new ArcNode; + p->adjvex = i; //(w,v)߽ڵjͷ + p->nextarc = G.vertices[j].firstarc; + G.vertices[j].firstarc = p; + return true; +} + +template +bool DeleteArc(ALGraph
&G, DT v,DT w) +{ + ArcNode *p,*q; + int i,j; + cout<<"Hello DeleteArc!"<adjvex!=j) // ppָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p&&p->adjvex ==j) // ҵ + { + if(p == G.vertices[i].firstarc) // 1߽ + { + G.vertices[i].firstarc = p->nextarc; + } + else // ǵ1߽ + { + q->nextarc = p->nextarc; + } + delete p; + G.arcnum--; + p = G.vertices[j].firstarc; + while(p&&p->adjvex!=i) // pqָIJǴɾ + { + q = p; + p = p->nextarc; + } + if(p == G.vertices[j].firstarc) // 1߽ + { + G.vertices[j].firstarc = p->nextarc; + } + else // ǵ1߽ + { + q->nextarc = p->nextarc; + } + delete p; + } + cout<<"Bye-bye DeleteArc!"< +bool DeleteVex(ALGraph
&G, DT v) +{ + int i,j; + ArcNode *p; + DT w; + i = LocateVex(G,v); + cout<<"ɾ"<adjvex; + cout<<"ɾ߶Ϊ"< +void DFS(ALGraph
G, int v) +{ + int w; + visited[v] = true; // ѷ + cout<=0;w=NextAdjVex(G,v,w)) + { + if(!visited[w]) + DFS(G,w); + } +} + +// 㷨6.9 +template +void DFSTraverse(ALGraph
G) +{ + int i ; + for(i = 0;i +void BFS(ALGraph
G, int v) +{ + int w; + ArcNode *p; + LinkQueue Q; + InitQueue(Q); + cout<adjvex; + if(!visited[w]) + { + cout<nextarc; + } + } +} + + +template +bool BFSTraverse(ALGraph
G) +{ + int i; + for(i = 0;i +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +struct QNode // +{ + DT data; //򣬴洢Ԫֵ + QNode *next;//ָָһ +}; + +template +struct LinkQueue +{ + QNode
* front; + QNode
* rear; +}; + + +//㷨3.19 +template +void InitQueue(LinkQueue
&Q)//ն +{ + Q.front=new QNode
; //ͷ + if(!Q.front) exit(1); //ʧܣ + Q.front->next=NULL; + Q.rear=Q.front; +} + +//㷨3.20 +template +void DestroyQueue(LinkQueue
&Q)//ͷ +{ + QNode
*p; + while(Q.front)//ͷ㿪ʼͷŽ + { + p=Q.front; + Q.front=Q.front->next; + delete p; + } +} + +//㷨3.21 +template +bool EnQueue(LinkQueue
&Q,DT e) +{ + QNode
*p; + p=new QNode
; // ½ + if(!p) return false; // ʧܣ + p->data=e; // ½㸳ֵ + p->next=NULL; // ڶβ + Q.rear->next=p; + Q.rear=p; + return true; // ӳɹtrue +} + +//㷨3.22 +template +bool DeQueue(LinkQueue
&Q,DT &e) +{ + QNode
*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 +bool GetHead(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) return false; // ӿգfalse + e=Q.front->next->data; + return true; // ɾɹtrue +} + +//ȡβԪ +template +bool GetTail(LinkQueue
Q,DT &e) +{ + if(Q.front==Q.rear) // ӿ + return false; // false + e=Q.rear->data; // ȡβԪ + return true; // true +} + +//ӿ +template +bool QueueEmpty(LinkQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; //true + else //ǿ + return false; //false +} + +//ʾ +template +void DispQueue(LinkQueue
Q) +{ + QNode
*p; + p=Q.front->next; + while(p) + { + cout<data<<"\t"; + p=p->next; + } + cout< +#include "LinkQueue.h" +#include "Mgraph.h" +#include +using namespace std; + + + +// ͼ + +void DispMenu() +{ + cout<<"ѡҪIJ"< G; + int choice; + do + { + DispMenu(); + cin>>choice; + switch(choice) + { + case 1: + CreateUDN(G); + cout<>u; + k=LocateVex(G,u); + if(k!=-1) + cout<<""<>index; + if(GetVex(G,index,v)) + cout<<"λΪ"<>u; + cout<<"ĺ󶥵ֵ: "; + cin>>v; + PutVex(G,u,v); + cout<<"ֵ޺ͼΪ"<>v; + InsertVex(G,v); + cout<<"붥ӦߺͼΪ"<>v; + DeleteVex(G,v); + cout<<"ɾͼΪ"<>u>>v; + InsertArc(G,u,v); + cout<<"ߺͼΪ"<>u>>v; + DeleteArc(G,u,v); + cout<<"ߺͼΪ"< +struct MGraph // ͼڽӾʾ洢 +{ + DT vexs[MAX_VEXNUM]; // Ϣ + int arcs[MAX_VEXNUM][MAX_VEXNUM]; + int vexnum,arcnum; // ͱ +}; + + +template +void DispG(MGraph
G) // ʾͼϢ +{ + int i,j; + DT u,v; + cout< +int LocateVex(MGraph
G,DT v) +{ + for(int i = 0;i +void CreateUDN(MGraph
&G) +{ + int i,j,k; + DT v1,v2; + cout<<"ͼĶ "; // 1. 붥 + cin>>G.vexnum ; + cout<<"ͼı "; + cin>>G.arcnum ; + cout<<""<>G.vexs[i]; + for(i=0;i>v1>>v2; // 4.1 ߵڽӵ + i = LocateVex(G,v1); // 4.2 λڽӵ + j = LocateVex(G,v2); + if(i<0 || j<0 || i==j) + { + cout<<"Ϣ룡"< +bool GetVex(MGraph
G, int k, DT &v) // ȡ u ֵv +{ + if(k<0 || k>=G.vexnum) // uڣfalse + return false; + else + { + v=G.vexs[k]; + return true; + } +} + + +template +bool PutVex(MGraph
&G,DT &u,DT v) // Ϊu㸳ֵ +{ + int k; + k=LocateVex(G,u); + if(k<0 ) // uڣfalse + return false; + else // uڣֵ + { + G.vexs[k] = v; + return true; + } +} + +//㷨6.3 ֵҵһڽӵ +template +int FirstAdjVex(MGraph
G,int u) +{ + if(u<0 || u>=G.vexnum) // 㲻 + return -1; // ڽӵ㣬-1 + for(int j=0;j +int NextAdjVex(MGraph
G,int u,int w) //ҵuڽӵWһڽӵ +{ + if(u<0 || u>=G.vexnum || w<0 + || w>=G.vexnum || G.arcs[u][w]==0 ) // + return -1; // ڽӵ + for(int j=w+1;j +bool InsertVex(MGraph
&G,DT v) // ֵΪvĶ +{ + DT w; + int j; + char ans; + if(G.vexnum>=MAX_VEXNUM) // ޴洢ռ䣬ܲ + { + cout<<"޴洢ռ䣬ܲ!"<>ans; + while(ans=='Y'|| ans=='y') + { + cout<<"һֵ"<>w; + j=LocateVex(G,w); + if(j>=0) // + InsertArc(G,v,w); + else + cout<>ans; + }; + return true; +} + +template +bool InsertArc(MGraph
&G,DT v,DT w) // ֵΪvwӱ +{ int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0 || j<0 || i==j) // 㲻ڻ˵ͬ + return false; // ܲߣfalse + G.arcs[i][j]=1; + G.arcs[j][i]=1; + G.arcnum++; + return true; +} + +template +bool DeleteArc(MGraph
&G,DT v,DT w) // ֵɾ +{ + int i = LocateVex(G,v); + int j = LocateVex(G,w); + if(i<0||j<0||i == j) // ߲ڣfalse + { + cout<<"߲ڣ"< +bool DeleteVex(MGraph
&G,DT v) // ֵɾ +{ + int i,j; + DT w; + i = LocateVex(G,v); // 㶨λ + if(i<0) + { + cout<<"㲻ڣ"< +void DFS(MGraph
G,int v) // ͨͼȱ +{ + int w; + visited[v] = true; // ȷindex + cout< +void DFSTraverse(MGraph
G) // ͨͼȱ +{ + int i; + for(i=0;i +void BFS(MGraph
G,int v) +{ + int w; + LinkQueue Q; + InitQueue(Q); + cout<=0;w=Nextadjvex(G,u,w))) + if(!visited[w]) + { + cout< +void BFSTranverse(MGraph
G) +{ + int i; + for(i=0;i +bool BFSTraverse(MGraph
G) // ȱ +{ + int i; + int u,w; + LinkQueue Q; + InitQueue(Q); + for(i = 0;i=0;w=NextAdjVex(G,u,w)) + { + if(!visited[w]) + { + visited[w] = true; + cout< +//#include"assert.h" +//#include"sort.h" +using namespace std; + + // ݼ,0Ԫδ +int IA[9]={0,58,40,65,97,87,8,17,58}; // ֱӲ롢۰ +int SA[11]={0,58,40,65,97,87,8,17,58,46,60}; // ϣ +int BA[9]={0,58,40,65,97,87,8,17,58}; // ð +int QA[9]={0,58,35,65,97,87,8,17,58}; // +int SSA[9]={0,58,35,25,97,87,8,58,17}; // ѡ +int HA[9]={0,38,25,16,36,18,32,28,50}; // +int EA1[8]={0,58,40,25,87,8,58,17}; // 鲢 +int EA2[9]={0,8,4,5,2,6,3,7,9}; // ݹ鲢 +int RA[9]={0,769,763,63,249,243,545,281,89}; // + + + +void CopyData(int a[],int n, int R[]) // ݺһԪ1Ԫʼ +{ + for(int i=1;i<=n;i++) + R[i]=a[i]; +} + +void DispData(int R[], int n) // ʾ +{ + int i; + for(i=1;i<=n;i++) + cout<R[i]) // 1.R[i-1]>R[i] + { + R[0]=R[i]; // 1.1 R[i]Ϊ + R[i]=R[i-1]; // 1.2 R[i] + for(j=i-2;R[j]>R[0];--j) // 1.3 ji-2ʼR[j]>R[0] R[j]ƣj-- + R[j+1]=R[j]; // 1.4 R[i]i++ + R[j+1]=R[0]; + } + cout<<" "<R[i]) high=m-1; // 2.2.2 R[m]>R[0]ѯϽ + else low=m+1; // 򣬵½ + } // 3.high+1Ԫƶ + for(j=i-1;j>=high+1;--j) // 3.1 R[i-1]R[high+1]κ + R[j+1]=R[j]; // 3.2 R[i]ƵR[high+1] + R[high+1]=R[0]; + cout<<" "<0 && (R[0]R[j+1]) // 2.1Ԫ򣬻λ + { + temp=R[j],R[j]=R[j+1],R[j+1]=temp; // R[j]<-->R[j+1] + exchange=true; // 2.2 ־Ϊtrue + } + } + cout<<" "<=pivot) // 2.1 Ӹ߶ + --high; // 2.1.1 λò + R[low]=R[high]; // 2.1.2 С¼ƵͶ + while(low R[j]) // 2.2ڽϴߣ¼ƶ + break; + R[s]=R[j]; // 2.3 R[j]Ϊ + s=j; // 2.4 һ + } + R[s]=R[0]; // R[s]뵽λ +} + +//㷨8.9 +void CreateHeap(int R[],int n) //R[1..n]ɴ +{ + int i; + for(i=n/2;i>0;--i) // ׸Ҷ㿪ʼ + { + HeapAdjust(R,i,n); // HeapAdjust + } + cout<<" \t"; // + DispData(R,n); +} + +//㷨8.10 +void HeapSort(int R[],int n) +{ + int i,t; + CreateHeap(R,n); // + for(i=1;ikeys[0]=R[i]/100; // λ + p->keys[1]=R[i]%100/10; // ʮλ + p->keys[2]=R[i]%100%10; // λ + p->next=NULL; + if (i==1) // Ԫ + { + L=p; + rr=L; + } + else // Ԫ + { + rr->next=p; + rr=p; + } + cout<keys[d]; + cout<<'\t'; + p=p->next; + } + cout<=0;i--) // dλdؼ֣d + { // ÿһ + for(j=0;jkeys[i]; // 2.1iӹؼΪkk + if(h[k]==NULL) // 2.1.1Ԫ㣬 + { + h[k]=L;t[k]=L; // βָͷ + } + else // 2.1.2Ԫ㣬 + { + t[k]->next=L; t[k]=L; // β + } + L=L->next; // 2.2 ȡһ´¼ + } + L=NULL; // ռ + for(j=0;jnext=h[j];T=t[j]; // β + } + } + } + T->next=NULL; // β㣬nextΪ + cout<<" "<>choice; + switch(choice) + { + case 1: // ֱӲ + { + cout<<"ֱӲ"<R + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R,n); + InsertSort(R,n); // ֱӲ + ; + break; + } + case 2: // ۰ + { + cout<<"۰"<R + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R,n); + BInsertSort(R,n); // ۰ + cout<R[] + cout<<"ʼ"<<'\t'; + DispData(R,n); // ʾʼ + ShellSort(R,n,d,3); // ϣ + break; + } + case 4: // ð + { + cout<<"ð"<R[] + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R,n); + Bubble_Sort(R,n); // ð + break; + } + case 5: // + { + cout<<""<R[] + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R,n); + QSort(R,1,n); // + cout<R[] + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R,n); + SelectSort(R,n); // ѡ + break; + } + case 7: // + { + cout<<""<R[] + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R,n); + HeapSort(R,n); // + cout<R1 + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R1,n1); + MergeSort(R1,n1); // ·ݹ鲢 + cout<<"鲢򣨵ݹ飩"<R2 + cout<<"ʼ"<<'\t'; // ʾʼ + DispData(R2,n2); + MergeSort1(R2,n2); + cout<<"\t"; + DispData(R2,n2); // + break; + } + case 9: // + { + int r,d; + r=10; // ʮ + d=3; // 3ؼ + RNode *L; // ʽ洢ͷ + L=NULL; // ձ + cout<<""<R + cout<<":"; + DispData(R,n); // ʾʼ + CreateDL(L,R,n); // + cout<<"ʼ"<<'\t'; // ʾ + DispDL(L); + RadixSort(L,r,d); // + cout<<"\t"; // ʾ + DispDL(L); + break; + } + + case 0://˳ + ; + cout<<""< & L) +{//˳LֱӲ + for(int i=2;i<=L.length;i++) + if(L.key[i]<=L.key[i-1])//"<"轫L.key[i]ӱ + { + L.key[0]=L.key[i];//Ϊڱ + L.key[i]=L.key[i-1]; + for(int j=i-2;L.key[0]<=L.key[j];--j) + L.key[j+1]=L.key[j];//¼ + L.key[j+1]=L.key[0];//뵽ȷλ + } +} + +//2۰ +template +void BInsertSort(SqList &L) +{//˳L۰ + int high,low,m; + for(int i=2;i<=L.length;i++) + { + L.key[0]=L.key[i];//L.key[i]ݴ浽L.key[0] + low=1; + high=i-1; + while(low<=high)//key[low]key[high]۰λ + { + m=(low+high)/2;//۰ + if(L.key[0]<=L.key[m]) + high=m-1;//Ͱ + else + low=m+1;//߰ + } + for(int j=i-1;j>=high+1;--j) + L.key[j+1]=L.key[j]; //¼ + L.key[high+1]=L.key[0]; // + } +} +//(3) +const int SIZE=100;//̬ +const int MAXINT=10000; +template +struct StaListNode{ + type data;//¼ + int next;//ָ +}; +template +class StaticList{ + public: + StaListNode node[SIZE]; + int curlen;//ʵʳ + StaticList()//캯 + { + cout<<"̬"<>curlen; + cout<<"ݣ"<>node[i].data; + } + } + ~StaticList()// + { + } +}; +void StaListInsertSort() +{ + StaticList sl; + int min,max;//ֵСֵ + sl.node[0].next=1; + sl.node[1].next=0;//ʼγֻͷѭ + max=min=1; + for(int i=2;i<=sl.curlen;i++)//ѭм㣬n-1 + { + + if(sl.node[i].data<=sl.node[min].data) + { + sl.node[0].next=i; + sl.node[i].next=i-1; + min=i; + } + if(sl.node[i].data>=sl.node[max].data) + { + sl.node[i].next=0; + sl.node[max].next=i; + max=i; + } + if(sl.node[i].datasl.node[min].data) + { + int index1=min,index2;//index2index1ǰһ± + while(sl.node[i].data>=sl.node[index1].data) + { + index2=index1; + index1=sl.node[index1].next; + } + sl.node[i].next=index1; + sl.node[index2].next=i; + } + + } + cout<<"£"< +void ShellInsert(SqList &L,int dk) +{//˳һϣ + for(int i=dk+1;i<=L.length;i++) + if(L.key[i]<=L.key[i-dk]) + { + L.key[0]=L.key[i]; + for(int j=i-dk;j>0&&L.key[0]<=L.key[j];j-=dk) + L.key[j+dk]=L.key[j]; + L.key[j+dk]=L.key[0]; + } +} +template +void ShellSort(SqList &L,int dlta[],int t) +{ + //dl[0]--dl[t-1]˳Lϣ + for(int k=0;k +void BubbleSort(SqList & L) +{ + for(int i=1;iL.key[j+1]) + { + t=L.key[j]; + L.key[j]=L.key[j+1]; + L.key[j+1]=t; + } + } +} +//(2) +template +int Partition(SqList& L,int low,int high) +{//˳Lӱkey[low]--key[high]еļ¼¼λλã +//ʱ֮ǰ󣩵ļ¼С + type pivotkey; + L.key[0]=L.key[low];//ӱĵһ¼¼ + pivotkey=L.key[low];//ؼ + while(low=pivotkey) --high; + L.key[low]=L.key[high];//Сļ¼Ͷ + while(low +void QSort(SqList& L,int low,int high) +{ + int mid;//λ + if(low +void QuitSort(SqList& L)//˳п +{ + QSort(L,1,L.length); + +} +//ѡ +//(1)ѡ +template +int SelectMinKey(SqList& L,int n) +{ + int min=n; + type minkey;//Сֵ + minkey=L.key[n]; + for(int i=n+1;i<=L.length;i++) + if(L.key[i] +void SelectSort(SqList& L) +{ + //˳Lѡ + int j; + type t; + for(int i=1;i<=L.length;i++) + { + j=SelectMinKey(L,i);//L.key[i]--L.key[L.length]ѡСļ¼ַj + if(i!=j)//¼ + { + t=L.key[i]; + L.key[i]=L.key[j]; + L.key[j]=t; + } + } +} +//(2)ѡ + +template +class TreeNode{ + public: + type data;//ֵ + int index;//λ + int active;//ǷȽϱ־ + TreeNode& operator=(TreeNode& treenode) + { + this->data=treenode.data; + this->index=treenode.index; + this->active=treenode.active; + return *this; + } +}; +template +void ChangeTree(TreeNode*tree,int i) +{ + if(i%2==0) + tree[(i-1)/2]=tree[i-1];//iΪżʱȽ + else + tree[(i-1)/2]=tree[i+1];//iΪʱҽȽ + i=(i-1)/2;//±Ƚϣi׽λ + int j; + while(i) + { + if(i%2==0)//ȷiıȽϽλ + j=i-1; + else + j=i+1; + if(!tree[i].active||!tree[j].active) + if(tree[i].active) + tree[(i-1)/2]=tree[i]; + else + tree[(i-1)/2]=tree[j]; + else + if(tree[i].data=1) + { + for(int i=1;i<=n;i++) + result*=2; + return result; + } + else + return 1; +} +int LeapNum(int n)//Ҷӽ +{ + for(int i=1;i<100;i++) + if(Power(i-1) +void TreeSort(type a[],int n) +{//򷽷ֻ + TreeNode* tree; + int bottsize=LeapNum(n);//ײ˴ + int size=2*bottsize-1;//н + int externalindex=bottsize-1;//ʼбȽϵĽλ + tree=new TreeNode[size]; + assert(tree); + int j=0; + for(int i=externalindex;i +void OutPut(type a[],int n)// +{ + for(int i=0;i +void HeapAdjust(SqList& L,int s,int m) +{//˳ңֵĺӽɸѡҵֵ + type rc=L.key[s]; + for(int j=2*s;j<=m;j*=2) + { + if(jL.key[j]) break;//rcƳwhileѭ + L.key[s]=L.key[j];//ֵֵ + s=j;//λ + } + L.key[s]=rc; +} +template +void HeapSort(SqList& L) +{//˳Lж + type value; + for(int i=L.length/2;i>0;i--)//L.key[1...L.length]Ϊ󶥶 + HeapAdjust(L,i,L.length); + for(i=L.length;i>1;--i) + { + value=L.key[1]; + L.key[1]=L.key[i]; + L.key[i]=value; + HeapAdjust(L,1,i-1);//L.key[1...i-1]µΪ󶥶 + } + +} +// +//鲢 +template +void Merge(type *SR,type *TR,int i,int m,int n) +{//SR[i...m]SR[m+1...n]鲢ΪTR[i...n] + for(int j=m+1,k=i;i<=m&&j<=n;k++)//SRеļ¼ɴСTR + { + if(SR[i]<=SR[j]) + TR[k]=SR[i++]; + else + TR[k]=SR[j++]; + } + if(i<=m)//ʣSR[i...m]ֵTR + for(int a=i;a<=m;a++) + TR[k++]=SR[a]; + else if(j<=n)//ʣSR[j...n]ƵTR + for(int b=j;b<=n;b++) + TR[k++]=SR[b]; +} +template +void MSort(type *SR,type *TR1,int s,int t) +{ + type TR2[100];//СԸʵ¶ + int m; + //SR[s...t]鲢ΪTR[s...t] + if(s==t) + TR1[s]=SR[s]; + else + { + m=(s+t)/2;//SR[s...t]ƽΪSR[s...m]SR[m+1...t] + MSort(SR,TR2,s,m);//ݹؽSR[s...m]鲢ΪTR2[s...m] + MSort(SR,TR2,m+1,t);//ݹؽSR[m+1...t]鲢ΪTR2[m+1...t] + Merge(TR2,TR1,s,m,t);//TR2[s...m]TR2[m+1...t]鲢TR1[s...t] + } +} +template +void MergeSort(SqList &L) +{//˳L鲢 + MSort(L.key,L.key,1,L.length); +} +// +//ʽ +const RADIX=10; +typedef int ArrType[RADIX]; +ArrType f,e; +template +struct SLCell{ + type *keys; + int next; +}; +template +struct SLList{ + SLCell* SList; + int keynum; + int recnum; +}; +template +void InitSLList(SLList& SL)//̬ +{ + cout<<"̬"<>SL.recnum; + SL.SList=new SLCell[SL.recnum+1]; + assert(SL.SList); + cout<<"ؼ"<>SL.keynum; + for(int i=1;i<=SL.recnum;i++) + { + SL.SList[i].keys=new type[SL.keynum+1]; + assert(SL.SList[i].keys); + } + SL.SList[0].next=1; + cout<<""<>SL.SList[i].keys[j]; + if(i!=SL.recnum) + SL.SList[i].next=i+1; + else + SL.SList[i].next=0; + } +} +template +void RelList(SLList &SL)//ͷڴռ +{ + for(int i=1;i<=SL.recnum;i++) + delete SL.SList[i].keys; + delete SL.SList; +} + + +template +void OutPut(SLList& SL)//̬ +{ + for(int i=SL.SList[0].next;i;i=SL.SList[i].next) + { + //cout< +void Distrbute(SLCell* r,int i,ArrType &f,ArrType& e) +{//iؼkeys[i]RADIXӱʹͬһӱм¼keys[i]ͬ +//f[0...RADIX]e[0...RADIX]ֱָӱеһһ¼ + int j; + for(j=0;j +void Collect(SLCell* r,int i,ArrType &f,ArrType& e) +{//keys[i]Сf[0...RADIX]ָӱӳһ + int j; + for( j=0;!f[j];j++);//ҵһǿӱ + r[0].next=f[j];//r[0].nextָһǿӱеһ + int t=e[j]; + while(j +void RadixSort(SLList &SL) +{//LʹLؼС + for(int i=SL.keynum;i>=1;i--)//λζԸؼֽзռ + { + Distrbute(SL.SList,i,f,e);//i˷ + Collect(SL.SList,i,f,e);//iռ + } +} + diff --git a/OrigFiles/树和二叉树/5-1-BiTree/SqQueue_bt.h b/OrigFiles/树和二叉树/5-1-BiTree/SqQueue_bt.h new file mode 100644 index 0000000..3e76921 --- /dev/null +++ b/OrigFiles/树和二叉树/5-1-BiTree/SqQueue_bt.h @@ -0,0 +1,66 @@ +//й +template +struct SqQueue // ˳ +{ + BTNode
* *base; // ַ + int front; // ͷָ + int rear; // βָ + int queuesize; // +}; + +//㷨3.14 ʼ +template +void InitQueue(SqQueue
&Q, int m) +{ + Q.base=new BTNode
*[m]; // пռ + if(Q.base==NULL) // ռʧ + { + cout<<"δɹ"; + exit (1); // ˳ + } + Q.front=Q.rear=0; // ö + Q.queuesize=m; +} + + +//㷨3.15 +template +void DestroyQueue(SqQueue
&Q) +{ + delete [] Q.base; // ͷŶпռ + Q.front=Q.rear=0; // ö + Q.queuesize=0; +} + + +//㷨3.16 +template +bool EnQueue(SqQueue
&Q,BTNode
* 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 +bool DeQueue(SqQueue
&Q,BTNode
* &e) +{ + if(Q.front==Q.rear) // ӿ + return false; + e=Q.base[Q.front]; + Q.front=(Q.front+1)%Q.queuesize; + return true; // ɾɹtrue +} + + // ӿ +template +bool QueueEmpty(SqQueue
Q) +{ + if(Q.front==Q.rear) // ӿ + return true; // true + else // Ӳ + return false; // false +} \ No newline at end of file diff --git a/OrigFiles/树和二叉树/5-1-BiTree/SqStack_bt.h b/OrigFiles/树和二叉树/5-1-BiTree/SqStack_bt.h new file mode 100644 index 0000000..ede3f93 --- /dev/null +++ b/OrigFiles/树和二叉树/5-1-BiTree/SqStack_bt.h @@ -0,0 +1,76 @@ +//ջ +template +struct SqStack // ˳ջ +{ + BTNode
* *base; // ջַ + int top; // ջָ + int stacksize; // ջ +}; + + +//㷨3.1 ʼջ +template +void InitStack(SqStack
&S, int m) +{ + S.base=new BTNode
* [m]; // ջռ + if(S.base==NULL) + { + cout<<"δɹ"; + exit (1); + } + S.top=-1; // ջ + S.stacksize=m; // ջΪm +} + + +//㷨3.2 ջ +template +void DestroyStack(SqStack
&S)// +{ + delete [] S.base; //ͷջռ + S.top=-1; + S.stacksize=0; +} + + +//㷨3.3 +template +bool Push(SqStack
&S,BTNode
* e) +{ + if(S.top==S.stacksize-1) // ջܲ + return false; + S.top++; + S.base[S.top]=e; + return true; // ɹtrue +} + +//㷨3.4 ջ +template +bool Pop(SqStack
&S,BTNode
* &e) +{ + if(S.top==-1) //ջ + return false; + e=S.base[S.top]; + S.top--; + return true; // ջɹtrue +} + +template // ȡջԪ +bool GetTop(SqStack
&S,BTNode
* &e) +{ + if(S.top==-1) // ջգfalse + return false; + e=S.base[S.top]; // ȡջԪ + return true; // true +} + + + // ջ +template +bool StackEmpty(SqStack
S) +{ + if(S.top==-1) // ջգtrue + return true; + else // ջǿգfalse + return false; +} \ No newline at end of file diff --git a/OrigFiles/树和二叉树/5-1-BiTree/bitree.cpp b/OrigFiles/树和二叉树/5-1-BiTree/bitree.cpp new file mode 100644 index 0000000..3c67bb7 --- /dev/null +++ b/OrigFiles/树和二叉树/5-1-BiTree/bitree.cpp @@ -0,0 +1,135 @@ + +#include//cout,cin +#include +using namespace std; +#include"bitree.h" +#include"SqQueue_bt.h" +#include"SqStack_bt.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<<"\nѡ1~120˳"< *bt; + int y=100,x=350; + + system("cls"); // + + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~120 ˳):"; + cin>>choice; + switch(choice) + { + case 1: // + cout<<"Բοݣ"< *p; + cout<<"\nҪѯĽֵ"<>e; + p=Search(bt,e); + if(p) + { + cout<<"\nҵ"; + cout<data< +struct BTNode // 㶨 +{ + DT data ; // + BTNode* lchild; //ָָ + BTNode* rchild; //ָָ +}; + + +//㷨5.1 ݹ㷨 +template +void PreOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + cout<data<<' '; //ϵ + PreOrDerBiTree(bt->lchild); //ݹĵǰ + PreOrDerBiTree(bt->rchild); //ݹĵǰ + } + //return; +} + +//㷨5.2 ݹ㷨 +template +void InOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + InOrDerBiTree(bt->lchild); //ݹĵ + cout<data<<' '; //ϵ + InOrDerBiTree(bt->rchild); //ݹĵ + } + //return; +} + +//㷨5.3 ݹ㷨 +template +void PostOrDerBiTree(BTNode
*bt) +{ + if(bt!=NULL) + { + PostOrDerBiTree(bt->lchild); //ݹĵú + PostOrDerBiTree(bt->rchild); //ݹĵú + cout<data<<' '; //ϵ + } + //return; +} + + +//㷨5.4 㷨 +template +void LevelBiTree(BTNode
*bt) +{ + SqQueue
Q; // һ + int m=20; + InitQueue(Q,m); + BTNode
* p; + p=bt; + if(p) EnQueue(Q,p); // ǿգ + while (!QueueEmpty(Q)) // ӷǿ + { + DeQueue(Q,p); // + cout<data; // + if(p->lchild!=NULL) // + EnQueue(Q, p->lchild); // + if(p->rchild!=NULL) // Һ + EnQueue(Q, p->rchild); // Һ + } + DestroyQueue(Q); // ٶ +} + + +//㷨5.5 DZݹ㷨 +template +void PreOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // ջ + int m=20; + InitStack(S, m); + BTNode
* p; + p=bt; + while (p!=NULL || !StackEmpty(S)) // ǿջջǿ + { + while(p!=NULL) // ǿ + { + cout<data<<' '; // ʽ + Push(S,p); // ջ + p=p->lchild; // ת + } + if(!StackEmpty(S)) // ջǿ + { + Pop(S,p); // ջ + p=p->rchild; // תջ + } + } + DestroyStack(S); //ջ +} + + +//㷨5.6 DZݹ㷨 +template +void InOrderBiTree_N(BTNode
*bt) +{ + SqStack
S; // һջ + int m=20; + InitStack(S, m); + BTNode
* p; + p=bt; + while (p!=NULL || !StackEmpty(S)) // ǿջջǿ + { + while(p!=NULL) // ǿ + { + Push(S,p); // ջ + p=p->lchild; // תջ + } + if(!StackEmpty(S)) // ջǿ + { + Pop(S,p); // ջ + cout<data<<' '; // ʳջ + p=p->rchild; // תջ + } + } + DestroyStack(S); // ջ +} + + + +//㷨5.7 DZݹ㷨 +template +void PostOrderBiTree_N(BTNode
*bt) +{ // 1. ʼ + SqStack
S; // һջ + int m=20; + InitStack(S, m); + BTNode
* p; + BTNode
* 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<data<<' '; // ջԪ + Pop(S,p); // ջ + r=p; // rָ򱻷ʽ + } + else // + { + p=p->rchild; // תջԪҺ + flag=false; // ǰΪջ + } + } + }while(!StackEmpty(S)); // ջǿգѭ + cout< +void CreateBiTree(BTNode
*&bt) +{ + char ch; + cin>>ch; // + if(ch=='#') // # ʾָΪգ˵Ϊ + bt=NULL ; + else + { + bt=new BTNode
; // ڴ + if(!bt) + { + cout<<"ڴʧܣ"<data=ch; + CreateBiTree(bt->lchild); // + CreateBiTree(bt->rchild); // + } + return; +} + + +//㷨5.9 ٶ +template +void DestroyBiTree(BTNode
*&bt) +{ + if(bt) // ǿ + { + DestroyBiTree(bt->lchild); // + DestroyBiTree(bt->rchild); // + delete bt; + } +} + + +//㷨5.10 + +template +BTNode
* Search(BTNode
* bt, DT e) // ֵΪeԪ +{ + BTNode
*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 +int Depth(BTNode
*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 +int NodeCount(BTNode
*bt) +{ + if(bt==NULL) // Ϊ0 + return 0; + else // ǿΪĺͼ1 + return NodeCount(bt->lchild)+NodeCount(bt->rchild)+1; +} + +template +void DispBiTree(BTNode
* bt,int level) // ʾ +{ + if(bt) // նʾ + { DispBiTree(bt->rchild,level+1); // ʾ + cout<data; // ʾڵ + DispBiTree(bt->lchild,level+1); // ʾ + cout< +struct ThrBTNode +{ + DT data; // + int lflag; // ־ + int rflag; // ұ־ + ThrBTNode *lchild; // ָ + ThrBTNode *rchild; // ָ +}; + + +template // +void CreateBiTree(ThrBTNode
*&bt) +{ // е˳,#Ϊָ־; + char ch; + cin>>ch; // + if(ch=='#') // # ʾָΪգ˵Ϊ + bt=NULL ; + else + { + bt=new ThrBTNode
; // ½ + if(!bt) + { + cout<<"ڴʧܣ"<lflag=0; // + bt->rflag=0; // + bt->data=ch; + CreateBiTree(bt->lchild); // ݹ鴴 + CreateBiTree(bt->rchild); // ݹ鴴 + } + return; +} + +ThrBTNode *pre; + +//㷨5.13 +template +void InThread(ThrBTNode
*&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 // +ThrBTNode
* CreateInThread(ThrBTNode
*&bt) +{ + ThrBTNode
*root; + root=new ThrBTNode
; // ͷ + 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 +void InThrBiTree( ThrBTNode
* bt) +{ + ThrBTNode
*p; + p=bt->lchild; // Ӹ㿪ʼ + while(p!=bt) // ǿ + { + while (p->lflag==0) // + p=p->lchild; // һ· + cout<data; // ʽ + while(p->rflag==1 && p->rchild!=bt) // кҷǿ + { + p=p->rchild; // ת + cout<data<<" "; // ʺ̽ + } + p=p->rchild; // ޺ת + } +} + +template // ʾ +void DispBiTree(ThrBTNode
* bt,int level) +{ + if(bt) // նʾ + { DispBiTree(bt->rchild,level+1); // ʾ + cout<data; // ʾڵ + DispBiTree(bt->lchild,level+1); // ʾ + cout< +void DestroyThrBiTree(ThrBTNode
*&bt) +{ + if(bt) + { + DestroyThrBiTree(bt->lchild); + DestroyThrBiTree(bt->rchild); + delete bt; + } +} \ No newline at end of file diff --git a/OrigFiles/树和二叉树/5-2-InThrBiTree/ThrBiTree.cpp b/OrigFiles/树和二叉树/5-2-InThrBiTree/ThrBiTree.cpp new file mode 100644 index 0000000..a53f7d9 --- /dev/null +++ b/OrigFiles/树和二叉树/5-2-InThrBiTree/ThrBiTree.cpp @@ -0,0 +1,76 @@ +#include//cout,cin +#include +#include"InThrBiTree.h" +using namespace std; + + +void dispmenu() +{ // ʾ˵ + cout< *bt; + system("cls"); // + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~7):"; + cin>>choice; + switch(choice) + { + case 1: // + cout<<"ݲο"<