commit 6beb7a1616f83e21418858aa9e360e4ee6730c34
Author: 魏遇卿 <358614401@qq.com>
Date: Tue Jul 18 23:13:36 2023 +0800
Initial commit
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..dfe0770
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,2 @@
+# Auto detect text files and perform LF normalization
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c6127b3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,52 @@
+# Prerequisites
+*.d
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/.name b/.idea/.name
new file mode 100644
index 0000000..e4763d2
--- /dev/null
+++ b/.idea/.name
@@ -0,0 +1 @@
+DSAndOS
\ No newline at end of file
diff --git a/.idea/DSAndOS.iml b/.idea/DSAndOS.iml
new file mode 100644
index 0000000..f08604b
--- /dev/null
+++ b/.idea/DSAndOS.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/.idea/QIUofDSAndOS.iml b/.idea/QIUofDSAndOS.iml
new file mode 100644
index 0000000..f08604b
--- /dev/null
+++ b/.idea/QIUofDSAndOS.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..0ef8d36
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..79b3c94
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..700073a
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.vs/CMake Overview b/.vs/CMake Overview
new file mode 100644
index 0000000..e69de29
diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json
new file mode 100644
index 0000000..8f0d733
--- /dev/null
+++ b/.vs/ProjectSettings.json
@@ -0,0 +1,3 @@
+{
+ "CurrentProjectSetting": "x64-Debug"
+}
\ No newline at end of file
diff --git a/.vs/QIUofDSAndOS/FileContentIndex/3c71460a-d1dc-4b3c-aea7-128667632a61.vsidx b/.vs/QIUofDSAndOS/FileContentIndex/3c71460a-d1dc-4b3c-aea7-128667632a61.vsidx
new file mode 100644
index 0000000..7e4c021
Binary files /dev/null and b/.vs/QIUofDSAndOS/FileContentIndex/3c71460a-d1dc-4b3c-aea7-128667632a61.vsidx differ
diff --git a/.vs/QIUofDSAndOS/FileContentIndex/f687c5e4-73b7-4da9-bdbe-8eda35646ef2.vsidx b/.vs/QIUofDSAndOS/FileContentIndex/f687c5e4-73b7-4da9-bdbe-8eda35646ef2.vsidx
new file mode 100644
index 0000000..a356ef1
Binary files /dev/null and b/.vs/QIUofDSAndOS/FileContentIndex/f687c5e4-73b7-4da9-bdbe-8eda35646ef2.vsidx differ
diff --git a/.vs/QIUofDSAndOS/FileContentIndex/read.lock b/.vs/QIUofDSAndOS/FileContentIndex/read.lock
new file mode 100644
index 0000000..e69de29
diff --git a/.vs/QIUofDSAndOS/v17/.wsuo b/.vs/QIUofDSAndOS/v17/.wsuo
new file mode 100644
index 0000000..a53726c
Binary files /dev/null and b/.vs/QIUofDSAndOS/v17/.wsuo differ
diff --git a/.vs/QIUofDSAndOS/v17/Browse.VC.db b/.vs/QIUofDSAndOS/v17/Browse.VC.db
new file mode 100644
index 0000000..b4a5acc
Binary files /dev/null and b/.vs/QIUofDSAndOS/v17/Browse.VC.db differ
diff --git a/.vs/QIUofDSAndOS/v17/ipch/AutoPCH/329b27794b9eb529/MAIN.ipch b/.vs/QIUofDSAndOS/v17/ipch/AutoPCH/329b27794b9eb529/MAIN.ipch
new file mode 100644
index 0000000..9a4d304
Binary files /dev/null and b/.vs/QIUofDSAndOS/v17/ipch/AutoPCH/329b27794b9eb529/MAIN.ipch differ
diff --git a/.vs/QIUofDSAndOS/v17/ipch/AutoPCH/ea65033509450063/2.1-2.5.ipch b/.vs/QIUofDSAndOS/v17/ipch/AutoPCH/ea65033509450063/2.1-2.5.ipch
new file mode 100644
index 0000000..a5c146b
Binary files /dev/null and b/.vs/QIUofDSAndOS/v17/ipch/AutoPCH/ea65033509450063/2.1-2.5.ipch differ
diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json
new file mode 100644
index 0000000..f2a74c8
--- /dev/null
+++ b/.vs/VSWorkspaceState.json
@@ -0,0 +1,18 @@
+{
+ "OutputFoldersPerTargetSystem": {
+ "本地计算机": [
+ "out\\build\\x64-Debug",
+ "out\\install\\x64-Debug"
+ ]
+ },
+ "ExpandedNodes": [
+ "",
+ "\\DSBookCode",
+ "\\DSBookCode\\数据结构第2版-书中算法(C语言版)",
+ "\\DSBookCode\\数据结构第2版-书中算法(C语言版)\\第2章",
+ "\\DSBookCode\\数据结构第2版-书中算法(C语言版)\\第2章\\2.1-2.5",
+ "\\DSBookCode\\数据结构第2版-书中算法(C语言版)\\第2章\\2.6-2.12"
+ ],
+ "SelectedNode": "\\DSBookCode\\数据结构第2版-书中算法(C语言版)\\第2章\\2.1-2.5\\main.cpp",
+ "PreviewInSolutionExplorer": false
+}
\ No newline at end of file
diff --git a/.vs/cmake.db b/.vs/cmake.db
new file mode 100644
index 0000000..cc36cb3
Binary files /dev/null and b/.vs/cmake.db differ
diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite
new file mode 100644
index 0000000..a9de331
Binary files /dev/null and b/.vs/slnx.sqlite differ
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..292ec24
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,7 @@
+cmake_minimum_required(VERSION 3.25)
+project(DSAndOS)
+
+set(CMAKE_CXX_STANDARD 17)
+
+add_executable(DSAndOS DSBySelf/2.1.cpp DSBySelf/2.1.cpp DSBySelf/main.cpp)
+add_executable(DSAndOS2 DSBySelf/2.1.cpp DSBySelf/2.1.cpp DSBySelf/2.1.cpp)
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.1-2.5/2.1-2.5.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.1-2.5/2.1-2.5.cpp
new file mode 100644
index 0000000..1cc3bf2
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.1-2.5/2.1-2.5.cpp
@@ -0,0 +1,171 @@
+#include
+#include
+#include
+#include
+using namespace std;
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
+typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
+
+#define MAXSIZE 100 //顺序表可能达到的最大长度
+struct Book {
+ string id;//ISBN
+ string name;//书名
+ double price;//定价
+};
+typedef struct {
+ Book *elem; //存储空间的基地址
+ int length; //当前长度
+} SqList;
+
+Status InitList_Sq(SqList &L) { //算法2.1 顺序表的初始化
+ //构造一个空的顺序表L
+ L.elem = new Book[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
+ if (!L.elem)
+ exit(OVERFLOW); //存储分配失败退出
+ L.length = 0; //空表长度为0
+ return OK;
+}
+
+Status GetElem(SqList L, int i, Book &e) {//算法2.2 顺序表的取值
+ if (i < 1 || i > L.length)
+ return ERROR; //判断i值是否合理,若不合理,返回ERROR
+ e = L.elem[i - 1]; //elem[i-1]单元存储第i个数据元素
+ return OK;
+}
+
+int LocateElem_Sq(SqList L, double e) { //算法2.3 顺序表的查找
+ //顺序表的查找
+ for (int i = 0; i < L.length; i++)
+ if (L.elem[i].price == e)
+ return i + 1;//查找成功,返回序号i+1
+ return 0;//查找失败,返回0
+}
+
+Status ListInsert_Sq(SqList &L, int i, Book e) { //算法2.4 顺序表的插入
+ //在顺序表L中第i个位置之前插入新的元素e
+ //i值的合法范围是1<=i<=L.length+1
+ if ((i < 1) || (i > L.length + 1))
+ return ERROR; //i值不合法
+ if (L.length == MAXSIZE)
+ return ERROR; //当前存储空间已满
+ for (int j = L.length - 1; j >= i - 1; j--)
+ L.elem[j + 1] = L.elem[j]; //插入位置及之后的元素后移
+ L.elem[i - 1] = e; //将新元素e放入第i个位置
+ ++L.length; //表长增1
+ return OK;
+}
+
+Status ListDelete_Sq(SqList &L, int i) { //算法2.5 顺序表的删除
+ //在顺序表L中删除第i个元素,并用e返回其值
+ //i值的合法范围是1<=i<=L.length
+ if ((i < 1) || (i > L.length))
+ return ERROR; //i值不合法
+ for (int j = i; j <= L.length; j++)
+ L.elem[j - 1] = L.elem[j]; //被删除元素之后的元素前移
+ --L.length; //表长减1
+ return OK;
+}
+
+int main() {
+ SqList L;
+ int i = 0, temp, a, c, choose;
+ double price;
+ Book e;
+ string head_1, head_2, head_3;
+ cout << "1. 建立\n";
+ cout << "2. 输入\n";
+ cout << "3. 取值\n";
+ cout << "4. 查找\n";
+ cout << "5. 插入\n";
+ cout << "6. 删除\n";
+ cout << "7. 输出\n";
+ cout << "0. 退出\n\n";
+
+ choose = -1;
+ while (choose != 0) {
+ cout << "请选择:";
+ cin >> choose;
+ switch (choose) {
+ case 1://创建顺序表
+ if (InitList_Sq(L))
+ cout << "成功建立顺序表\n\n";
+ else
+ cout << "顺序表建立失败\n\n";
+ break;
+ case 2: {//顺序表信息输入
+ i = 0;
+ L.elem = new Book[MAXSIZE];
+ if (!L.elem)
+ exit(OVERFLOW);
+ L.length = 0;
+ fstream file;
+ file.open("book.txt");
+ if (!file) {
+ cout << "错误!未找到文件!" << endl;
+ exit(ERROR);
+ }
+ file >> head_1 >> head_2 >> head_3;
+ while (!file.eof()) {
+ file >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price;
+ i++;
+ }
+ cout << "输入 book.txt 信息完毕\n\n";
+ L.length = i;
+ file.close();
+ }
+ break;
+ case 3://顺序表的取值
+ cout << "请输入一个位置用来取值:\n";
+ cin >> i;
+ temp = GetElem(L, i, e);
+ if (temp != 0) {
+ cout << "查找成功\n";
+ cout << "第" << i << "本图书的信息是:\n";
+ cout << left << setw(15) << e.id << "\t" << left << setw(50)
+ << e.name << "\t" << left << setw(5) << e.price << endl
+ << endl;
+ } else
+ cout << "查找失败!位置超出范围\n\n";
+ break;
+ case 4: //顺序表的查找
+ cout << "请输入所要查找价格:";
+ cin >> price;
+ temp = LocateElem_Sq(L, price);
+ if (temp != 0) {
+ cout << "查找成功\n";
+ cout << "该价格对应的书名为:" << L.elem[temp - 1].name << endl << endl;
+ } else
+ cout << "查找失败!没有这个价格对应的书籍\n\n";
+ break;
+ case 5: //顺序表的插入
+ cout << "请输入插入的位置和书本信息,包括:编号 书名 价格(用空格隔开):";
+ cin >> a;
+ cin >> e.id >> e.name >> e.price; //输入a和b,a代表插入的位置,b代表插入的数值(书本信息)
+ if (ListInsert_Sq(L, a, e))
+ cout << "插入成功.\n\n";
+ else
+ cout << "插入失败.\n\n";
+ break;
+ case 6: //顺序表的删除
+ cout << "请输入所要删除的书籍的位置:";
+ cin >> c;
+ if (ListDelete_Sq(L, c))
+ cout << "删除成功.\n\n";
+ else
+ cout << "删除失败.\n\n";
+ break;
+ case 7: //顺序表的输出
+ cout << "当前图书系统信息(顺序表)读出:\n";
+ for (i = 0; i < L.length; i++)
+ cout << left << setw(15) << L.elem[i].id << "\t" << left
+ << setw(50) << L.elem[i].name << "\t" << left
+ << setw(5) << L.elem[i].price << endl;
+ cout << endl;
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.1-2.5/book.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.1-2.5/book.txt
new file mode 100644
index 0000000..751c324
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.1-2.5/book.txt
@@ -0,0 +1,17 @@
+ISBN 书名 定价
+9787302257646 程序设计基础 25
+9787302219972 单片机技术及应用 32
+9787302203513 编译原理 46
+9787811234923 汇编语言程序设计教程 21
+9787512100831 计算机操作系统 17
+9787302265436 计算机导论实验指导 18
+9787302180630 实用数据结构 29
+9787302225065 数据结构(C语言版) 38
+9787302171676 C#面向对象程序设计 39
+9787302250692 C语言程序设计 42
+9787302150664 数据库原理 35
+9787302260806 Java编程与实践 56
+9787302252887 Java程序设计与应用教程 39
+9787302198505 嵌入式操作系统及编程 25
+9787302169666 软件测试 24
+9787811231557 Eclipse基础与应用 35
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.13-2.14/2.13-2.14.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.13-2.14/2.13-2.14.cpp
new file mode 100644
index 0000000..a57fe72
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.13-2.14/2.13-2.14.cpp
@@ -0,0 +1,189 @@
+#include
+#include
+#include
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
+typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
+
+struct Book {
+ string id;//ISBN
+ string name;//书名
+ double price;//定价
+};
+typedef struct DuLNode {
+ Book data; //数据域
+ struct DuLNode *prior; //直接前驱
+ struct DuLNode *next; //直接后继
+} DuLNode, *DuLinkList;
+
+string head_1, head_2, head_3;
+int length;
+
+Status InitDuList_L(DuLinkList &L) {
+ //构造一个空的双向链表L
+ L = new DuLNode; //生成新结点作为头结点,用头指针L指向头结点
+ L->next = NULL; //头结点的指针域置空
+ L->prior = NULL;
+ return OK;
+}
+
+DuLNode *GetElemP_DuL(DuLinkList L, int i) {
+ //在带头结点的双向链表L中查找第i个元素,返回结点的地址
+ int j;
+ DuLinkList p;
+ p = L->next;
+ j = 1; //初始化,p指向第一个结点,j为计数器
+ while (j < i && p) { //顺链域向后扫描,直到p指向第i个元素或p为空
+ p = p->next;
+ ++j;
+ }
+ if (!p || j > i)
+ return NULL; //第i个元素不存在
+ return p;
+} //GetElemP_DuL
+
+Status ListInsert_DuL(DuLinkList &L, int i, Book e) { //算法2.13 双向链表的插入
+ //在带头结点的双向链表L中第i个位置之前插入元素e,i的合法值为1<=i<=表长+1
+ DuLinkList s, p;
+ if (!(p = GetElemP_DuL(L, i))) //在L中确定第i个元素的位置指针p
+ return ERROR; //p为NULL时,第i个元素不存在
+ if (i == 1) {//在双向链表的第一个元素上插入
+ s = new DuLNode; //生成新结点s
+ s->data = e; //将结点s数据置为e
+ DuLinkList p = L->next;
+ L->next = s;
+ s->prior = L;
+ s->next = p;//将结点*s插入L中
+ p->prior = s;
+ ++length;
+ } else if (i == length) {//在双向链表的最后一个元素上插入
+ s = new DuLNode; //生成新结点s
+ s->data = e; //将结点s数据置为e
+ DuLinkList p = L;
+ while (p->next)
+ p = p->next;//将LinkList p指向双向链表结尾
+ p->next = s;
+ s->prior = p;//将结点*s插入到p的后面,插入到L中
+ s->next = NULL;
+ ++length;
+ } else {
+ s = new DuLNode; //生成新结点*s
+ s->data = e; //将结点*s数据域置为e
+ s->prior = p->prior; //将结点*s插入L中,此步对应图2.20①
+ p->prior->next = s; //对应图2.20②
+ s->next = p; //对应图2.20③
+ p->prior = s; //对应图2.20④
+ ++length;
+ }
+ return OK;
+} //ListInsert_DuL
+
+Status ListDelete_DuL(DuLinkList &L, int i) { //算法2.14 双向链表的删除
+ //删除带头结点的双向链表L中第i个位置之前插入元素e,i的合法值为1<=i<=表长
+ DuLinkList p;
+ if (!(p = GetElemP_DuL(L, i))) //在L中确定第i个元素的位置指针p
+ return ERROR; //p为NULL时,第i个元素不存在
+ if (i == 1)//删除双向链表的第一个元素
+ L = L->next;
+ else if (i == length) {//删除双向链表的最后一个元素
+ p->prior->next = NULL;
+ delete p;
+ --length;
+ return OK;
+ } else {
+ p->prior->next = p->next; //修改被删结点的前驱结点的后继指针,对应图2.21①
+ p->next->prior = p->prior; //修改被删结点的后继结点的前驱指针,对应图2.21②
+ delete p; //释放被删结点的空间
+ --length;
+ return OK;
+ }
+}//ListDelete_DuL
+
+void CreateDuList_L(DuLinkList &L) {
+ //正位序输入n个元素的值,建立带表头结点的双向链表L,同时建立前驱指针
+ DuLinkList r, p;
+ L = new DuLNode;
+ L->next = NULL; //先建立一个带头结点的空链表
+ r = L;//尾指针r指向头结点
+ length = 0;
+ fstream file;
+ file.open("book.txt");
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ file >> head_1 >> head_2 >> head_3;
+ while (!file.eof()) {
+ p = new DuLNode; //生成新结点
+ file >> p->data.id >> p->data.name >> p->data.price; //输入元素值
+ p->next = NULL;
+ r->next = p; //插入到表尾
+ r = p; //r指向新的尾结点
+ p->prior = L->prior; //插入到表头
+ L->prior = p;
+ length++;
+ }
+ file.close();
+} //CreateDuList_L
+
+int main() {
+ int a, choose;
+ Book e;
+ DuLinkList L, p;
+ cout << "1. 建立\n";
+ cout << "2. 输入\n";
+ cout << "3. 插入\n";
+ cout << "4. 删除\n";
+ cout << "5. 输出\n";
+ cout << "0. 退出\n\n";
+
+ choose = -1;
+ while (choose != 0) {
+ cout << "请选择:";
+ cin >> choose;
+ switch (choose) {
+ case 1: //建立一个双向链表
+ if (InitDuList_L(L))
+ cout << "成功建立双向链表!\n\n";
+ break;
+ case 2: //使用后插法创建双向链表
+ CreateDuList_L(L);
+ cout << "输入 book.txt 信息完毕\n\n";
+ break;
+ case 3: //双向链表的插入
+ cout << "请输入两个数分别代表插入的位置和数值(书的信息:编号&书名&价格):";
+ cin >> a;
+ cin >> e.id >> e.name >> e.price;
+ if (ListInsert_DuL(L, a, e))
+ cout << "插入成功.\n\n";
+ else
+ cout << "插入失败!\n\n";
+ break;
+ case 4: //双向链表的删除
+ cout << "请输入所要删除的书籍的位置:";
+ cin >> a;
+ if (ListDelete_DuL(L, a))
+ cout << "删除成功!\n\n";
+ else
+ cout << "删除失败!\n\n";
+ break;
+ case 5: //双向链表的输出
+ cout << "当前图书系统信息读出:\n";
+ p = L->next;
+ while (p) {
+ cout << left << setw(15) << p->data.id << "\t" << left << setw(
+ 50) << p->data.name << "\t" << left << setw(5)
+ << p->data.price << endl;
+ p = p->next;
+ }
+ cout << endl;
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.13-2.14/book.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.13-2.14/book.txt
new file mode 100644
index 0000000..751c324
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.13-2.14/book.txt
@@ -0,0 +1,17 @@
+ISBN 书名 定价
+9787302257646 程序设计基础 25
+9787302219972 单片机技术及应用 32
+9787302203513 编译原理 46
+9787811234923 汇编语言程序设计教程 21
+9787512100831 计算机操作系统 17
+9787302265436 计算机导论实验指导 18
+9787302180630 实用数据结构 29
+9787302225065 数据结构(C语言版) 38
+9787302171676 C#面向对象程序设计 39
+9787302250692 C语言程序设计 42
+9787302150664 数据库原理 35
+9787302260806 Java编程与实践 56
+9787302252887 Java程序设计与应用教程 39
+9787302198505 嵌入式操作系统及编程 25
+9787302169666 软件测试 24
+9787811231557 Eclipse基础与应用 35
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/2.15(链表).cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/2.15(链表).cpp
new file mode 100644
index 0000000..6c56c8f
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/2.15(链表).cpp
@@ -0,0 +1,109 @@
+#include
+#include
+#include
+using namespace std;
+
+#define ERROR 0
+
+typedef struct LNode {
+ int data; //结点的数据域
+ struct LNode *next; //结点的指针域
+} LNode, *List; //LinkList为指向结构体LNode的指针类型
+
+int n, m;
+char st = 'A';
+
+void InitList(List &L) //创建链表
+{
+ L = new LNode;
+ L->next = NULL;
+}
+
+void ListInput(List &L, int n) //链表数据的输入
+{
+ int i;
+ List p, r;
+ r = L;
+ char filename[20] = { 0 };
+ cout << "请输入顺序表L" << st << "的数据文件名称(文件名+“.txt”,如List" << st << ".txt):" << endl;
+ ++st;
+ gets(filename);
+ fstream file;
+ file.open(filename);
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ while (!file.eof()) {
+ p = new LNode;
+ file >> p->data;
+ p->next = NULL;
+ r->next = p;
+ r = p;
+ n++;
+ }
+ file.close();
+}
+
+bool LocateElem(List L, int e) //判断List里有没有e这个元素
+{
+ List p;
+ p = L->next;
+ while (p != NULL) {
+ if (p->data == e)
+ return true;
+ p = p->next;
+ }
+ return false;
+}
+
+void ListInsert(List &L, int e) //将e插入到List中
+{
+ List p;
+ p = new LNode;
+ p->data = e;
+ p->next = L->next;
+ L->next = p;
+}
+
+void ListOutput(List L) //输出List
+{
+ List p;
+ p = L->next;
+ while (p != NULL) {
+ cout << p->data << " ";
+ p = p->next;
+ }
+ cout << endl;
+}
+
+void unionList(List &LA, List LB) //算法2.15 线性表的合并(链表)
+{
+ //将所有在线性表LB中但不在LA中的数据元素插入到LA中
+ int e;
+ List p;
+ p = LB->next;
+ while (p != NULL) {
+ e = p->data;
+ if (!LocateElem(LA, e)) //LA中不存在和e相同的数据元素,则插入之
+ ListInsert(LA, e);
+ p = p->next;
+ }
+} //unionList
+
+int main() {
+ List LA, LB;
+
+ InitList(LA);
+
+ InitList(LB);
+
+ ListInput(LA, n);
+ ListInput(LB, m);
+
+ unionList(LA, LB);
+
+ cout << "LA和LB合并后的集合为:\n";
+ ListOutput(LA);
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/ListA.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/ListA.txt
new file mode 100644
index 0000000..44caecf
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/ListA.txt
@@ -0,0 +1 @@
+7 5 3 11
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/ListB.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/ListB.txt
new file mode 100644
index 0000000..7050af9
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(链表)/ListB.txt
@@ -0,0 +1 @@
+2 6 3
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/2.15(顺序表).cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/2.15(顺序表).cpp
new file mode 100644
index 0000000..12268fb
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/2.15(顺序表).cpp
@@ -0,0 +1,100 @@
+#include
+#include
+using namespace std;
+#define MAXSIZE 100
+#define ERROR 0
+typedef struct { //定义顺序表
+ int *elem;
+ int length;
+} List;
+int n, m;
+char st = 'A';
+
+void InitList(List &L) //创建顺序表
+{
+ L.elem = new int[MAXSIZE];
+ L.length = 0;
+}
+
+void ListInput(List &L, int n) //顺序表数据的输入
+{
+ int i = 0;
+ char filename[20] = { 0 };
+ cout << "请输入顺序表L" << st << "的数据文件名称(文件名+“.txt”,如List" << st << ".txt):" << endl;
+ ++st;
+ gets(filename);
+ fstream file;
+ file.open(filename);
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ while (!file.eof()) {
+ file >> L.elem[i];
+ i++;
+ }
+ L.length = i;
+
+}
+
+int ListLength(List L) //求顺序表的长度
+{
+ return L.length;
+}
+
+void GetElem(List L, int i, int &e) //求顺序表的第i个元素,并以e返回
+{
+ e = L.elem[i - 1];
+}
+
+bool LocateElem(List L, int e) //判断List里有没有e这个元素
+{
+ int i;
+ for (i = 0; i < L.length; i++)
+ if (e == L.elem[i])
+ return true;
+ return false;
+}
+
+void ListInsert(List &L, int e) //将e插入到List的最后
+{
+ L.elem[L.length] = e;
+ L.length++;
+}
+
+void ListOutput(List L) //输出List
+{
+ int i;
+ for (i = 0; i < L.length; i++)
+ cout << L.elem[i] << " ";
+ cout << endl;
+}
+
+void unionList(List &LA, List LB) //算法2.14 线性表的合并(顺序表)
+{
+ //将所有在线性表LB中但不在LA中的数据元素插入到LA中
+ int LA_len, LB_len, i, e;
+ LA_len = ListLength(LA);
+ LB_len = ListLength(LB); //求线性表的长度
+ for (i = 1; i <= LB_len; i++) {
+ GetElem(LB, i, e); //取LB中第i个数据元素赋给e
+ if (!LocateElem(LA, e)) //LA中不存在和e相同的数据元素,则插入之
+ ListInsert(LA, e);
+ }
+} //unionList
+
+int main() {
+ List LA, LB;
+ InitList(LA);
+ InitList(LB);
+
+ ListInput(LA, n);
+
+ ListInput(LB, m);
+
+ unionList(LA, LB);
+
+ cout << "LA和LB合并后的集合为:\n";
+ ListOutput(LA);
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/ListA.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/ListA.txt
new file mode 100644
index 0000000..44caecf
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/ListA.txt
@@ -0,0 +1 @@
+7 5 3 11
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/ListB.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/ListB.txt
new file mode 100644
index 0000000..7050af9
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.15(顺序表)/ListB.txt
@@ -0,0 +1 @@
+2 6 3
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/2.16.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/2.16.cpp
new file mode 100644
index 0000000..28567d4
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/2.16.cpp
@@ -0,0 +1,93 @@
+#include
+#include
+using namespace std;
+#define MAXSIZE 100
+#define ERROR 0
+
+typedef struct //定义顺序表
+{
+ int *elem;
+ int length;
+} SqList;
+int num_a = 0, num_b = 0;
+char st = 'A';
+
+void InitList_Sq(SqList &L, int n) //创建顺序表
+{
+ L.elem = new int[MAXSIZE];
+ if (!L.elem)
+ exit(0);
+ L.length = 0;
+}
+
+void input(SqList &L, int n) //依次往顺序表L里输入数据
+{
+ int i = 0;
+ char filename[20] = { 0 };
+ cout << "请输入非递减线性表" << st << "的数据文件名称(文件名+“.txt”,如List" << st << ".txt):" << endl;
+ ++st;
+ gets(filename);
+ fstream file;
+ file.open(filename);
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ while (!file.eof()) {
+ file >> L.elem[i];
+ i++;
+ }
+ L.length = i;
+}
+
+void output(SqList L) //依次输出顺序表里的每个元素
+{
+ int i;
+ for (i = 0; i < L.length; i++) {
+ if (i)
+ cout << ",";
+ cout << L.elem[i];
+ }
+}
+
+void MergeList_Sq(SqList LA, SqList LB, SqList &LC) //算法2.16 顺序有序表的合并
+{
+ //已知顺序有序表LA和LB的元素按值非递减排列
+ //归并LA和LB得到新的顺序有序表LC,LC的元素也按值非递减排列
+ int *pa, *pb, *pc, *pa_last, *pb_last;
+ pa = LA.elem;
+ pb = LB.elem; //指针pa和pb的初值分别指向两个表的第一次元素
+ LC.length = LA.length + LB.length; //新表长度为待合并两表的长度之和
+ LC.elem = new int[LC.length]; //为合并后的新表分配一个数组空间
+ pc = LC.elem; //指针pc指向新表的第一个元素
+ pa_last = LA.elem + LA.length - 1; //指针pa_last指向LA表的最后一个元素
+ pb_last = LB.elem + LB.length - 1; //指针pb_last指向LB表的最后一个元素
+ while (pa <= pa_last && pb <= pb_last) //两个表都非空
+ {
+ if (*pa <= *pb) //依次“摘取”两表中值较小的结点插入到LC表的最后
+ *pc++ = *pa++;
+ else
+ *pc++ = *pb++;
+ }
+ while (pa <= pa_last) //LB已到达表尾,依次将LA的剩余元素插入LC表的最后
+ *pc++ = *pa++;
+ while (pb <= pb_last) //LA已到达表尾,依次将LB的剩余元素插入LC表的最后
+ *pc++ = *pb++;
+} //MergeList_List
+
+int main() {
+ SqList La, Lb, Lc;
+
+ InitList_Sq(La, num_a); //La表的创建
+ input(La, num_a); //依次往顺序表La里输入数据
+
+ InitList_Sq(Lb, num_b); //Lb表的创建
+ input(Lb, num_b); //依次往顺序表La里输入数据
+
+ MergeList_Sq(La, Lb, Lc); //将顺序表La和Lb进行合并
+
+ cout << "非递减线性表A,B合并后的非递减线性表C为:\n"; //输出合并后的有序顺序表Lc
+ output(Lc);
+ cout << endl;
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/ListA.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/ListA.txt
new file mode 100644
index 0000000..9e7bd71
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/ListA.txt
@@ -0,0 +1 @@
+3 5 8 11
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/ListB.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/ListB.txt
new file mode 100644
index 0000000..4dd13df
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.16/ListB.txt
@@ -0,0 +1 @@
+2 6 8 9 11 15 20
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/2.17.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/2.17.cpp
new file mode 100644
index 0000000..0593c9d
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/2.17.cpp
@@ -0,0 +1,104 @@
+#include
+#include
+using namespace std;
+#define ERROR 0
+
+typedef struct LNode //定义单链表
+{
+ int data;
+ struct LNode *next;
+} LNode, *LinkList;
+
+int num_a, num_b;
+char st = 'A';
+
+void InitList_L(LinkList &L) //创建单链表
+{
+ L = new LNode;
+ L->next = NULL;
+}
+
+void input(LinkList &L, int n) //依次往单链表L里输入数据
+{
+ int i;
+ LNode *p, *r;
+ r = L;
+ char filename[20] = { 0 };
+ cout << "请输入非递减单链表" << st << "的数据文件名称(文件名+“.txt”,如List" << st << ".txt):" << endl;
+ ++st;
+ gets(filename);
+ fstream file;
+ file.open(filename);
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ while (!file.eof()) {
+ p = new LNode;
+ file >> p->data;
+ p->next = NULL;
+ r->next = p;
+ r = p;
+ n++;
+ }
+ file.close();
+}
+
+void output(LinkList L) //依次输出单链表里的每个元素
+{
+ int i = 0;
+ LNode *p;
+ p = L->next;
+ while (p) {
+ if (i)
+ cout << ",";
+ cout << p->data;
+ p = p->next;
+ i = 1;
+ }
+}
+
+void MergeList_L(LinkList &LA, LinkList &LB, LinkList &LC) //算法2.17 链式有序表的合并
+{
+ //已知单链表LA和LB的元素按值非递减排列
+ //归并LA和LB得到新的单链表LC,LC的元素也按值非递减排列
+ LinkList pa, pb, pc;
+ pa = LA->next;
+ pb = LB->next; //pa和pb的初值分别指向两个表的第一个结点
+ LC = LA; //用LA的头结点作为LC的头结点
+ pc = LC; //pc的初值指向LC的头结点
+ while (pa && pb) //两个表都非空
+ {
+ if (pa->data <= pb->data) //依次"摘取"两表中值较小的结点插入到LC表的最后
+ {
+ pc->next = pa; //将pa所指结点链接到pc所指结点之后
+ pc = pa;
+ pa = pa->next;
+ } else {
+ pc->next = pb; //将pb所指结点链接到pc所指结点之后
+ pc = pb;
+ pb = pb->next;
+ }
+ }
+ pc->next = pa ? pa : pb; //插入非空表的剩余段
+ delete LB; //释放LB的头结点
+} //MergeList_L()
+
+int main() {
+ LinkList La, Lb, Lc;
+
+ InitList_L(La); //La表的创建
+ input(La, num_a); //依次往单链表La里输入数据
+
+ InitList_L(Lb); //Lb表的创建
+ input(Lb, num_b); //依次往单链表La里输入数据
+
+ InitList_L(Lc);
+ MergeList_L(La, Lb, Lc); //将单链表La和Lb进行合并
+
+
+ cout << "非递减单链表A,B合并后的非递减单链表C为:\n"; //输出合并后的单链表Lc
+ output(Lc);
+ cout << endl;
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/ListA.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/ListA.txt
new file mode 100644
index 0000000..9e7bd71
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/ListA.txt
@@ -0,0 +1 @@
+3 5 8 11
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/ListB.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/ListB.txt
new file mode 100644
index 0000000..4dd13df
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.17/ListB.txt
@@ -0,0 +1 @@
+2 6 8 9 11 15 20
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/2.18-2.19.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/2.18-2.19.cpp
new file mode 100644
index 0000000..738e542
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/2.18-2.19.cpp
@@ -0,0 +1,124 @@
+#include
+#include
+#include
+using namespace std;
+#define ERROR 0
+
+typedef struct PNode {
+ float coef; //系数
+ int expn; //指数
+ struct PNode *next;
+} PNode, *Polynomial;
+string head_1, head_2;
+int temp;
+char st = 'A';
+
+void CreatPolyn(Polynomial &P, int m) //算法2.18 多项式的创建
+{
+ //输入m项的系数和指数,建立表示一个多项式的有序链表P
+ Polynomial q, pre, s;
+ int i;
+ P = new PNode;
+ P->next = NULL; //先建立一个带头结点的单链表
+ char filename[20] = { 0 };
+ cout << "请输入有关多项式p" << char(st + 32) << "系数和指数的数据文件名称(文件名+“.txt”,如Poly" << st << ".txt):" << endl;
+ ++st;
+ gets(filename);
+ fstream file;
+ file.open(filename);
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ file >> head_1 >> head_2;
+ while (!file.eof())
+ {
+ s = new PNode; //生成新结点
+ file >> s->coef >> s->expn; //输入元素值
+ pre = P; //pre用于保存q的前驱,初值为头结点
+ q = P->next;
+ while (q && q->expn < s->expn) //通过比较指数找到第一个大于输入项指数的项q
+ {
+ pre = q;
+ q = q->next;
+ } //while
+ s->next = q; //将输入项s插入到q和其前驱结点pre之间
+ pre->next = s;
+ }//for
+ file.close();
+} //CreatPolyn
+
+void AddPolyn(Polynomial &Pa, Polynomial &Pb) //算法2.19 多项式的相加
+{
+ //多项式加法:Pa=Pa+Pb,利用两个多项式的结点构成“和多项式”
+ Polynomial r, p1, p2, p3;
+ float sum;
+ p1 = Pa->next;
+ p2 = Pb->next; //p1和p2初值分别指向Pa和Pb的第一个结点
+ p3 = Pa; //p3指向和多项式的当前结点,初值为Pa
+ while (p1 && p2) //p1和p2均非空
+ {
+ if (p1->expn == p2->expn) //指数相等
+ {
+ sum = p1->coef + p2->coef; //sum保存两项的系数和
+ if (sum != 0) //系数和不为0
+ {
+ p1->coef = sum; //修改Pa当前结点p1的系数值为两项系数的和
+ p3->next = p1;
+ p3 = p1; //将修改后的Pa当前结点p1链在p3之后,p3指向p1
+ p1 = p1->next; //p1指向后一项
+ r = p2;
+ p2 = p2->next;
+ delete r; //删除Pb当前结点r
+ } else //系数和为0
+ {
+ r = p1;
+ p1 = p1->next;
+ delete r; //删除Pb当前结点p1
+ r = p2;
+ p2 = p2->next;
+ delete r; //删除Pb当前结点p2
+ }
+ } else if (p1->expn < p2->expn) //Pa当前结点p1的指数值小
+ {
+ p3->next = p1; //将p1链在p3之后
+ p3 = p1; //p3指向p1
+ p1 = p1->next; //p1指向后一项
+ } else //Pa当前结点p2的指数值小
+ {
+ p3->next = p2; //将p2链在p3之后
+ p3 = p2; //p3指向p2
+ p2 = p2->next; //p2指向后一项
+ }
+ } //while
+ p3->next = p1 ? p1 : p2; //插入非空多项式的剩余段
+ delete Pb; //释放Pb的头结点
+} //AddPolyn
+
+
+int main() {
+ Polynomial Pa, Pb;
+ Polynomial p;
+ int i;
+
+ //创建多项式Pa
+ CreatPolyn(Pa, temp); //调用函数,输入Pa每一项的系数和指数
+
+ //创建多项式Pb
+ CreatPolyn(Pb, temp); //调用函数,输入Pa每一项的系数和指数
+
+ AddPolyn(Pa, Pb);
+ cout << "多项式Pa和Pb相加后的结果是:\n";
+ p = Pa->next;
+ i = 0;
+ while (p) //输出相加后的结果,每一项以x^n表示
+ {
+ if (i)
+ cout << " + ";
+ cout << "(" << p->coef << ") * x^" << p->expn;
+ p = p->next;
+ i = 1;
+ }
+ cout << endl;
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/PolyA.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/PolyA.txt
new file mode 100644
index 0000000..694af45
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/PolyA.txt
@@ -0,0 +1,5 @@
+系数p 指数
+7 0
+3 1
+9 8
+5 17
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/PolyB.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/PolyB.txt
new file mode 100644
index 0000000..6dc6a9b
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.18-2.19/PolyB.txt
@@ -0,0 +1,4 @@
+系数p 指数
+8 1
+22 7
+-9 8
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.6-2.12/2.6-2.12.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.6-2.12/2.6-2.12.cpp
new file mode 100644
index 0000000..bf32fc1
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.6-2.12/2.6-2.12.cpp
@@ -0,0 +1,227 @@
+#include
+#include
+#include
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
+typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
+
+struct Book {
+ string id;//ISBN
+ string name;//书名
+ double price;//定价
+};
+typedef struct LNode {
+ Book data; //结点的数据域
+ struct LNode *next; //结点的指针域
+} LNode, *LinkList; //LinkList为指向结构体LNode的指针类型
+
+string head_1, head_2, head_3;
+int length;
+
+Status InitList_L(LinkList &L) { //算法2.6 单链表的初始化
+ //构造一个空的单链表L
+ L = new LNode; //生成新结点作为头结点,用头指针L指向头结点
+ L->next = NULL; //头结点的指针域置空
+ return OK;
+}
+
+Status GetElem_L(LinkList L, int i, Book &e) { //算法2.7 单链表的取值
+ //在带头结点的单链表L中查找第i个元素
+ //用e返回L中第i个数据元素的值
+ int j;
+ LinkList p;
+ p = L->next;
+ j = 1; //初始化,p指向第一个结点,j为计数器
+ while (j < i && p) { //顺链域向后扫描,直到p指向第i个元素或p为空
+ p = p->next; //p指向下一个结点
+ ++j; //计数器j相应加1
+ }
+ if (!p || j > i)
+ return ERROR; //i值不合法i>n或i<=0
+ e = p->data; //取第i个结点的数据域
+ return OK;
+} //GetElem_L
+
+LNode *LocateElem_L(LinkList L, int e) { //算法2.8 按值查找
+ //在带头结点的单链表L中查找值为e的元素
+ LinkList p;
+ p = L->next;
+ while (p && p->data.price != e)//顺链域向后扫描,直到p为空或p所指结点的数据域等于e
+ p = p->next; //p指向下一个结点
+ return p; //查找成功返回值为e的结点地址p,查找失败p为NULL
+} //LocateElem_L
+
+Status ListInsert_L(LinkList &L, int i, Book &e) { //算法2.9 单链表的插入
+ //在带头结点的单链表L中第i个位置插入值为e的新结点
+ int j;
+ LinkList p, s;
+ p = L;
+ j = 0;
+ while (p && j < i - 1) {
+ p = p->next;
+ ++j;
+ }//查找第i?1个结点,p指向该结点
+ if (!p || j > i - 1)
+ return ERROR; //i>n+1或者i<1
+ s = new LNode; //生成新结点*s
+ s->data = e; //将结点*s的数据域置为e
+ s->next = p->next; //将结点*s的指针域指向结点ai
+ p->next = s; //将结点*p的指针域指向结点*s
+ ++length;
+ return OK;
+} //ListInsert_L
+
+Status ListDelete_L(LinkList &L, int i) { //算法2.9 单链表的删除
+ //在带头结点的单链表L中,删除第i个位置
+ LinkList p, q;
+ int j;
+ p = L;
+ j = 0;
+ while ((p->next) && (j < i - 1)) //查找第i?1个结点,p指向该结点
+ {
+ p = p->next;
+ ++j;
+ }
+ if (!(p->next) || (j > i - 1))
+ return ERROR; //当i>n或i<1时,删除位置不合理
+ q = p->next; //临时保存被删结点的地址以备释放
+ p->next = q->next; //改变删除结点前驱结点的指针域
+ delete q; //释放删除结点的空间
+ --length;
+ return OK;
+} //ListDelete_L
+
+void CreateList_H(LinkList &L, int n) { //算法2.11 前插法创建单链表
+ //逆位序输入n个元素的值,建立到头结点的单链表L
+ LinkList p;
+ L = new LNode;
+ L->next = NULL; //先建立一个带头结点的空链表
+ length = 0;
+ fstream file;
+ file.open("book.txt");
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ file >> head_1 >> head_2 >> head_3;
+ while (!file.eof()) {
+ p = new LNode; //生成新结点*p
+ file >> p->data.id >> p->data.name >> p->data.price; //输入元素值赋给新结点*p的数据域
+ p->next = L->next;
+ L->next = p; //将新结点*p插入到头结点之后
+ length++;//同时对链表长度进行统计
+ }
+ file.close();
+} //CreateList_F
+
+void CreateList_R(LinkList &L, int n) { //算法2.12 后插法创建单链表
+ //正位序输入n个元素的值,建立带表头结点的单链表L
+ LinkList p, r;
+ L = new LNode;
+ L->next = NULL; //先建立一个带头结点的空链表
+ r = L; //尾指针r指向头结点
+ length = 0;
+ fstream file; //打开文件进行读写操作
+ file.open("book.txt");
+ if (!file) {
+ cout << "未找到相关文件,无法打开!" << endl;
+ exit(ERROR);
+ }
+ file >> head_1 >> head_2 >> head_3;
+ while (!file.eof()) { //将文件中的信息运用后插法插入到链表中
+ p = new LNode;//生成新结点
+ file >> p->data.id >> p->data.name >> p->data.price;//输入元素值赋给新结点*p的数据域
+ p->next = NULL;
+ r->next = p;//将新结点*p插入尾结点*r之后
+ r = p;//r指向新的尾结点*p
+ length++; //同时对链表长度进行统计
+ }
+ file.close();
+} //CreateList_L
+
+int main() {
+ int a, n, choose;
+ double price;
+ Book e;
+ LinkList L, p;
+ cout << "1. 建立\n";
+ cout << "2. 输入\n";
+ cout << "3. 取值\n";
+ cout << "4. 查找\n";
+ cout << "5. 插入\n";
+ cout << "6. 删除\n";
+ cout << "7. 输出\n";
+ cout << "0. 退出\n\n";
+
+ choose = -1;
+ while (choose != 0) {
+ cout << "请选择:";
+ cin >> choose;
+ switch (choose) {
+ case 1: //建立一个单链表
+ if (InitList_L(L))
+ cout << "成功建立链表!\n\n";
+ break;
+ case 2: //使用后插法创建单链表
+ CreateList_R(L, length);
+ cout << "输入 book.txt 信息完毕\n\n";
+ break;
+ case 3: //单链表的按序号取值
+ cout << "请输入一个位置用来取值:";
+ cin >> a;
+ if (GetElem_L(L, a, e)) {
+ cout << "查找成功\n";
+ cout << "第" << a << "本图书的信息是:\n";
+ cout << left << setw(15) << e.id << "\t" << left << setw(50)
+ << e.name << "\t" << left << setw(5) << e.price << endl
+ << endl;
+ } else
+ cout << "查找失败\n\n";
+ break;
+ case 4: //单链表的按值查找
+ cout << "请输入所要查找价格:";
+ cin >> price;
+ if (LocateElem_L(L, price) != NULL) {
+ cout << "查找成功\n";
+ cout << "该价格对应的书名为:" << LocateElem_L(L, price)->data.name
+ << endl << endl;
+ } else
+ cout << "查找失败! 定价" << price << " 没有找到\n\n";
+ break;
+ case 5: //单链表的插入
+ cout << "请输入插入的位置和书的信息,包括:编号 书名 价格(用空格隔开):";
+ cin >> a;
+ cin >> e.id >> e.name >> e.price;
+ if (ListInsert_L(L, a, e))
+ cout << "插入成功.\n\n";
+ else
+ cout << "插入失败!\n\n";
+ break;
+ case 6: //单链表的删除
+ cout << "请输入所要删除的书籍的位置:";
+ cin >> a;
+ if (ListDelete_L(L, a))
+ cout << "删除成功!\n\n";
+ else
+ cout << "删除失败!\n\n";
+ break;
+ case 7: //单链表的输出
+ cout << "当前图书系统信息(链表)读出:\n";
+ p = L->next;
+ while (p) {
+ cout << left << setw(15) << p->data.id << "\t" << left << setw(
+ 50) << p->data.name << "\t" << left << setw(5)
+ << p->data.price << endl;
+ p = p->next;
+ }
+ cout << endl;
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.6-2.12/book.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.6-2.12/book.txt
new file mode 100644
index 0000000..751c324
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第2章/2.6-2.12/book.txt
@@ -0,0 +1,17 @@
+ISBN 书名 定价
+9787302257646 程序设计基础 25
+9787302219972 单片机技术及应用 32
+9787302203513 编译原理 46
+9787811234923 汇编语言程序设计教程 21
+9787512100831 计算机操作系统 17
+9787302265436 计算机导论实验指导 18
+9787302180630 实用数据结构 29
+9787302225065 数据结构(C语言版) 38
+9787302171676 C#面向对象程序设计 39
+9787302250692 C语言程序设计 42
+9787302150664 数据库原理 35
+9787302260806 Java编程与实践 56
+9787302252887 Java程序设计与应用教程 39
+9787302198505 嵌入式操作系统及编程 25
+9787302169666 软件测试 24
+9787811231557 Eclipse基础与应用 35
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/3.1-3.4.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/3.1-3.4.cpp
new file mode 100644
index 0000000..9b0da70
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/3.1-3.4.cpp
@@ -0,0 +1,117 @@
+/***顺序栈的实现***/
+
+#include
+#include
+using namespace std;
+
+//顺序栈定义
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+#define MAXSIZE 100//顺序栈存储空间的初始分配量
+typedef int Status;
+typedef char SElemType;
+
+typedef struct {
+ SElemType *base;//栈底指针
+ SElemType *top;//栈顶指针
+ int stacksize;//栈可用的最大容量
+} SqStack;
+
+//算法3.1 顺序栈的初始化
+Status InitStack(SqStack &S) {
+ //构造一个空栈S
+ S.base = new SElemType[MAXSIZE];//为顺序栈动态分配一个最大容量为MAXSIZE的数组空间
+ if (!S.base)
+ exit(OVERFLOW); //存储分配失败
+ S.top = S.base; //top初始为base,空栈
+ S.stacksize = MAXSIZE; //stacksize置为栈的最大容量MAXSIZE
+ return OK;
+}
+//算法3.2 顺序栈的入栈
+Status Push(SqStack &S, SElemType e) {
+ // 插入元素e为新的栈顶元素
+ if (S.top - S.base == S.stacksize)
+ return ERROR; //栈满
+ *(S.top++) = e; //元素e压入栈顶,栈顶指针加1
+ return OK;
+}
+//算法3.3 顺序栈的出栈
+Status Pop(SqStack &S, SElemType &e) {
+ //删除S的栈顶元素,用e返回其值
+ if (S.base == S.top)
+ return ERROR;//栈空
+ e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
+ return OK;
+}
+//算法3.4 取顺序栈的栈顶元素
+char GetTop(SqStack S) {//返回S的栈顶元素,不修改栈顶指针
+ if (S.top != S.base) //栈非空
+ return *(S.top - 1); //返回栈顶元素的值,栈顶指针不变
+}
+
+int main() {
+ SqStack s;
+ int choose, flag = 0;
+ SElemType j, e, t;
+ cout << "1.初始化\n";
+ cout << "2.入栈\n";
+ cout << "3.读栈顶元素\n";
+ cout << "4.出栈\n";
+ cout << "0.退出\n\n";
+
+ choose = -1;
+ while (choose != 0) {
+ cout << "请选择:";
+ cin >> choose;
+ switch (choose) {
+ case 1://算法3.1 顺序栈的初始化
+ if (InitStack(s)) {
+ flag = 1;
+ cout << "成功对栈进行初始化\n\n";
+ } else
+ cout << "初始化栈失败\n\n";
+ break;
+ case 2: {//算法3.2 顺序栈的入栈
+ fstream file;
+ file.open("SqStack.txt");//
+ if (!file) {
+ cout << "错误!未找到文件!\n\n" << endl;
+ exit(ERROR);
+ }
+ if (flag) {
+ flag = 1;
+ cout << "进栈元素依次为:\n";
+ while (!file.eof()) {
+ file >> j;
+ if (file.fail())
+ break;
+ else {
+ Push(s, j);
+ cout << j << " ";
+ }
+ }
+ cout << endl << endl;
+ } else
+ cout << "栈未建立,请重新选择\n\n";
+ file.close();
+ }
+ break;
+ case 3://算法3.3 顺序栈的出栈
+ if(flag != -1 && flag != 0)
+ cout << "栈顶元素为:\n" << GetTop(s) << endl << endl;
+ else
+ cout << "栈中无元素,请重新选择\n" << endl;
+ break;
+ case 4://算法3.4 取顺序栈的栈顶元素
+ cout << "依次弹出的栈顶元素为:\n";
+ while (Pop(s, t)){
+ flag = -1;
+ cout << t << " ";
+ }
+ cout << endl << endl;
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/3.1-3.4.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/3.1-3.4.suo
new file mode 100644
index 0000000..2eca1be
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/3.1-3.4.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/SqStack.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/SqStack.txt
new file mode 100644
index 0000000..8422d40
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.1-3.4/SqStack.txt
@@ -0,0 +1,4 @@
+A
+B
+C
+D
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.cpp
new file mode 100644
index 0000000..9b0fdc0
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.cpp
@@ -0,0 +1,16 @@
+/***Hanoi塔问题***/
+
+#include"3.10.h"
+
+int main() {
+ int n;
+ char a, b, c;
+ a = '1';
+ b = '2';
+ c = '3';
+ cout << "请输入初始第一个柱子上的圆盘个数:" << endl;
+ cin >> n;
+ cout << "将第一个柱子上的圆盘全部移动到第三个柱子上的过程为:" << endl;
+ Hanoi(n, a, b, c);
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.h b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.h
new file mode 100644
index 0000000..a8a2692
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.h
@@ -0,0 +1,20 @@
+#include
+using namespace std;
+
+int m = 0;//£¨mÊdzõֵΪ0µÄȫ¾ֱäÁ¿£¬¶ᶯ¼ÆÊý£©
+void move(char A, int n, char C) // °ᶯ²Ù×÷
+{
+ cout << "µÚ" << ++m << "²½," << "½«±àºÅΪ" << n << "µÄԲÅ̴ӵÚ" << A << "¸öÖù×ÓÉÏÒƵ½µÚ" << C
+ << "¸öÖù×ÓÉÏ" << endl;
+}
+
+//Ë㷨3.10¡¡HanoiËþÎÊÌâµĵݹéË㷨
+void Hanoi(int n, char A, char B, char C) {//½«Ëþ×ùAÉϵÄn¸öԲÅ̰´¹æÔò°ᵽCÉϣ¬B×ö¸¨ÖúËþ
+ if (n == 1)
+ move(A, 1, C); //½«±àºÅΪ1µÄԲÅ̴ÓAÒƵ½C
+ else {
+ Hanoi(n - 1, A, C, B); //½«AÉϱàºÅΪ1ÖÁn-1µÄԲÅÌÒƵ½B£¬C×ö¸¨ÖúËþ
+ move(A, n, C);//½«±àºÅΪnµÄԲÅ̴ÓAÒƵ½C
+ Hanoi(n - 1, B, A, C); //½«BÉϱàºÅΪ1ÖÁn-1µÄԲÅÌÒƵ½C£¬A×ö¸¨ÖúËþ
+ }
+}
\ No newline at end of file
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.suo
new file mode 100644
index 0000000..cede1be
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.10/3.10.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/3.11-3.15.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/3.11-3.15.cpp
new file mode 100644
index 0000000..3ccb64b
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/3.11-3.15.cpp
@@ -0,0 +1,123 @@
+/***循环队列基本操作***/
+
+#include
+#include
+using namespace std;
+
+#define MAXQSIZE 100
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef char QElemType;
+typedef char SElemType;
+typedef int Status;
+
+typedef struct {
+ QElemType *base;//初始化时动态分配存储空间
+ int front;//头指针
+ int rear;//尾指针
+} SqQueue;
+
+//算法3.11 循环队列的初始化
+Status InitQueue(SqQueue &Q) {//构造一个空队列Q
+ Q.base = new QElemType[MAXQSIZE]; //为队列分配一个最大容量为MAXSIZE的数组空间
+ if (!Q.base)
+ exit(OVERFLOW); //存储分配失败
+ Q.front = Q.rear = 0; //头指针和尾指针置为零,队列为空
+ return OK;
+}
+
+//算法3.12 求循环队列的长度
+int QueueLength(SqQueue Q) {//返回Q的元素个数,即队列的长度
+ return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
+}
+
+//算法3.13 循环队列的入队
+Status EnQueue(SqQueue &Q, QElemType e) {//插入元素e为Q的新的队尾元素
+ if ((Q.rear + 1) % MAXQSIZE == Q.front) //尾指针在循环意义上加1后等于头指针,表明队满
+ return ERROR;
+ Q.base[Q.rear] = e; //新元素插入队尾
+ Q.rear = (Q.rear + 1) % MAXQSIZE; //队尾指针加1
+ return OK;
+}
+
+//算法3.14 循环队列的出队
+Status DeQueue(SqQueue &Q, QElemType &e) {//删除Q的队头元素,用e返回其值
+ if (Q.front == Q.rear)
+ return ERROR; //队空
+ e = Q.base[Q.front]; //保存队头元素
+ Q.front = (Q.front + 1) % MAXQSIZE; //队头指针加1
+ return OK;
+}
+
+//算法3.15 取循环队列的队头元素
+SElemType GetHead(SqQueue Q) {//返回Q的队头元素,不修改队头指针
+ if (Q.front != Q.rear) //队列非空
+ return Q.base[Q.front]; //返回队头元素的值,队头指针不变
+}
+
+int main() {
+ int choose, flag = 0;
+ SqQueue Q;
+ QElemType e, j;
+ cout << "1.初始化\n";
+ cout << "2.入队\n";
+ cout << "3.读队头元素\n";
+ cout << "4.出队\n";
+ cout << "0.退出\n\n";
+
+ choose = -1;
+ while (choose != 0) {
+ cout << "请选择:";
+ cin >> choose;
+ switch (choose) {
+ case 1://算法3.16 链队的初始化
+ if (InitQueue(Q)) {
+ flag = 1;
+ cout << "成功对队列进行初始化\n\n";
+ } else
+ cout << "初始化队列失败\n\n";
+ break;
+ case 2: {//算法3.17 链队的入队
+ fstream file;
+ file.open("QNode.txt");
+ if (!file) {
+ cout << "错误!未找到文件!\n\n" << endl;
+ exit(ERROR);
+ }
+ if (flag) {
+ flag = 1;
+ cout << "入队的元素依次为:\n";
+ while (!file.eof()) {
+ file >> j;
+ if (file.fail())
+ break;
+ else {
+ EnQueue(Q, j);
+ cout << j << " ";
+ }
+ }
+ cout << endl << endl;
+ } else
+ cout << "队列未建立,请重新选择\n\n";
+ file.close();
+ }
+ break;
+ case 3://算法3.19 取链队的队头元素
+ if (flag != -1 && flag != 0)
+ cout << "队头元素为:\n" << GetHead(Q) << endl << endl;
+ else
+ cout << "队列中无元素,请重新选择\n" << endl;
+ break;
+ case 4://算法3.18 链队的出队
+ cout << "依次弹出的队头元素为:\n";
+ while (DeQueue(Q, e)) {
+ flag = -1;
+ cout << e << " ";
+ }
+ cout << endl << endl;
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/3.11-3.15.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/3.11-3.15.suo
new file mode 100644
index 0000000..79c5e44
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/3.11-3.15.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/QNode.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/QNode.txt
new file mode 100644
index 0000000..8422d40
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.11-3.15/QNode.txt
@@ -0,0 +1,4 @@
+A
+B
+C
+D
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/3.16-3.19.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/3.16-3.19.cpp
new file mode 100644
index 0000000..72a6016
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/3.16-3.19.cpp
@@ -0,0 +1,126 @@
+/***链队的基本操作***/
+
+#include
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef char QElemType;
+typedef int Status;
+typedef char SElemType;
+
+//- - - - - 队列的链式存储结构- - - - -
+typedef struct QNode {
+ QElemType data;
+ struct QNode *next;
+} QNode, *QueuePtr;
+typedef struct {
+ QueuePtr front; //队头指针
+ QueuePtr rear; //队尾指针
+} LinkQueue;
+
+//算法3.16 链队的初始化
+Status InitQueue(LinkQueue &Q) {//构造一个空队列Q
+ Q.front = Q.rear = new QNode; //生成新结点作为头结点,队头和队尾指针指向此结点
+ Q.front->next = NULL; //头结点的指针域置空
+ return OK;
+}
+
+//算法3.17 链队的入队
+Status EnQueue(LinkQueue &Q, QElemType e) {//插入元素e为Q的新的队尾元素
+ QueuePtr p;
+ p = new QNode; //为入队元素分配结点空间,用指针p指向
+ p->data = e; //将新结点数据域置为e
+ p->next = NULL;
+ Q.rear->next = p; //将新结点插入到队尾
+ Q.rear = p; //修改队尾指针
+ return OK;
+}
+
+//算法3.18 链队的出队
+Status DeQueue(LinkQueue &Q, QElemType &e) {//删除Q的队头元素,用e返回其值
+ QueuePtr p;
+ if (Q.front == Q.rear)
+ return ERROR; //若队列空,则返回ERROR
+ p = Q.front->next; //p指向队头元素
+ e = p->data; //e保存队头元素的值
+ Q.front->next = p->next; //修改头指针
+ if (Q.rear == p)
+ Q.rear = Q.front; //最后一个元素被删,队尾指针指向头结点
+ delete p; //释放原队头元素的空间
+ return OK;
+}
+
+//算法3.19 取链队的队头元素
+SElemType GetHead(LinkQueue Q) {//返回Q的队头元素,不修改队头指针
+ if (Q.front != Q.rear) //队列非空
+ return Q.front->next->data; //返回队头元素的值,队头指针不变
+}
+
+int main() {
+ int choose, flag = 0;
+ LinkQueue Q;
+ QElemType e, j;
+ cout << "1.初始化\n";
+ cout << "2.入队\n";
+ cout << "3.读队头元素\n";
+ cout << "4.出队\n";
+ cout << "0.退出\n\n";
+
+ choose = -1;
+ while (choose != 0) {
+ cout << "请选择:";
+ cin >> choose;
+ switch (choose) {
+ case 1://算法3.16 链队的初始化
+ if (InitQueue(Q)) {
+ flag = 1;
+ cout << "成功对队列进行初始化\n\n";
+ } else
+ cout << "初始化队列失败\n\n";
+ break;
+ case 2: {//算法3.17 链队的入队
+ fstream file;
+ file.open("QNode.txt");
+ if (!file) {
+ cout << "错误!未找到文件!\n\n" << endl;
+ exit(ERROR);
+ }
+ if (flag) {
+ flag = 1;
+ cout << "入队的元素依次为:\n";
+ while (!file.eof()) {
+ file >> j;
+ if (file.fail())
+ break;
+ else {
+ EnQueue(Q, j);
+ cout << j << " ";
+ }
+ }
+ cout << endl << endl;
+ } else
+ cout << "队列未建立,请重新选择\n\n";
+ file.close();
+ }
+ break;
+ case 3://算法3.19 取链队的队头元素
+ if (flag != -1 && flag != 0)
+ cout << "队头元素为:\n" << GetHead(Q) << endl << endl;
+ else
+ cout << "队列中无元素,请重新选择\n" << endl;
+ break;
+ case 4://算法3.18 链队的出队
+ cout << "依次弹出的队头元素为:\n";
+ while (DeQueue(Q, e)) {
+ flag = -1;
+ cout << e << " ";
+ }
+ cout << endl << endl;
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/3.16-3.19.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/3.16-3.19.suo
new file mode 100644
index 0000000..abbde2c
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/3.16-3.19.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/QNode.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/QNode.txt
new file mode 100644
index 0000000..8422d40
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.16-3.19/QNode.txt
@@ -0,0 +1,4 @@
+A
+B
+C
+D
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.20/3.20.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.20/3.20.cpp
new file mode 100644
index 0000000..90b500f
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.20/3.20.cpp
@@ -0,0 +1,69 @@
+/***链栈实现数制的转换***/
+
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+
+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, int e) {
+ LinkStack p;
+ p = new SNode;
+ if (!p) {
+ return OVERFLOW;
+ }
+ p->data = e;
+ p->next = S;
+ S = p;
+ return OK;
+}
+Status Pop(LinkStack &S, int &e) {
+ LinkStack p;
+ if (!S)
+ return ERROR;
+ e = S->data;
+ p = S;
+ S = S->next;
+ delete p;
+ return OK;
+}
+//算法3.20 数制的转换(链栈实现)
+void conversion(int N) {//对于任意一个非负十进制数,打印输出与其等值的八进制数
+ int e;
+ LinkStack S;
+ InitStack(S); //初始化空栈S
+ while (N) //当N非零时,循环
+ {
+ Push(S, N % 8); //把N与8求余得到的八进制数压入栈S
+ N = N / 8; //N更新为N与8的商
+ }
+ while (!StackEmpty(S)) //当栈S非空时,循环
+ {
+ Pop(S, e); //弹出栈顶元素e
+ cout << e; //输出e
+ }
+}
+int main() {//对于输入的任意一个非负十进制数,打印输出与其等值的八进制数
+ int n, e;
+ cout << "输入一个非负十进制数:" << endl;
+ cin >> n;
+
+ conversion(n);
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.20/3.20.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.20/3.20.suo
new file mode 100644
index 0000000..12c7d53
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.20/3.20.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.21/3.21.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.21/3.21.cpp
new file mode 100644
index 0000000..75562ac
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.21/3.21.cpp
@@ -0,0 +1,96 @@
+/***链栈实现括号匹配***/
+
+#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) {
+ if (!S)
+ return ERROR;
+ return S->data;
+}
+
+//算法3.21 括号的匹配
+Status Matching() {//检验表达式中所含括号是否正确匹配,如果匹配,则返回true,否则返回false
+ //表达式以“#”结束
+ char ch;
+ SElemType x;
+ LinkStack S;
+ InitStack(S); //初始化空栈
+ int flag = 1; //标记匹配结果以控制循环及返回结果
+ cin >> ch; //读入第一个字符
+ while (ch != '#' && flag) //假设表达式以“#”结尾
+ {
+ switch (ch) {
+ case '[' :
+ case '(': //若是左括号,则将其压入栈
+ Push(S, ch);
+ break;
+ case ')': //若是“)”,则根据当前栈顶元素的值分情况考虑
+ if (!StackEmpty(S) && GetTop(S) == '(')
+ Pop(S, x); //若栈非空且栈顶元素是“(”,则正确匹配
+ else
+ flag = 0; //若栈空或栈顶元素不是“(”,则错误失败
+ break;
+ case ']': //若是“]”,则根据当前栈顶元素的值分情况考虑
+ if (!StackEmpty(S) && GetTop(S) == '[')
+ Pop(S, x); //若栈非空且栈顶元素是“[”,则正确匹配
+ else
+ flag = 0; //若栈空或栈顶元素不是“[”,则错误匹配
+ break;
+ } //switch
+ cin >> ch; //继续读入下一个字符
+ } //while
+ if (StackEmpty(S) && flag)
+ return true; //匹配成功
+ else
+ return false; //匹配失败
+}
+int main() {
+ LinkStack S;
+ cout << "请输入待匹配的表达式,以“#”结束:" << endl;
+ int flag = (int) Matching();
+ if (flag)
+ cout << "括号匹配成功!" << endl;
+ else
+ cout << "括号匹配失败!" << endl;
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.21/3.21.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.21/3.21.suo
new file mode 100644
index 0000000..e939021
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.21/3.21.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.cpp
new file mode 100644
index 0000000..572683e
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.cpp
@@ -0,0 +1,29 @@
+#include"3.22.h"
+
+int menu() {
+ int c;
+ cout << "0-9以内的多项式计算" << endl;
+ cout << "1.计算" << endl;
+ cout << "0.退出\n" << endl;
+ cout << "选择:";
+ cin >> c;
+ return c;
+}
+
+void main() {
+ while (1) {
+ switch (menu()) {
+ case 1: {
+ cout << "请输入要计算的表达式(操作数和结果都在0-9的范围内,以#结束):" << endl;
+ char res = EvaluateExpression();//算法3.22 表达式求值
+ cout << "计算结果为" << res - 48 << endl << endl;
+ }
+ break;
+ case 0:
+ cout << "退出成功\n" << endl;
+ exit(0);
+ default:
+ break;
+ }
+ }
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.h b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.h
new file mode 100644
index 0000000..d21e0f4
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.h
@@ -0,0 +1,117 @@
+/***Á´ջʵÏֱí´ïʽÇóֵ***/
+
+#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) {
+ if (!S)
+ return ERROR;
+
+ return S->data;
+}
+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;
+}
+
+//Ë㷨3.22¡¡±í´ïʽÇóֵ
+char EvaluateExpression() {//ËãÊõ±í´ïʽÇóֵµÄËã·ûÓÅÏÈË㷨£¬ÉèOPTRºÍOPND·ֱðΪÔËËã·ûջºͲÙ×÷Êýջ
+ LinkStack OPTR, OPND;
+ char ch, theta, a, b, x, top;
+ InitStack(OPND); //³õʼ»¯OPNDջ
+ InitStack(OPTR); //³õʼ»¯OPTRջ
+ Push(OPTR, '#'); //½«±í´ïʽÆðʼ·û¡°#¡±ѹÈëOPTRջ
+ cin >> ch;
+ while (ch != '#' || (GetTop(OPTR) != '#')) //±í´ïʽûÓÐɨÃèÍê±ϻòOPTRµÄջ¶¥ԪËز»Ϊ¡°#¡±
+ {
+ if (!In(ch)) {
+ Push(OPND, ch);
+ cin >> ch;
+ } //ch²»ÊÇÔËËã·ûÔò½øOPNDջ
+ else
+ switch (Precede(GetTop(OPTR), ch)) //±ȽÏOPTRµÄջ¶¥ԪËغÍchµÄÓÅÏȼ¶
+ {
+ case '<':
+ Push(OPTR, ch);
+ cin >> ch; //µ±ǰ×ַûchѹÈëOPTRջ£¬¶ÁÈëÏÂһ×ַûch
+ break;
+ case '>':
+ Pop(OPTR, theta); //µ¯³öOPTRջ¶¥µÄÔËËã·û
+ Pop(OPND, b);
+ Pop(OPND, a); //µ¯³öOPNDջ¶¥µÄÁ½¸öÔËËãÊý
+ Push(OPND, Operate(a, theta, b)); //½«ÔËËã½á¹ûѹÈëOPNDջ
+ break;
+ case '=': //OPTRµÄջ¶¥ԪËØÊǡ°(¡±ÇÒchÊǡ°)¡±
+ Pop(OPTR, x);
+ cin >> ch; //µ¯³öOPTRջ¶¥µġ°(¡±£¬¶ÁÈëÏÂһ×ַûch
+ break;
+ } //switch
+ } //while
+ return GetTop(OPND); //OPNDջ¶¥ԪËؼ´Ϊ±í´ïʽÇóֵ½á¹û
+}
\ No newline at end of file
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.suo
new file mode 100644
index 0000000..0f6aae8
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.22/3.22.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.cpp
new file mode 100644
index 0000000..8a261a3
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.cpp
@@ -0,0 +1,18 @@
+#include"3.23.h"
+
+Person dancer[MAXQSIZE];
+int main() {
+ int i = 0;
+ fstream file;
+ file.open("DancePartner.txt");
+ if (!file) {
+ cout << "错误!未找到文件!\n\n" << endl;
+ exit( ERROR);
+ }
+ while (!file.eof()) {
+ file >> dancer[i].name >> dancer[i].sex;
+ i++;
+ }
+ DancePartner(dancer, i + 1);
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.h b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.h
new file mode 100644
index 0000000..fb2721d
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.h
@@ -0,0 +1,89 @@
+#include
+#include
+#include
+#define MAXQSIZE 100//¶ÓÁпÉÄܴﵽµÄ×î´È
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+using namespace std;
+
+typedef struct {
+ char name[20]; //ÐÕÃû
+ char sex; //ÐԱð£¬'F'±íʾŮÐԣ¬'M'±íʾÄÐÐÔ
+} Person;
+
+//- - - - - ¶ÓÁеÄ˳Ðò´洢½ṹ- - - - -
+typedef struct {
+ Person *base; //¶ÓÁÐÖÐÊý¾ÝԪËØÀàÐÍΪPerson
+ int front; //ͷָÕë
+ int rear; //βָÕë
+} SqQueue;
+
+SqQueue Mdancers, Fdancers; //·ֱð´æ·ÅÄÐʿºÍŮʿÈë¶ÓÕ߶ÓÁÐ
+
+int InitQueue(SqQueue &Q) {//¹¹Ôìһ¸ö¿նÓÁÐQ
+ Q.base = new Person[MAXQSIZE]; //Ϊ¶ÓÁзÖÅäһ¸ö×î´óÈÝÁ¿ΪMAXSIZEµÄÊý×é¿ռä
+ if (!Q.base)
+ exit( OVERFLOW); //´洢·ÖÅäʧ°Ü
+ Q.front = Q.rear = 0; //ͷָÕëºÍβָÕëÖÃΪÁ㣬¶ÓÁÐΪ¿Õ
+ return OK;
+}
+
+int EnQueue(SqQueue &Q, Person e) {//²åÈëԪËØeΪQµÄеĶÓβԪËØ
+ if ((Q.rear + 1) % MAXQSIZE == Q.front) //βָÕëÔÚѭ»·ÒâÒåÉϼÓ1ºóµÈÓÚͷָÕ룬±íÃ÷¶ÓÂú
+ return ERROR;
+ Q.base[Q.rear] = e; //ÐÂԪËزåÈë¶Óβ
+ Q.rear = (Q.rear + 1) % MAXQSIZE; //¶ÓβָÕë¼Ó1
+ return OK;
+}
+
+int QueueEmpty(SqQueue &Q) {
+ if (Q.front == Q.rear) //¶ӿÕ
+ return OK;
+ else
+ return ERROR;
+}
+
+int DeQueue(SqQueue &Q, Person &e)//ɾ³ýQµĶÓͷԪËأ¬ÓÃe·µ»ØÆäֵ
+{
+ if (Q.front == Q.rear)
+ return ERROR; //¶ӿÕ
+ e = Q.base[Q.front]; //±£´æ¶ÓͷԪËØ
+ Q.front = (Q.front + 1) % MAXQSIZE; //¶ÓͷָÕë¼Ó1
+ return OK;
+}
+
+Person GetHead(SqQueue Q) {//·µ»ØQµĶÓͷԪËأ¬²»ÐĶÓͷָÕë
+ if (Q.front != Q.rear) //¶ÓÁзǿÕ
+ return Q.base[Q.front]; //·µ»ضÓͷԪËصÄֵ£¬¶ÓͷָÕ벻±ä
+}
+
+//Ë㷨3.23¡¡Îè°éÎÊÌâ
+void DancePartner(Person dancer[], int num) {//½ṹÊý×édancerÖдæ·ÅÌøÎèµÄÄÐŮ£¬numÊÇÌøÎèµÄÈËÊý¡£
+ InitQueue(Mdancers); //ÄÐʿ¶ÓÁгõʼ»¯
+ InitQueue(Fdancers); //Ůʿ¶ÓÁгõʼ»¯
+ Person p;
+ for (int i = 0; i < num; i++) //ÒÀ´ν«ÌøÎèÕ߸ù¾ÝÆäÐԱðÈë¶Ó
+ {
+ p = dancer[i];
+ if (p.sex == 'F')
+ EnQueue(Fdancers, p); //²åÈëٶÓ
+ else
+ EnQueue(Mdancers, p); //²åÈëÄжÓ
+ }
+ cout << "The dancing partners are:" << endl;
+ while (!QueueEmpty(Fdancers) && !QueueEmpty(Mdancers)) {//ÒÀ´ÎÊä³öÄÐŮÎè°éµÄÐÕÃû
+ DeQueue(Fdancers, p); //Ůʿ³ö¶Ó
+ cout << p.name << " "; //Êä³ö³ö¶ÓŮʿÐÕÃû
+ DeQueue(Mdancers, p); //ÄÐʿ³ö¶Ó
+ cout << p.name << endl; //Êä³ö³ö¶ÓÄÐʿÐÕÃû
+ }
+ if (!QueueEmpty(Fdancers)) { //Ůʿ¶ÓÁзǿգ¬Êä³ö¶ÓͷŮʿµÄÐÕÃû
+ p = GetHead(Fdancers); //ȡŮʿ¶Óͷ
+ cout << "The first man to get a partner is: " << endl;
+ cout << p.name << endl;
+ } else if (!QueueEmpty(Mdancers)) { //ÄÐʿ¶ÓÁзǿգ¬Êä³ö¶ÓͷÄÐʿµÄÐÕÃû
+ p = GetHead(Mdancers); //ȡÄÐʿ¶Óͷ
+ cout << "The first woman to get a partner is: " << p.name << endl;
+ }
+}
\ No newline at end of file
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.suo
new file mode 100644
index 0000000..4741da0
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/3.23.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/DancePartner.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/DancePartner.txt
new file mode 100644
index 0000000..b57d95b
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.23/DancePartner.txt
@@ -0,0 +1,18 @@
+鄢雪棱 F
+高欣雅 F
+张甜源 F
+许丹丹 F
+冉小溪 F
+周如意 F
+胡思琪 F
+蒋雅琪 F
+金城武 M
+吴彦祖 M
+王思聪 M
+古天乐 M
+周杰伦 M
+李敏浩 M
+李钟硕 M
+马小云 M
+张智霖 M
+王力宏 M
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/3.5-3.8.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/3.5-3.8.cpp
new file mode 100644
index 0000000..0419911
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/3.5-3.8.cpp
@@ -0,0 +1,115 @@
+/***链栈的实现***/
+#include
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status;
+typedef char SElemType;
+
+typedef struct StackNode {
+ SElemType data;
+ struct StackNode *next;
+} StackNode, *LinkStack;
+
+//算法3.5 链栈的初始化
+Status InitStack(LinkStack &S) { // 构造一个空栈 S,栈顶指针置空
+ S = NULL;
+ return OK;
+}
+
+//算法3.6 链栈的入栈
+Status Push(LinkStack &S, SElemType e) {//在栈顶插入元素e
+ LinkStack p;
+ p = new StackNode; //生成新结点
+ p->data = e; //将新结点数据域置为e
+ p->next = S; //将新结点插入栈顶
+ S = p; //修改栈顶指针为p
+ return OK;
+}
+
+//算法3.7 链栈的出栈
+Status Pop(LinkStack &S, SElemType &e) {//删除S的栈顶元素,用e返回其值
+ LinkStack p;
+ if (S == NULL)
+ return ERROR; //栈空
+ e = S->data; //将栈顶元素赋给e
+ p = S; //用p临时保存栈顶元素空间,以备释放
+ S = S->next; //修改栈顶指针
+ delete p; //释放原栈顶元素的空间
+ return OK;
+}
+//算法3.8 取链栈的栈顶元素
+SElemType GetTop(LinkStack S) {//返回S的栈顶元素,不修改栈顶指针
+ if (S != NULL) //栈非空
+ return S->data; //返回栈顶元素的值,栈顶指针不变
+}
+
+int main() {
+ LinkStack s;
+ int choose, flag = 0;
+ SElemType j, t;
+ cout << "1.初始化\n";
+ cout << "2.入栈\n";
+ cout << "3.读栈顶元素\n";
+ cout << "4.出栈\n";
+ cout << "0.退出\n\n";
+
+ choose = -1;
+ while (choose != 0) {
+ cout << "请选择:";
+ cin >> choose;
+ switch (choose) {
+ case 1://算法3.5 链栈的初始化
+ if (InitStack(s)) {
+ flag = 1;
+ cout << "成功对栈进行初始化\n\n";
+ } else
+ cout << "初始化栈失败\n\n";
+ break;
+ case 2: {//算法3.6 链栈的入栈
+ fstream file;
+ file.open("SqStack.txt");
+ if (!file) {
+ cout << "错误!未找到文件!\n\n" << endl;
+ exit(ERROR);
+ }
+ if (flag) {
+ flag = 1;
+ cout << "进栈元素依次为:\n";
+ while (!file.eof()) {
+ file >> j;
+ if (file.fail())
+ break;
+ else {
+ Push(s, j);
+ cout << j << " ";
+ }
+ }
+ cout << endl << endl;
+ } else
+ cout << "栈未建立,请重新选择\n\n";
+ file.close();
+ }
+ break;
+ case 3://算法3.8 取链栈的栈顶元素
+ if (flag != -1 && flag != 0)
+ cout << "栈顶元素为:\n" << GetTop(s) << endl << endl;
+ else
+ cout << "栈中无元素,请重新选择\n" << endl;
+ break;
+ case 4://算法3.7 链栈的出栈
+ if (flag) {
+ cout << "依次弹出的栈顶元素为:\n";
+ while (Pop(s, t))
+ cout << t << " ";
+ cout << endl << endl;
+ } else
+ cout << "栈未建立,请重新选择\n\n";
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/3.5-3.8.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/3.5-3.8.suo
new file mode 100644
index 0000000..920ffab
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/3.5-3.8.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/SqStack.txt b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/SqStack.txt
new file mode 100644
index 0000000..8422d40
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.5-3.8/SqStack.txt
@@ -0,0 +1,4 @@
+A
+B
+C
+D
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.9/3.9.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.9/3.9.cpp
new file mode 100644
index 0000000..c043b7b
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.9/3.9.cpp
@@ -0,0 +1,53 @@
+/***依次输出链表中的各个结点***/
+
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status;
+typedef char ElemType;
+typedef struct LNode {
+ ElemType data; //结点的数据域
+ LNode *next; //结点的指针域
+} LNode, *LinkList;//头指针
+
+//后插法创建链表算法
+void CreateList_L(LinkList &L, int n) {
+ L = new LNode;
+ L->next = NULL;
+ LNode *p, *r;
+ r = L;
+ for (int i = 0; i < n; i++) {
+ p = new LNode;
+ cin >> p->data;
+ p->next = NULL;
+ r->next = p;
+ r = p;
+ }
+}
+
+//算法3.9 遍历输出链表中各个结点的递归算法
+void TraverseList(LinkList p) {
+ if (p == NULL)
+ return; //递归终止
+ else {
+ cout << p->data << endl; //输出当前结点的数据域
+ TraverseList(p->next); //p指向后继指点继续递归
+ }
+}
+
+int main() {
+ int n;
+ LinkList L;
+ //InitList(L);
+ cout << "请输入元素个数:" << endl;
+ cin >> n;
+ cout << "请输入链表元素(以空格隔开,按回车结束):" << endl;
+ CreateList_L(L, n); //后插法创建链表算法
+ cout << "链表中的元素依次为:" << endl;
+ TraverseList(L->next);
+ cout << endl;
+ return 0;
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.9/3.9.suo b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.9/3.9.suo
new file mode 100644
index 0000000..e872860
Binary files /dev/null and b/DSBookCode/数据结构第2版-书中算法(C语言版)/第3章/3.9/3.9.suo differ
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第4章/4.1.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第4章/4.1.cpp
new file mode 100644
index 0000000..7684c8b
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第4章/4.1.cpp
@@ -0,0 +1,60 @@
+/***字符串匹配算法***/
+#include
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status;
+#define MAXSTRLEN 255 //用户可在255以内定义最长串长
+typedef char SString[MAXSTRLEN+1]; //0号单元存放串的长度
+
+Status StrAssign(SString T, char *chars) { //生成一个其值等于chars的串T
+ int i;
+ if (strlen(chars) > MAXSTRLEN)
+ return ERROR;
+ else {
+ T[0] = strlen(chars);
+ for (i = 1; i <= T[0]; i++)
+ T[i] = *(chars + i - 1);
+ return OK;
+ }
+}
+
+//算法4.1 BF算法
+int Index(SString S, SString T, int pos)
+{
+ //返回模式T在主串S中第pos个字符之后第s一次出现的位置。若不存在,则返回值为0
+ //其中,T非空,1≤pos≤StrLength(S)
+ int i = pos;
+ int j = 1;
+ while(i <= S[0]&&j <= T[0])
+ {
+ if(S[i]==T[j])
+ {
+ ++i;
+ ++j;
+ } //继续比较后继字符
+ else
+ {
+ i=i-j+2;
+ j=1;
+ } //指针后退重新开始匹配
+ }
+ if (j > T[0])
+ return i - T[0];
+ else
+ return 0;
+ return 0;
+}//Index
+
+int main()
+{
+ SString S;
+ StrAssign(S,"bbaaabbaba") ;
+ SString T;
+ StrAssign(T,"abb") ;
+ cout<<"主串和子串在第"<
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status;
+#define MAXSTRLEN 255 //用户可在255以内定义最长串长
+typedef char SString[MAXSTRLEN+1]; //0号单元存放串的长度
+
+Status StrAssign(SString T, char *chars) { //生成一个其值等于chars的串T
+ int i;
+ if (strlen(chars) > MAXSTRLEN)
+ return ERROR;
+ else {
+ T[0] = strlen(chars);
+ for (i = 1; i <= T[0]; i++)
+ T[i] = *(chars + i - 1);
+ return OK;
+ }
+}
+//算法4.3 计算next函数值
+void get_next(SString T, int next[])
+{ //求模式串T的next函数值并存入数组next
+ int i = 1, j = 0;
+ next[1] = 0;
+ while (i < T[0])
+ if (j == 0 || T[i] == T[j])
+ {
+ ++i;
+ ++j;
+ next[i] = j;
+ }
+ else
+ j = next[j];
+}//get_next
+
+//算法4.2 KMP算法
+int Index_KMP(SString S, SString T, int pos, int next[])
+{ // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法
+ //其中,T非空,1≤pos≤StrLength(S)
+ int i = pos, j = 1;
+ while (i <= S[0] && j <= T[0])
+ if (j == 0 || S[i] == T[j]) // 继续比较后继字
+ {
+ ++i;
+ ++j;
+ }
+ else
+ j = next[j]; // 模式串向右移动
+ if (j > T[0]) // 匹配成功
+ return i - T[0];
+ else
+ return 0;
+}//Index_KMP
+
+int main()
+{
+ SString S;
+ StrAssign(S,"aaabbaba") ;
+ SString T;
+ StrAssign(T,"abb") ;
+ int *p = new int[T[0]+1]; // 生成T的next数组
+ get_next(T,p);
+ cout<<"主串和子串在第"<
+#include
+using namespace std;
+
+#define OK 1
+#define ERROR 0
+#define OVERFLOW -2
+typedef int Status;
+#define MAXSTRLEN 255 //用户可在255以内定义最长串长
+typedef char SString[MAXSTRLEN+1]; //0号单元存放串的长度
+
+Status StrAssign(SString T, char *chars) { //生成一个其值等于chars的串T
+ int i;
+ if (strlen(chars) > MAXSTRLEN)
+ return ERROR;
+ else {
+ T[0] = strlen(chars);
+ for (i = 1; i <= T[0]; i++)
+ T[i] = *(chars + i - 1);
+ return OK;
+ }
+}
+
+//算法4.4 计算next函数修正值
+void get_nextval(SString T, int nextval[])
+{ // 求模式串T的next函数修正值并存入数组nextval
+ int i = 1, j = 0;
+ nextval[1] = 0;
+ while (i < T[0])
+ if (j == 0 || T[i] == T[j])
+ {
+ ++i;
+ ++j;
+ if (T[i] != T[j])
+ nextval[i] = j;
+ else
+ nextval[i] = nextval[j];
+ } else
+ j = nextval[j];
+}//get_nextval
+
+//算法4.2 KMP算法
+int Index_KMP(SString S, SString T, int pos, int next[])
+{ // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法
+ //其中,T非空,1≤pos≤StrLength(S)
+ int i = pos, j = 1;
+ while (i <= S[0] && j <= T[0])
+ if (j == 0 || S[i] == T[j]) // 继续比较后继字
+ {
+ ++i;
+ ++j;
+ }
+ else
+ j = next[j]; // 模式串向右移动
+ if (j > T[0]) // 匹配成功
+ return i - T[0];
+ else
+ return 0;
+}//Index_KMP
+
+int main()
+{
+ SString S;
+ StrAssign(S,"babaaaabbaba") ;
+ SString T;
+ StrAssign(T,"abb") ;
+ int *p = new int[T[0]+1]; // 生成T的next数组
+ get_nextval(T,p);
+ cout<<"主串和子串在第"<
+#include
+#include
+using namespace std;
+
+typedef struct{
+ char ch[600]; //若是非空串,则按串长分配存储区,否则ch为NULL
+ int len; //串长度
+}HString;
+
+
+int Index_BF(HString S,HString T,int pos)
+{//返回模式T在主串S中第pos个字符开始第一次出现的位置。若不存在,则返回值为0
+ //其中,T非空,1≤pos≤StrLength(S)
+ int i,j;
+ i=pos; j=1;
+ while(i<=S.len&&j<=T.len)
+ {
+ if(S.ch[i]==T.ch[j]){++i;++j;} //继续比较后继字符
+ else{i=i-j+2;j=1;} //指针后退重新开始匹配
+ }
+ if(j>T.len) return i-T.len;
+ else return 0;
+}
+
+void Virus_detection()
+{
+ int num,m,flag,i,j; char Vir[600];
+ HString Virus,Person,temp;
+ ifstream inFile("病毒感染检测输入数据.txt");
+ ofstream outFile("病毒感染检测输出结果.txt");
+ inFile>>num;//读取待检测的任务数
+ while(num--) //依次检测每对病毒DNA和人的DNA是否匹配
+ {
+ inFile>>Virus.ch+1;//读取病毒DNA序列
+ inFile>>Person.ch+1;//读取人的DNA序列
+ strcpy(Vir,Virus.ch);
+ Virus.len=strlen(Virus.ch)-1;
+ Person.len=strlen(Person.ch)-1;
+ flag=0;//用来标识是否匹配,初始为0,匹配后为非0
+ m=Virus.len;
+ for(i=m+1,j=1;j<=m;j++) Virus.ch[i++]=Virus.ch[j];
+ //因病毒为环状,故将病毒的长度扩大2倍
+ //即可线性取到所有长度为Virus.len的字符串
+ Virus.ch[2*m+1]='\0'; //添加结束符号
+ for(i=0;i
+using namespace std;
+typedef struct BiNode{ //二叉链表定义
+ char data;
+ struct BiNode *lchild,*rchild;
+}BiTNode,*BiTree;
+
+//用算法5.3 先序遍历的顺序建立二叉链表
+void CreateBiTree(BiTree &T){
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else{
+ T=new BiTNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+void InOrderTraverse(BiTree T){
+ //中序遍历二叉树T的递归算法
+ if(T){
+ InOrderTraverse(T->lchild);
+ cout << T->data;
+ InOrderTraverse(T->rchild);
+ }
+}
+
+void main(){
+ BiTree tree;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ cout<<"中序遍历的结果为:\n";
+ InOrderTraverse(tree);
+ cout<
+using namespace std;
+typedef struct
+{
+ int weight;
+ int parent,lchild,rchild;
+}HTNode,*HuffmanTree;
+
+void Select(HuffmanTree HT,int len,int &s1,int &s2)
+{
+ int i,min1=0x3f3f3f3f,min2=0x3f3f3f3f;//先赋予最大值
+ for(i=1;i<=len;i++)
+ {
+ if(HT[i].weight>HT[i].weight;
+ /*――――――――――初始化工作结束,下面开始创建赫夫曼树――――――――――*/
+ for(i=n+1;i<=m;++i)
+ { //通过n-1次的选择、删除、合并来创建赫夫曼树
+ Select(HT,i-1,s1,s2);
+ //在HT[k](1≤k≤i-1)中选择两个其双亲域为0且权值最小的结点,
+ // 并返回它们在HT中的序号s1和s2
+ HT[s1].parent=i;
+ HT[s2].parent=i;
+ //得到新结点i,从森林中删除s1,s2,将s1和s2的双亲域由0改为i
+ HT[i].lchild=s1;
+ HT[i].rchild=s2 ; //s1,s2分别作为i的左右孩子
+ HT[i].weight=HT[s1].weight+HT[s2].weight; //i 的权值为左右孩子权值之和
+ } //for
+ } // CreatHuffmanTree
+
+void main()
+{
+ HuffmanTree HT;
+ int n;
+ cout<<"请输入叶子结点的个数:\n";
+ cin>>n;
+ CreatHuffmanTree(HT,n);
+ cout<<"哈夫曼树建立完毕!\n";
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.11.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.11.cpp
new file mode 100644
index 0000000..12aad55
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.11.cpp
@@ -0,0 +1,105 @@
+//算法5.11 根据赫夫曼树求赫夫曼编码
+#include
+using namespace std;
+typedef struct
+{
+ int weight;
+ int parent,lchild,rchild;
+}HTNode,*HuffmanTree;
+typedef char **HuffmanCode;
+
+void Select(HuffmanTree HT,int len,int &s1,int &s2)
+{
+ int i,min1=0x3f3f3f3f,min2=0x3f3f3f3f;//先赋予最大值
+ for(i=1;i<=len;i++)
+ {
+ if(HT[i].weight>HT[i].weight;
+ /*――――――――――初始化工作结束,下面开始创建赫夫曼树――――――――――*/
+ for(i=n+1;i<=m;++i)
+ { //通过n-1次的选择、删除、合并来创建赫夫曼树
+ Select(HT,i-1,s1,s2);
+ //在HT[k](1≤k≤i-1)中选择两个其双亲域为0且权值最小的结点,
+ // 并返回它们在HT中的序号s1和s2
+ HT[s1].parent=i;
+ HT[s2].parent=i;
+ //得到新结点i,从森林中删除s1,s2,将s1和s2的双亲域由0改为i
+ HT[i].lchild=s1;
+ HT[i].rchild=s2 ; //s1,s2分别作为i的左右孩子
+ HT[i].weight=HT[s1].weight+HT[s2].weight; //i 的权值为左右孩子权值之和
+ } //for
+}
+ // CreatHuffmanTree
+void CreatHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int n)
+{
+ //从叶子到根逆向求每个字符的赫夫曼编码,存储在编码表HC中
+ int i,start,c,f;
+ HC=new char*[n+1]; //分配n个字符编码的头指针矢量
+ char *cd=new char[n]; //分配临时存放编码的动态数组空间
+ cd[n-1]='\0'; //编码结束符
+ for(i=1;i<=n;++i)
+ { //逐个字符求赫夫曼编码
+ start=n-1; //start开始时指向最后,即编码结束符位置
+ c=i;
+ f=HT[i].parent; //f指向结点c的双亲结点
+ while(f!=0)
+ { //从叶子结点开始向上回溯,直到根结点
+ --start; //回溯一次start向前指一个位置
+ if(HT[f].lchild==c)
+ cd[start]='0'; //结点c是f的左孩子,则生成代码0
+ else
+ cd[start]='1'; //结点c是f的右孩子,则生成代码1
+ c=f;
+ f=HT[f].parent; //继续向上回溯
+ } //求出第i个字符的编码
+ HC[i]=new char[n-start]; // 为第i 个字符编码分配空间
+ strcpy(HC[i], &cd[start]); //将求得的编码从临时空间cd复制到HC的当前行中
+ }
+ delete cd; //释放临时空间
+} // CreatHuffanCode
+void show(HuffmanTree HT,HuffmanCode HC)
+{
+ for(int i=1;i<=sizeof(HC)+1;i++)
+ cout<>n; //输入赫夫曼树的叶子结点个数
+ CreatHuffmanTree(HT,n);
+ CreatHuffmanCode(HT,HC,n);
+ show(HT,HC);
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.12-5.13.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.12-5.13.cpp
new file mode 100644
index 0000000..9b087b9
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.12-5.13.cpp
@@ -0,0 +1,201 @@
+#include
+#include
+#define ERROR -1
+#define MAXSIZE 100
+using namespace std;
+
+typedef struct BiTNode{ //二叉树的二叉链表存储表示
+ char data;
+ struct BiTNode *lchild,*rchild;
+}BiTNode,*BiTree;
+
+typedef struct{ // 字符栈定义
+ char *base;
+ char *top;
+ int stacksize;
+}SqStack;
+
+typedef struct{ //树栈定义
+ BiTree *base;
+ BiTree *top;
+ int stacksize;
+}BiTreeStack;
+
+
+void InitStack(SqStack &S){ //字符栈初始化
+ S.base=new char[MAXSIZE];
+ if(!S.base) exit(-1);
+ S.top=S.base;
+ S.stacksize=MAXSIZE;
+}
+
+
+void InitBiTreeStack(BiTreeStack &S){ //树栈初始化
+ S.base=new BiTree[MAXSIZE];
+ if(!S.base) exit(-1);
+ S.top=S.base;
+ S.stacksize=MAXSIZE;
+}
+
+
+void Push(SqStack &S,char e) //字符栈入栈
+{
+ if(S.top-S.base==S.stacksize) return ;
+ *S.top=e;
+ S.top++;
+
+}
+void PushBiTree(BiTreeStack &S,BiTree e) //树栈入栈
+{
+ if(S.top-S.base==S.stacksize) return ;
+ *S.top=e;
+ S.top++;
+
+}
+void Pop(SqStack &S,char &e) //字符栈出栈
+{
+ if(S.top==S.base) return;
+ e=*--S.top;
+
+}
+
+void PopBiTree(BiTreeStack &S,BiTree &e) //树栈出栈
+{
+ if(S.top==S.base) return;
+ e=*--S.top;
+
+}
+
+
+char GetTop(SqStack &S) //字符栈得到栈顶函数
+{
+ if(S.top==S.base) exit(1);
+ return *(S.top-1);
+}
+
+char Precede(char t1,char t2)
+ { /* 根据教科书表3.1,判断两符号的优先关系 */
+ char f;
+ switch(t2)
+ {
+ case '+':if(t1=='('||t1=='#')
+ f='<';
+ else
+ f='>';
+ break;
+ case '-':if(t1=='('||t1=='#')
+ f='<';
+ else
+ f='>';
+ break;
+ case '*':if(t1=='*'||t1=='/'||t1==')')
+ f='>';
+ else
+ f='<';
+ break;
+ case '/':if(t1=='*'||t1=='/'||t1==')')
+ f='>';
+ else
+ f='<';
+ break;
+ case '(':if(t1!=')')
+ f='<';
+ break;
+ case')':if(t1=='(')
+ f='=';
+ else
+ f='>';
+ break;
+ case'#':if(t1=='#')
+ f='=';
+ else
+ f='>';
+ }
+
+ return f;
+ }
+int In(char c)
+ { /* 判断c是否为运算符 */
+ switch(c)
+ {
+ case '+':
+ case'-':
+ case'*':
+ case '/':
+ case'#':
+ case '(':
+ case')':return 1;break;
+ default:return 0;
+ }
+ }
+int GetValue(char theta,int a,int b) //进行运算的函数
+ {
+ int c;
+ switch(theta)
+ {
+ case '+':c=a+b;break;
+ case'-':c=a-b;break;
+ case'*':c=a*b;break;
+ case'/':c=a/b;break;
+ default:
+ break;
+ }
+ return c;
+ }
+
+void CreateExpTree(BiTree &T,BiTree a,BiTree b,char ch){ //简单二叉树的创建
+ T=new BiTNode;
+ T->data=ch;
+ T->lchild=a;
+ T->rchild=b;
+}
+
+
+ void InitExpTree(BiTree &T) //算法5.12 表达式树的创建
+ {
+ SqStack OPTR;
+ BiTreeStack EXPT;
+ char ch,theta,x;
+ BiTree a,b;
+ InitStack(OPTR);Push(OPTR,'#');
+ InitBiTreeStack(EXPT);cin>>ch;
+ while(ch!='#'||GetTop(OPTR)!='#'){
+ if(!In(ch)){CreateExpTree(T,NULL,NULL,ch);PushBiTree(EXPT,T);cin>>ch;}//*q
+ else
+ switch(Precede(GetTop(OPTR),ch)){
+ case'<':
+ Push(OPTR,ch);cin>>ch;
+ break;
+ case'>':
+ Pop(OPTR,theta);
+ PopBiTree(EXPT,b);PopBiTree(EXPT,a);
+ CreateExpTree(T,a,b,theta); //*
+ PushBiTree(EXPT,T);
+ break;
+ case'=':
+ Pop(OPTR,x);cin>>ch;
+ break;
+ }
+ }
+ }
+
+ int EvaluateExTree(BiTree T){ //算法5.13 表达式树的求值
+ int lvalue,rvalue;
+ lvalue=rvalue=0;
+ if(T->lchild==NULL&&T->rchild==NULL)
+ return T->data-'0';
+ else{
+ lvalue=EvaluateExTree(T->lchild);
+ rvalue=EvaluateExTree(T->rchild);
+ return GetValue(T->data,lvalue,rvalue); //运算函数
+ }
+ }
+
+ void main()
+ {
+ cout<<"请输入算术表达式,并以#结束,中间计算过程要是个位数."<
+using namespace std;
+
+//二叉树的二叉链表存储表示
+typedef struct BiNode
+{
+ char data; //结点数据域
+ struct BiNode *lchild,*rchild; //左右孩子指针
+}BiTNode,*BiTree;
+
+//链栈的定义
+typedef struct StackNode
+{
+ BiTNode data;
+ struct StackNode *next;
+}StackNode,*LinkStack;
+
+//用算法5.3 先序遍历的顺序建立二叉链表
+void CreateBiTree(BiTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else{
+ T=new BiTNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+void InitStack(LinkStack &S)
+{
+ //构造一个空栈S,栈顶指针置空
+ S=NULL;
+}
+
+bool StackEmpty(LinkStack S)
+{
+ if(!S)
+ return true;
+ return false;
+}
+
+void Push(LinkStack &S,BiTree e)
+{
+ //在栈顶插入元素*e
+ StackNode *p=new StackNode;
+ p->data=*e;
+ p->next=S;
+ S=p;
+}
+
+void Pop(LinkStack &S,BiTree e)
+{
+ if(S!=NULL)//原书上写的是if(S==NULL)return ERROR;
+ {
+ *e=S->data;
+ StackNode *p=S;
+ S=S->next;
+ delete p;
+ }
+}
+
+void InOrderTraverse1(BiTree T)
+{
+ // 中序遍历二叉树T的非递归算法
+ LinkStack S; BiTree p;
+ BiTree q=new BiTNode;
+ InitStack(S); p=T;
+ while(p||!StackEmpty(S))
+ {
+ if(p)
+ {
+ Push(S,p); //p非空根指针进栈,遍历左子树
+ p=p->lchild;
+ }
+ else
+ {
+ Pop(S,q); //p为空根指针退栈,访问根结点,遍历右子树
+ cout<data;
+ p=q->rchild;
+ }
+ } // while
+} // InOrderTraverse
+
+void main()
+{
+ BiTree tree;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ cout<<"中序遍历的结果为:\n";
+ InOrderTraverse1(tree);
+ cout<
+using namespace std;
+
+//二叉树的二叉链表存储表示
+typedef struct BiNode
+{
+ char data; //结点数据域
+ struct BiNode *lchild,*rchild; //左右孩子指针
+}BiTNode,*BiTree;
+
+void CreateBiTree(BiTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else{
+ T=new BiTNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+//用算法5.1 中序遍历的递归算法
+void InOrderTraverse(BiTree T)
+{
+ //中序遍历二叉树T的递归算法
+ if(T){
+ InOrderTraverse(T->lchild);
+ cout << T->data;
+ InOrderTraverse(T->rchild);
+ }
+}
+void main()
+{
+ BiTree tree;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ cout<<"所建立的二叉链表中序序列:\n";
+ InOrderTraverse(tree);
+ cout<
+using namespace std;
+
+//二叉树的二叉链表存储表示
+typedef struct BiNode
+{
+ char data; //结点数据域
+ struct BiNode *lchild,*rchild; //左右孩子指针
+}BiTNode,*BiTree;
+
+//用算法5.3建立二叉链表
+void CreateBiTree(BiTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else{
+ T=new BiTNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+void Copy(BiTree T, BiTree &NewT)
+{
+ if(T==NULL )
+ { //如果是空树,递归结束
+ NewT=NULL;
+ return;
+ }
+ else
+ {
+ NewT=new BiTNode;
+ NewT->data= T->data; //复制根结点
+ Copy(T->lchild, NewT->lchild); //递归复制左子树
+ Copy(T->rchild, NewT->rchild); //递归复制右子树
+ } //else
+} //CopyBiTree
+
+//用算法5.1 中序遍历的递归算法
+void InOrderTraverse(BiTree T)
+{
+ //中序遍历二叉树T的递归算法
+ if(T){
+ InOrderTraverse(T->lchild);
+ cout << T->data;
+ InOrderTraverse(T->rchild);
+ }
+}
+void main()
+{
+ BiTree tree,new_tree;
+ cout<<"请输入建立二叉树的序列:\n";
+ CreateBiTree(tree);
+ Copy(tree,new_tree);
+ cout<<"复制得到的新树的中序序列:\n";
+ InOrderTraverse(new_tree);
+ cout<
+using namespace std;
+
+//二叉树的二叉链表存储表示
+typedef struct BiNode
+{
+ char data; //结点数据域
+ struct BiNode *lchild,*rchild; //左右孩子指针
+}BiTNode,*BiTree;
+
+//用算法5.3建立二叉链表
+void CreateBiTree(BiTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else{
+ T=new BiTNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+int Depth(BiTree T)
+{
+ int m,n;
+ if(T == NULL ) return 0; //如果是空树,深度为0,递归结束
+ else
+ {
+ m=Depth(T->lchild); //递归计算左子树的深度记为m
+ n=Depth(T->rchild); //递归计算右子树的深度记为n
+ if(m>n) return(m+1); //二叉树的深度为m 与n的较大者加1
+ else return (n+1);
+ }
+}
+
+void main()
+{
+ BiTree tree;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ cout<<"数的深度为:"<
+using namespace std;
+
+//二叉树的二叉链表存储表示
+typedef struct BiNode
+{
+ char data; //结点数据域
+ struct BiNode *lchild,*rchild; //左右孩子指针
+}BiTNode,*BiTree;
+
+//用算法5.3建立二叉链表
+void CreateBiTree(BiTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else{
+ T=new BiTNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+int NodeCount(BiTree T)
+{
+ if(T==NULL) return 0; // 如果是空树,则结点个数为0,递归结束
+ else return NodeCount(T->lchild)+ NodeCount(T->rchild) +1;
+ //否则结点个数为左子树的结点个数+右子树的结点个数+1
+}
+
+void main()
+{
+ BiTree tree;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ cout<<"结点个数为:"<
+using namespace std;
+
+//二叉树的二叉线索类型存储表示
+typedef struct BiThrNode
+{
+ char data; //结点数据域
+ struct BiThrNode *lchild,*rchild; //左右孩子指针
+ int LTag,RTag;
+}BiThrNode,*BiThrTree;
+
+//全局变量pre
+BiThrNode *pre=new BiThrNode;
+
+//用算法5.3建立二叉链表
+void CreateBiTree(BiThrTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else
+ {
+ T=new BiThrNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+void InThreading(BiThrTree p)
+{
+ //pre是全局变量,初始化时其右孩子指针为空,便于在树的最左点开始建线索
+ if(p)
+ {
+ InThreading(p->lchild); //左子树递归线索化
+ if(!p->lchild)
+ { //p的左孩子为空
+ p->LTag=1; //给p加上左线索
+ p->lchild=pre; //p的左孩子指针指向pre(前驱)
+ }
+ else
+ p->LTag=0;
+ if(!pre->rchild)
+ { //pre的右孩子为空
+ pre->RTag=1; //给pre加上右线索
+ pre->rchild=p; //pre的右孩子指针指向p(后继)
+ }
+ else
+ pre->RTag=0;
+ pre=p; //保持pre指向p的前驱
+ InThreading(p->rchild); //右子树递归线索化
+ }
+}//InThreading
+
+void main()
+{
+ pre->RTag=1;
+ pre->rchild=NULL;
+ BiThrTree tree;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ InThreading(tree);
+ cout<<"线索化完毕!\n";
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.8.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.8.cpp
new file mode 100644
index 0000000..53df055
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.8.cpp
@@ -0,0 +1,85 @@
+//算法5.8 带头结点的中序线索化
+#include
+using namespace std;
+
+//二叉树的二叉线索类型存储表示
+typedef struct BiThrNode
+{
+ char data; //结点数据域
+ struct BiThrNode *lchild,*rchild; //左右孩子指针
+ int LTag,RTag;
+}BiThrNode,*BiThrTree;
+
+//全局变量pre
+BiThrNode *pre=new BiThrNode;
+
+//用算法5.3建立二叉链表
+void CreateBiTree(BiThrTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else
+ {
+ T=new BiThrNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+//用算法5.7 以结点P为根的子树中序线索化
+void InThreading(BiThrTree p)
+{
+ //pre是全局变量,初始化时其右孩子指针为空,便于在树的最左点开始建线索
+ if(p)
+ {
+ InThreading(p->lchild); //左子树递归线索化
+ if(!p->lchild)
+ { //p的左孩子为空
+ p->LTag=1; //给p加上左线索
+ p->lchild=pre; //p的左孩子指针指向pre(前驱)
+ }
+ else
+ p->LTag=0;
+ if(!pre->rchild)
+ { //pre的右孩子为空
+ pre->RTag=1; //给pre加上右线索
+ pre->rchild=p; //pre的右孩子指针指向p(后继)
+ }
+ else
+ pre->RTag=0;
+ pre=p; //保持pre指向p的前驱
+ InThreading(p->rchild); //右子树递归线索化
+ }
+}//InThreading
+
+void InOrderThreading (BiThrTree &Thrt,BiThrTree T)
+{
+ //中序遍历二叉树T,并将其中序线索化,Thrt指向头结点
+ Thrt=new BiThrNode; //建头结点
+ Thrt->LTag=0; //头结点有左孩子,若树非空,则其左孩子为树根
+ Thrt->RTag=1; //头结点的右孩子指针为右线索
+ Thrt->rchild=Thrt; //初始化时右指针指向自己
+ if(!T) Thrt->lchild=Thrt; //若树为空,则左指针也指向自己
+ else
+ {
+ Thrt->lchild=T; pre=Thrt; //头结点的左孩子指向根,pre初值指向头结点
+ InThreading(T); //调用算法5.7,对以T为根的二叉树进行中序线索化
+ pre->rchild=Thrt; //算法5.7结束后,pre为最右结点,pre的右线索指向头结点
+ pre->RTag=1;
+ Thrt->rchild=pre; //头结点的右线索指向pre
+ }
+} //InOrderThreading
+
+void main()
+{
+ pre->RTag=1;
+ pre->rchild=NULL;
+ BiThrTree tree,Thrt;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ InOrderThreading(Thrt,tree);
+ cout<<"线索化完毕!\n";
+}
diff --git a/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.9.cpp b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.9.cpp
new file mode 100644
index 0000000..4de9b76
--- /dev/null
+++ b/DSBookCode/数据结构第2版-书中算法(C语言版)/第5章/5.9.cpp
@@ -0,0 +1,108 @@
+//算法5.9 遍历中序线索二叉树
+#include
+using namespace std;
+
+//二叉树的二叉线索类型存储表示
+typedef struct BiThrNode
+{
+ char data; //结点数据域
+ struct BiThrNode *lchild,*rchild; //左右孩子指针
+ int LTag,RTag;
+}BiThrNode,*BiThrTree;
+
+//全局变量pre
+BiThrNode *pre=new BiThrNode;
+
+//用算法5.3建立二叉链表
+void CreateBiTree(BiThrTree &T)
+{
+ //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
+ char ch;
+ cin >> ch;
+ if(ch=='#') T=NULL; //递归结束,建空树
+ else
+ {
+ T=new BiThrNode;
+ T->data=ch; //生成根结点
+ CreateBiTree(T->lchild); //递归创建左子树
+ CreateBiTree(T->rchild); //递归创建右子树
+ } //else
+} //CreateBiTree
+
+//用算法5.7以结点P为根的子树中序线索化
+void InThreading(BiThrTree p)
+{
+ //pre是全局变量,初始化时其右孩子指针为空,便于在树的最左点开始建线索
+ if(p)
+ {
+ InThreading(p->lchild); //左子树递归线索化
+ if(!p->lchild)
+ { //p的左孩子为空
+ p->LTag=1; //给p加上左线索
+ p->lchild=pre; //p的左孩子指针指向pre(前驱)
+ }
+ else
+ p->LTag=0;
+ if(!pre->rchild)
+ { //pre的右孩子为空
+ pre->RTag=1; //给pre加上右线索
+ pre->rchild=p; //pre的右孩子指针指向p(后继)
+ }
+ else
+ pre->RTag=0;
+ pre=p; //保持pre指向p的前驱
+ InThreading(p->rchild); //右子树递归线索化
+ }
+}//InThreading
+
+//用算法5.8带头结点的中序线索化
+void InOrderThreading (BiThrTree &Thrt,BiThrTree T)
+{
+ //中序遍历二叉树T,并将其中序线索化,Thrt指向头结点
+ Thrt=new BiThrNode; //建头结点
+ Thrt->LTag=0; //头结点有左孩子,若树非空,则其左孩子为树根
+ Thrt->RTag=1; //头结点的右孩子指针为右线索
+ Thrt->rchild=Thrt; //初始化时右指针指向自己
+ if(!T) Thrt->lchild=Thrt; //若树为空,则左指针也指向自己
+ else
+ {
+ Thrt->lchild=T; pre=Thrt; //头结点的左孩子指向根,pre初值指向头结点
+ InThreading(T); //调用算法5.7,对以T为根的二叉树进行中序线索化
+ pre->rchild=Thrt; //算法5.7结束后,pre为最右结点,pre的右线索指向头结点
+ pre->RTag=1;
+ Thrt->rchild=pre; //头结点的右线索指向pre
+ }
+} //InOrderThreading
+
+void InOrderTraverse_Thr(BiThrTree T)
+{
+ //T指向头结点,头结点的左链lchild指向根结点,可参见线索化算法5.8。
+ //中序遍历二叉线索树T的非递归算法,对每个数据元素直接输出
+ BiThrTree p;
+ p=T->lchild; //p指向根结点
+ while(p!=T) //空树或遍历结束时,p==T
+ {
+ while(p->LTag==0) //沿左孩子向下
+ p=p->lchild; //访问其左子树为空的结点
+ cout<data;
+ while(p->RTag==1&&p->rchild!=T)
+ {
+ p=p->rchild; //沿右线索访问后继结点
+ cout<data;
+ }
+ p=p->rchild;
+ }
+} //InOrderTraverse_Thr
+
+void main()
+{
+ pre->RTag=1;
+ pre->rchild=NULL;
+ BiThrTree tree,Thrt;
+ cout<<"请输入建立二叉链表的序列:\n";
+ CreateBiTree(tree);
+ InOrderThreading(Thrt,tree);
+ cout<<"中序遍历线索二叉树的结果为:\n";
+ InOrderTraverse_Thr(Thrt);
+ cout<
+using namespace std;
+
+#define MaxInt 32767 //表示极大值,即∞
+#define MVNum 100 //最大顶点数
+#define OK 1
+
+typedef char VerTexType; //假设顶点的数据类型为字符型
+typedef int ArcType; //假设边的权值类型为整型
+
+//- - - - -图的邻接矩阵存储表示- - - - -
+typedef struct{
+ VerTexType vexs[MVNum]; //顶点表
+ ArcType arcs[MVNum][MVNum]; //邻接矩阵
+ int vexnum,arcnum; //图的当前点数和边数
+}AMGraph;
+
+int LocateVex(AMGraph G , VerTexType v){
+ //确定点v在G中的位置
+ for(int i = 0; i < G.vexnum; ++i)
+ if(G.vexs[i] == v)
+ return i;
+ return -1;
+}//LocateVex
+
+int CreateUDN(AMGraph &G){
+ //采用邻接矩阵表示法,创建无向网G
+ int i , j , k;
+ cout <<"请输入总顶点数,总边数,以空格隔开:";
+ cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
+ cout << endl;
+
+ cout << "输入点的名称,如a" << endl;
+
+ for(i = 0; i < G.vexnum; ++i){
+ cout << "请输入第" << (i+1) << "个点的名称:";
+ cin >> G.vexs[i]; //依次输入点的信息
+ }
+ cout << endl;
+ for(i = 0; i < G.vexnum; ++i) //初始化邻接矩阵,边的权值均置为极大值MaxInt
+ for(j = 0; j < G.vexnum; ++j)
+ G.arcs[i][j] = MaxInt;
+ cout << "输入边依附的顶点及权值,如 a b 5" << endl;
+ for(k = 0; k < G.arcnum;++k){ //构造邻接矩阵
+ VerTexType v1 , v2;
+ ArcType w;
+ cout << "请输入第" << (k + 1) << "条边依附的顶点及权值:";
+ cin >> v1 >> v2 >> w; //输入一条边依附的顶点及权值
+ i = LocateVex(G, v1); j = LocateVex(G, v2); //确定v1和v2在G中的位置,即顶点数组的下标
+ G.arcs[i][j] = w; //边的权值置为w
+ G.arcs[j][i] = G.arcs[i][j]; //置的对称边的权值为w
+ }//for
+ return OK;
+}//CreateUDN
+
+int main(){
+ cout << "************算法6.1 采用邻接矩阵表示法创建无向网**************" << endl << endl;
+ AMGraph G; int i , j;
+ CreateUDN(G);
+
+ cout <
+using namespace std;
+
+#define MaxInt 32767 //表示极大值,即∞
+#define MVNum 100 //最大顶点数
+typedef char VerTexType; //假设顶点的数据类型为字符型
+typedef int ArcType; //假设边的权值类型为整型
+
+int *D=new int[MVNum]; //用于记录最短路的长度
+bool *S=new bool[MVNum]; //标记顶点是否进入S集合
+int *Path=new int[MVNum]; //用于记录最短路顶点的前驱
+
+//------------图的邻接矩阵-----------------
+typedef struct{
+ VerTexType vexs[MVNum]; //顶点表
+ ArcType arcs[MVNum][MVNum]; //邻接矩阵
+ int vexnum,arcnum; //图的当前点数和边数
+}AMGraph;
+
+int LocateVex(AMGraph G , VerTexType v){
+ //确定点v在G中的位置
+ for(int i = 0; i < G.vexnum; ++i)
+ if(G.vexs[i] == v)
+ return i;
+ return -1;
+}//LocateVex
+
+void CreateUDN(AMGraph &G){
+ //采用邻接矩阵表示法,创建无向网G
+ int i , j , k;
+ cout <<"请输入总顶点数,总边数,以空格隔开:";
+ cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
+ cout << endl;
+
+ cout << "输入点的名称:,如a" << endl;
+
+ for(i = 0; i < G.vexnum; ++i){
+ cout << "请输入第" << (i+1) << "个点的名称:";
+ cin >> G.vexs[i]; //依次输入点的信息
+ }
+ cout << endl;
+ for(i = 0; i < G.vexnum; ++i) //初始化邻接矩阵,边的权值均置为极大值MaxInt
+ for(j = 0; j < G.vexnum; ++j)
+ G.arcs[i][j] = MaxInt;
+ cout << "输入边依附的顶点及权值,如a b 7" << endl;
+ for(k = 0; k < G.arcnum;++k){ //构造邻接矩阵
+ VerTexType v1 , v2;
+ ArcType w;
+ cout << "请输入第" << (k + 1) << "条边依附的顶点及权值:";
+ cin >> v1 >> v2 >> w; //输入一条边依附的顶点及权值
+ i = LocateVex(G, v1); j = LocateVex(G, v2); //确定v1和v2在G中的位置,即顶点数组的下标
+ G.arcs[i][j] = w; //边的权值置为w
+ G.arcs[j][i] = G.arcs[i][j]; //置的对称边的权值为w
+ }//for
+}//CreateUDN
+
+void ShortestPath_DIJ(AMGraph G, int v0){
+ //用Dijkstra算法求有向网G的v0顶点到其余顶点的最短路径
+ int v , i , w , min;
+ int n = G.vexnum; //n为G中顶点的个数
+
+ for(v = 0; v < n; ++v){ //n个顶点依次初始化
+ S[v] = false; //S初始为空集
+ D[v] = G.arcs[v0][v]; //将v0到各个终点的最短路径长度初始化为弧上的权值
+ if(D[v] < MaxInt) Path [v] = v0; //如果v0和v之间有弧,则将v的前驱置为v0
+ else Path [v] = -1; //如果v0和v之间无弧,则将v的前驱置为-1
+ }//for
+
+ S[v0]=true; //将v0加入S
+ D[v0]=0; //源点到源点的距离为0
+
+ /*―初始化结束,开始主循环,每次求得v0到某个顶点v的最短路径,将v加到S集―*/
+ for(i = 1;i < n; ++i){ //对其余n-1个顶点,依次进行计算
+ min= MaxInt;
+ for(w = 0; w < n; ++w)
+ if(!S[w] && D[w] < min){ //选择一条当前的最短路径,终点为v
+ v = w;
+ min = D[w];
+ }//if
+ S[v]=true; //将v加入S
+ for(w = 0;w < n; ++w) //更新从v0出发到集合V?S上所有顶点的最短路径长度
+ if(!S[w] && (D[v] + G.arcs[v][w] < D[w])){
+ D[w] = D[v] + G.arcs[v][w]; //更新D[w]
+ Path [w] = v; //更改w的前驱为v
+ }//if
+ }//for
+}//ShortestPath_DIJ
+
+void DisplayPath(AMGraph G , int begin ,int temp ){
+ //显示最短路
+ if(Path[temp] != -1){
+ DisplayPath(G , begin ,Path[temp]);
+ cout << G.vexs[Path[temp]] << "-->";
+ }
+}//DisplayPath
+
+void main()
+{
+ cout << "************算法6.10 迪杰斯特拉算法**************" << endl << endl;
+ AMGraph G;
+ int i , j ,num_start , num_destination;
+ VerTexType start , destination;
+ CreateUDN(G);
+ cout <> start >> destination;
+ num_start = LocateVex(G , start);
+ num_destination = LocateVex(G , destination);
+ ShortestPath_DIJ(G , num_start);
+ cout << endl <<"最短路径为:";
+ DisplayPath(G , num_start , num_destination);
+ cout << G.vexs[num_destination]<
+using namespace std;
+
+#define MaxInt 32767 //表示极大值,即∞
+#define MVNum 100 //最大顶点数
+
+typedef char VerTexType; //假设顶点的数据类型为字符型
+typedef int ArcType; //假设边的权值类型为整型
+
+int Path[MVNum][MVNum]; //最短路径上顶点vj的前一顶点的序号
+int D[MVNum][MVNum]; //记录顶点vi和vj之间的最短路径长度
+
+//------------图的邻接矩阵---------------
+typedef struct{
+ VerTexType vexs[MVNum]; //顶点表
+ ArcType arcs[MVNum][MVNum]; //邻接矩阵
+ int vexnum,arcnum; //图的当前点数和边数
+}AMGraph;
+
+int LocateVex(AMGraph G , VerTexType v){
+ //确定点v在G中的位置
+ for(int i = 0; i < G.vexnum; ++i)
+ if(G.vexs[i] == v)
+ return i;
+ return -1;
+}//LocateVex
+
+void CreateUDN(AMGraph &G){
+ //采用邻接矩阵表示法,创建有向网G
+ int i , j , k;
+ cout <<"请输入总顶点数,总边数,以空格隔开:";
+ cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
+ cout << endl;
+
+ cout << "输入点的名称,如a" << endl;
+
+ for(i = 0; i < G.vexnum; ++i){
+ cout << "请输入第" << (i+1) << "个点的名称:";
+ cin >> G.vexs[i]; //依次输入点的信息
+ }
+ cout << endl;
+ for(i = 0; i < G.vexnum; ++i){ //初始化邻接矩阵,边的权值均置为极大值MaxInt
+ for(j = 0; j < G.vexnum; ++j){
+ if(j != i)
+ G.arcs[i][j] = MaxInt;
+ else
+ G.arcs[i][j] = 0;
+ }//for
+ }//for
+
+ cout << "输入边依附的顶点及权值,如a b 3" << endl;
+ for(k = 0; k < G.arcnum;++k){ //构造邻接矩阵
+ VerTexType v1 , v2;
+ ArcType w;
+ cout << "请输入第" << (k + 1) << "条边依附的顶点及权值:";
+ cin >> v1 >> v2 >> w; //输入一条边依附的顶点及权值
+ i = LocateVex(G, v1); j = LocateVex(G, v2); //确定v1和v2在G中的位置,即顶点数组的下标
+ G.arcs[i][j] = w; //边的权值置为w
+ }//for
+}//CreateUDN
+
+void ShortestPath_Floyed(AMGraph G){
+ //用Floyd算法求有向网G中各对顶点i和j之间的最短路径
+ int i , j , k ;
+ for (i = 0; i < G.vexnum; ++i) //各对结点之间初始已知路径及距离
+ for(j = 0; j < G.vexnum; ++j){
+ D[i][j] = G.arcs[i][j];
+ if(D[i][j] < MaxInt && i != j) Path[i][j]=i; //如果i和j之间有弧,则将j的前驱置为i
+ else Path [i][j] = -1; //如果i和j之间无弧,则将j的前驱置为-1
+ }//for
+ for(k = 0; k < G.vexnum; ++k)
+ for(i = 0; i < G.vexnum; ++i)
+ for(j = 0; j < G.vexnum; ++j)
+ if(D[i][k] + D[k][j] < D[i][j]){ //从i经k到j的一条路径更短
+ D[i][j] = D[i][k]+D[k][j]; //更新D[i][j]
+ Path[i][j] = Path[k][j]; //更改j的前驱为k
+ }//if
+}//ShortestPath_Floyed
+
+void DisplayPath(AMGraph G , int begin ,int temp ){
+ //显示最短路径
+ if(Path[begin][temp] != -1){
+ DisplayPath(G , begin ,Path[begin][temp]);
+ cout << G.vexs[Path[begin][temp]] << "-->";
+ }
+}//DisplayPath
+
+void main(){
+ cout << "************算法6.11 弗洛伊德算法**************" << endl << endl;
+ AMGraph G;
+ char start , destination;
+ int num_start , num_destination;
+
+ CreateUDN(G);
+
+ cout <> start >> destination;
+ num_start = LocateVex(G , start);
+ num_destination = LocateVex(G , destination);
+
+ DisplayPath(G , num_start , num_destination);
+ cout << G.vexs[num_destination] << endl;
+ cout << "最短路径的长度为:" << D[num_start][num_destination] << endl;
+ cout <
+using namespace std;
+
+#define MVNum 100 //最大顶点数
+#define OK 1
+#define ERROR 0
+
+typedef char VerTexType;
+
+//- - - - -图的邻接表存储表示- - - - -
+typedef struct ArcNode{ //边结点
+ int adjvex; //该边所指向的顶点的位置
+ struct ArcNode *nextarc; //指向下一条边的指针
+}ArcNode;
+
+typedef struct VNode{
+ VerTexType data; //顶点信息
+ ArcNode *firstarc; //指向第一条依附该顶点的边的指针
+}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
+
+typedef struct{
+ AdjList vertices; //邻接表
+ AdjList converse_vertices; //逆邻接表
+ int vexnum, arcnum; //图的当前顶点数和边数
+}ALGraph;
+//- - - - - - - - - - - - - - - -
+
+//- - - - -顺序栈的定义- - - - -
+typedef struct{
+ int *base;
+ int *top;
+ int stacksize;
+}spStack;
+//- - - - - - - - - - - - - - - -
+
+int indegree[MVNum]; //数组indegree存放个顶点的入度
+spStack S;
+
+//------------栈的相关操作----------------------
+void InitStack(spStack &S){
+ //初始化栈
+ S.base = new int[MVNum];
+ if(!S.base)
+ exit(1);
+ S.top = S.base;
+ S.stacksize = MVNum;
+}//InitStack
+
+void Push(spStack &S , int i){
+ //进栈
+ if(S.top - S.base == S.stacksize)
+ return;
+ *S.top++ = i;
+}//Push
+
+void Pop(spStack &S , int &i){
+ //出栈
+ if(S.top == S.base)
+ return;
+ i = *--S.top;
+}//Pop
+
+bool StackEmpty(spStack S){
+ //判断栈是否为空
+ if(S.top == S.base)
+ return true;
+ return false;
+}//StackEmpty
+//-------------------------------------------------
+
+int LocateVex(ALGraph G , VerTexType v){
+ //确定点v在G中的位置
+ for(int i = 0; i < G.vexnum; ++i)
+ if(G.vertices[i].data == v)
+ return i;
+ return -1;
+}//LocateVex
+
+int CreateUDG(ALGraph &G){
+ //创建有向图G的邻接表、逆邻接表
+ int i , k;
+
+ cout <<"请输入总顶点数,总边数,以空格隔开:";
+ cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
+ cout << endl;
+
+ cout << "输入点的名称,如a" << endl;
+
+ for(i = 0; i < G.vexnum; ++i){ //输入各点,构造表头结点表
+ cout << "请输入第" << (i+1) << "个点的名称:";
+ cin >> G.vertices[i].data; //输入顶点值
+ G.converse_vertices[i].data = G.vertices[i].data;
+ //初始化表头结点的指针域为NULL
+ G.vertices[i].firstarc=NULL;
+ G.converse_vertices[i].firstarc=NULL;
+ }//for
+ cout << endl;
+ cout << "输入边依附的顶点,如a b" << endl;
+ for(k = 0; k < G.arcnum;++k){ //输入各边,构造邻接表
+ VerTexType v1 , v2;
+ int i , j;
+ cout << "请输入第" << (k + 1) << "条边依附的顶点:";
+ cin >> v1 >> v2; //输入一条边依附的两个顶点
+ i = LocateVex(G, v1); j = LocateVex(G, v2);
+ //确定v1和v2在G中位置,即顶点在G.vertices中的序号
+
+ ArcNode *p1=new ArcNode; //生成一个新的边结点*p1
+ p1->adjvex=j; //邻接点序号为j
+ p1->nextarc = G.vertices[i].firstarc; G.vertices[i].firstarc=p1;
+ //将新结点*p1插入顶点vi的边表头部
+
+ ArcNode *p2=new ArcNode; //生成一个新的边结点*p1
+ p2->adjvex=i; //逆邻接点序号为i
+ p2->nextarc = G.converse_vertices[j].firstarc; G.converse_vertices[j].firstarc=p2;
+ //将新结点*p1插入顶点vi的边表头部
+ }//for
+ return OK;
+}//CreateUDG
+
+void FindInDegree(ALGraph G){
+ //求出各顶点的入度存入数组indegree中
+ int i , count;
+
+ for(i = 0 ; i < G.vexnum ; i++){
+ count = 0;
+ ArcNode *p = G.converse_vertices[i].firstarc;
+ if(p){
+ while(p){
+ p = p->nextarc;
+ count++;
+ }
+ }
+ indegree[i] = count;
+ }
+}//FindInDegree
+
+int TopologicalSort(ALGraph G , int topo[]){
+ //有向图G采用邻接表存储结构
+ //若G无回路,则生成G的一个拓扑序列topo[]并返回OK,否则ERROR
+ int i , m;
+ FindInDegree(G); //求出各顶点的入度存入数组indegree中
+ InitStack(S); //栈S初始化为空
+ for(i = 0; i < G.vexnum; ++i)
+ if(!indegree[i]) Push(S, i); //入度为0者进栈
+ m = 0; //对输出顶点计数,初始为0
+ while(!StackEmpty(S)){ //栈S非空
+ Pop(S, i); //将栈顶顶点vi出栈
+ topo[m]=i; //将vi保存在拓扑序列数组topo中
+ ++m; //对输出顶点计数
+ ArcNode *p = G.vertices[i].firstarc; //p指向vi的第一个邻接点
+ while(p){
+ int k = p->adjvex; //vk为vi的邻接点
+ --indegree[k]; //vi的每个邻接点的入度减1
+ if(indegree[k] ==0) Push(S, k); //若入度减为0,则入栈
+ p = p->nextarc; //p指向顶点vi下一个邻接结点
+ }//while
+ }//while
+
+ if(m < G.vexnum) return ERROR; //该有向图有回路
+ else return OK;
+}//TopologicalSort
+
+int main(){
+ cout << "************算法6.12 拓扑排序**************" << endl << endl;
+ ALGraph G;
+ CreateUDG(G);
+ int *topo = new int [G.vexnum];
+
+ cout << endl;
+ cout << "有向图的邻接表、逆邻接表创建完成!" << endl << endl;
+
+ if(TopologicalSort(G , topo)){
+ cout << "该有向图的拓扑有序序列为:";
+ for(int j = 0 ; j < G.vexnum; j++){
+ if(j != G.vexnum - 1)
+ cout << G.vertices[topo[j]].data << " , ";
+ else
+ cout << G.vertices[topo[j]].data << endl << endl;
+ }//for
+ }
+ else
+ cout << "网中存在环,无法进行拓扑排序!" <
+using namespace std;
+
+#define MVNum 100 //最大顶点数
+#define BDNum MVNum * (MVNum - 1) //最大边数
+#define OK 1
+#define ERROR 0
+
+typedef char VerTexType;
+
+//- - - - -图的邻接表存储表示- - - - -
+typedef struct ArcNode{ //边结点
+ int adjvex; //该边所指向的顶点的位置
+ int weight; //权值
+ struct ArcNode *nextarc; //指向下一条边的指针
+}ArcNode;
+
+typedef struct VNode{
+ VerTexType data; //顶点信息
+ ArcNode *firstarc; //指向第一条依附该顶点的边的指针
+}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
+
+typedef struct{
+ AdjList vertices; //邻接表
+ AdjList converse_vertices; //逆邻接表
+ int vexnum, arcnum; //图的当前顶点数和边数
+}ALGraph;
+//- - - - - - - - - - - - - - - -
+
+//- - - - -顺序栈的定义- - - - -
+typedef struct{
+ int *base;
+ int *top;
+ int stacksize;
+}spStack;
+//- - - - - - - - - - - - - - - -
+
+int indegree[MVNum]; //数组indegree存放个顶点的入度
+int ve[BDNum]; //事件vi的最早发生时间
+int vl[BDNum]; //事件vi的最迟发生时间
+int topo[MVNum]; //记录拓扑序列的顶点序号
+spStack S;
+
+//----------------栈的操作--------------------
+void InitStack(spStack &S){
+ //栈的初始化
+ S.base = new int[MVNum];
+ if(!S.base)
+ exit(1);
+ S.top = S.base;
+ S.stacksize = MVNum;
+}//InitStack
+
+void Push(spStack &S , int i){
+ //入栈
+ if(S.top - S.base == S.stacksize)
+ return;
+ *S.top++ = i;
+}//Push
+
+void Pop(spStack &S , int &i){
+ //出栈
+ if(S.top == S.base)
+ return;
+ i = *--S.top;
+}//Pop
+
+bool StackEmpty(spStack S){
+ //判断栈是否为空
+ if(S.top == S.base)
+ return true;
+ return false;
+}//StackEmpty
+//---------------------------------------
+
+int LocateVex(ALGraph G , VerTexType v){
+ //确定点v在G中的位置
+ for(int i = 0; i < G.vexnum; ++i)
+ if(G.vertices[i].data == v)
+ return i;
+ return -1;
+}//LocateVex
+
+int CreateUDG(ALGraph &G){
+ //创建有向图G的邻接表、逆邻接表
+ int i , k;
+
+ cout <<"请输入总顶点数,总边数,以空格隔开:";
+ cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
+ cout << endl;
+
+ cout << "输入点的名称,如a" << endl;
+
+ for(i = 0; i < G.vexnum; ++i){ //输入各点,构造表头结点表
+ cout << "请输入第" << (i+1) << "个点的名称:";
+ cin >> G.vertices[i].data; //输入顶点值
+ G.converse_vertices[i].data = G.vertices[i].data;
+ //初始化表头结点的指针域为NULL
+ G.vertices[i].firstarc=NULL;
+ G.converse_vertices[i].firstarc=NULL;
+ }//for
+ cout << endl;
+
+ cout << "输入边依附的顶点及其权值,如a b 3" << endl;
+
+ for(k = 0; k < G.arcnum;++k){ //输入各边,构造邻接表
+ VerTexType v1 , v2;
+ int i , j , w;
+ cout << "请输入第" << (k + 1) << "条边依附的顶点及其权值:";
+ cin >> v1 >> v2 >> w; //输入一条边依附的两个顶点
+ i = LocateVex(G, v1); j = LocateVex(G, v2);
+ //确定v1和v2在G中位置,即顶点在G.vertices中的序号
+
+ ArcNode *p1=new ArcNode; //生成一个新的边结点*p1
+ p1->adjvex=j; //邻接点序号为j
+ p1->nextarc = G.vertices[i].firstarc; G.vertices[i].firstarc=p1;
+ p1->weight = w;
+ //将新结点*p1插入顶点vi的边表头部
+
+ ArcNode *p2=new ArcNode; //生成一个新的边结点*p1
+ p2->adjvex=i; //逆邻接点序号为i
+ p2->nextarc = G.converse_vertices[j].firstarc; G.converse_vertices[j].firstarc=p2;
+ p2->weight = w;
+ //将新结点*p1插入顶点vi的边表头部
+ }//for
+ return OK;
+}//CreateUDG
+
+void FindInDegree(ALGraph G){
+ //求出各顶点的入度存入数组indegree中
+ int i , count;
+
+ for(i = 0 ; i < G.vexnum ; i++){
+ count = 0;
+ ArcNode *p = G.converse_vertices[i].firstarc;
+ if(p){
+ while(p){
+ p = p->nextarc;
+ count++;
+ }
+ }//if
+ indegree[i] = count;
+ }//for
+}//FindInDegree
+
+int TopologicalOrder(ALGraph G , int topo[]){
+ //有向图G采用邻接表存储结构
+ //若G无回路,则生成G的一个拓扑序列topo[]并返回OK,否则ERROR
+ int i , m;
+ FindInDegree(G); //求出各顶点的入度存入数组indegree中
+ InitStack(S); //栈S初始化为空
+ for(i = 0; i < G.vexnum; ++i)
+ if(!indegree[i]) Push(S, i); //入度为0者进栈
+ m = 0; //对输出顶点计数,初始为0
+ while(!StackEmpty(S)){ //栈S非空
+ Pop(S, i); //将栈顶顶点vi出栈
+ topo[m]=i; //将vi保存在拓扑序列数组topo中
+ ++m; //对输出顶点计数
+ ArcNode *p = G.vertices[i].firstarc; //p指向vi的第一个邻接点
+ while(p){
+ int k = p->adjvex; //vk为vi的邻接点
+ --indegree[k]; //vi的每个邻接点的入度减1
+ if(indegree[k] ==0) Push(S, k); //若入度减为0,则入栈
+ p = p->nextarc; //p指向顶点vi下一个邻接结点
+ }//while
+ }//while
+
+ if(m < G.vexnum) return ERROR; //该有向图有回路
+ else return OK;
+}//TopologicalOrder
+
+int CriticalPath(ALGraph G){
+ //G为邻接表存储的有向网,输出G的各项关键活动
+ int n , i , k , j , e , l;
+ if (!TopologicalOrder(G, topo)) return ERROR;
+ //调用拓扑排序算法,使拓扑序列保存在topo中,若调用失败,则存在有向环,返回ERROR
+ n = G.vexnum; //n为顶点个数
+ for(i = 0; i < n; i++) //给每个事件的最早发生时间置初值0
+ ve[i] = 0;
+
+
+ /*――――――――――按拓扑次序求每个事件的最早发生时间-――――-―――――*/
+ for(i = 0;i < n; i++){
+ k = topo[i]; //取得拓扑序列中的顶点序号k
+ ArcNode *p = G.vertices[k].firstarc; //p指向k的第一个邻接顶点
+ while(p != NULL){ //依次更新k的所有邻接顶点的最早发生时间
+ j = p->adjvex; //j为邻接顶点的序号
+ if(ve[j] < ve[k] + p->weight) //更新顶点j的最早发生时间ve[j]
+ ve[j] = ve[k] + p->weight;
+ p = p->nextarc; //p指向k的下一个邻接顶点
+ } //while
+ } //for
+
+ for(i=0;i= 0; i--){
+ k = topo[i]; //取得拓扑序列中的顶点序号k
+ ArcNode *p = G.vertices[k].firstarc; //p指向k的第一个邻接顶点
+ while(p != NULL){ //根据k的邻接点,更新k的最迟发生时间
+ j = p->adjvex; //j为邻接顶点的序号
+ if(vl[k] > vl[j] - p->weight) //更新顶点k的最迟发生时间vl[k]
+ vl[k] = vl[j] - p->weight;
+ p = p->nextarc; //p指向k的下一个邻接顶点
+ }//while
+ }//for
+
+ /*――――――――――――判断每一活动是否为关键活动-――――――-―――――*/
+ cout << endl;
+ cout << "关键活动路径为:";
+ for(i = 0;i < n; i++){ //每次循环针对vi为活动开始点的所有活动
+ ArcNode *p = G.vertices[i].firstarc; //p指向i的第一个邻接顶点
+ while(p != NULL) {
+ j = p->adjvex; //j为i的邻接顶点的序号
+ e = ve[i]; //计算活动的最早开始时间
+ l = vl[j] - p->weight; //计算活动的最迟开始时间
+ if(e == l) //若为关键活动,则输出
+ cout << G.vertices[i].data << "-->" << G.vertices[j].data << " ";
+ p = p->nextarc; //p指向i的下一个邻接顶点
+ } //while
+ } //for
+ return OK;
+}//CriticalPath
+
+int main(){
+ cout << "************算法6.13 关键路径算法**************" << endl << endl;
+ ALGraph G;
+ CreateUDG(G);
+ int *topo = new int [G.vexnum];
+
+ cout << endl;
+ cout << "有向图创建完成!" << endl << endl;
+
+ if(!CriticalPath(G))
+ cout << "网中存在环,无法进行拓扑排序!" <
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include