├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── LICENSE_CN ├── README.md └── src ├── 2_Linear_List ├── 2_1_Sequential_List.cpp ├── 2_2_Singly_Linked_List.cpp ├── 2_3_Header_Linked_List.cpp ├── 2_4_Circular_Linked_List.cpp ├── 2_5_Doubly_Linked_List.cpp └── 2_6_Doubly_Circular_Linked_List.cpp ├── 3_Stack_and_Queue ├── 3_1_Sequential_Stack.cpp ├── 3_2_Linked_Stack.cpp ├── 3_3_Circular_Queue.cpp └── 3_4_Linked_Queue.cpp ├── 4_Array ├── 4_1_Three-dimensional_Array.cpp └── 4_2_Sparse_Matrix.cpp └── 5_Tree_and_Binary_Tree └── 5_1_Binary_Tree.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # CLion 35 | .idea 36 | 37 | # CMake 38 | cmake-build-*/ 39 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.19) 2 | project(NJUPT_Data_Structure_Examples) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | file(GLOB APP_SOURCES src/*/*.cpp) 7 | 8 | foreach( sourcefile ${APP_SOURCES} ) 9 | get_filename_component( demo_name ${sourcefile} NAME_WE ) 10 | add_executable( ${demo_name} ${sourcefile} ) 11 | endforeach( sourcefile ${APP_SOURCES} ) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Carlos Pi 2 | 3 | "Anti 996" License Version 1.0 (Draft) 4 | 5 | Permission is hereby granted to any individual or legal entity 6 | obtaining a copy of this licensed work (including the source code, 7 | documentation and/or related items, hereinafter collectively referred 8 | to as the "licensed work"), free of charge, to deal with the licensed 9 | work for any purpose, including without limitation, the rights to use, 10 | reproduce, modify, prepare derivative works of, distribute, publish 11 | and sublicense the licensed work, subject to the following conditions: 12 | 13 | 1. The individual or the legal entity must conspicuously display, 14 | without modification, this License and the notice on each redistributed 15 | or derivative copy of the Licensed Work. 16 | 17 | 2. The individual or the legal entity must strictly comply with all 18 | applicable laws, regulations, rules and standards of the jurisdiction 19 | relating to labor and employment where the individual is physically 20 | located or where the individual was born or naturalized; or where the 21 | legal entity is registered or is operating (whichever is stricter). In 22 | case that the jurisdiction has no such laws, regulations, rules and 23 | standards or its laws, regulations, rules and standards are 24 | unenforceable, the individual or the legal entity are required to 25 | comply with Core International Labor Standards. 26 | 27 | 3. The individual or the legal entity shall not induce, suggest or force 28 | its employee(s), whether full-time or part-time, or its independent 29 | contractor(s), in any methods, to agree in oral or written form, to 30 | directly or indirectly restrict, weaken or relinquish his or her 31 | rights or remedies under such laws, regulations, rules and standards 32 | relating to labor and employment as mentioned above, no matter whether 33 | such written or oral agreements are enforceable under the laws of the 34 | said jurisdiction, nor shall such individual or the legal entity 35 | limit, in any methods, the rights of its employee(s) or independent 36 | contractor(s) from reporting or complaining to the copyright holder or 37 | relevant authorities monitoring the compliance of the license about 38 | its violation(s) of the said license. 39 | 40 | THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 41 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 42 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 43 | IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, 44 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 45 | OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE 46 | LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK. 47 | -------------------------------------------------------------------------------- /LICENSE_CN: -------------------------------------------------------------------------------- 1 | 版权所有(c)2021 皮康龙 2 | 3 | 反996许可证版本1.0 4 | 5 | 在符合下列条件的情况下,特此免费向任何得到本授权作品的副本(包括源代码、文件和/或相关内容,以 6 | 下统称为“授权作品”)的个人和法人实体授权:被授权个人或法人实体有权以任何目的处置授权作品,包括 7 | 但不限于使用、复制,修改,衍生利用、散布,发布和再许可: 8 | 9 | 1. 个人或法人实体必须在许可作品的每个再散布或衍生副本上包含以上版权声明和本许可证,不得自行修 10 | 改。 11 | 2. 个人或法人实体必须严格遵守与个人实际所在地或个人出生地或归化地、或法人实体注册地或经营地( 12 | 以较严格者为准)的司法管辖区所有适用的与劳动和就业相关法律、法规、规则和标准。如果该司法管辖区 13 | 没有此类法律、法规、规章和标准或其法律、法规、规章和标准不可执行,则个人或法人实体必须遵守国际 14 | 劳工标准的核心公约。 15 | 3. 个人或法人不得以任何方式诱导、暗示或强迫其全职或兼职员工或其独立承包人以口头或书面形式同意 16 | 直接或间接限制、削弱或放弃其所拥有的,受相关与劳动和就业有关的法律、法规、规则和标准保护的权利 17 | 或补救措施,无论该等书面或口头协议是否被该司法管辖区的法律所承认,该等个人或法人实体也不得以任 18 | 何方法限制其雇员或独立承包人向版权持有人或监督许可证合规情况的有关当局报告或投诉上述违反许可证 19 | 的行为的权利。 20 | 21 | 该授权作品是"按原样"提供,不做任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵 22 | 权性的保证。在任何情况下,无论是在合同诉讼、侵权诉讼或其他诉讼中,版权持有人均不承担因本软件或 23 | 本软件的使用或其他交易而产生、引起或与之相关的任何索赔、损害或其他责任。 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NJUPT_Data_Structure_Examples 2 | 南京邮电大学考研《数据结构》程序示例 3 | 4 | **811--《数据结构》考研大纲** 5 | 6 | 一、考核内容: 7 | 8 | 1. - [x] 绪论 9 | 10 | - [x] 1.1 算法的基本概念 11 | - [x] 1.2 数据结构的基本概念 12 | - [x] 1.3 数据抽象和抽象数据类型 13 | - [x] 1.4 描述数据结构和算法 14 | - [x] 1.5 算法分析的基本方法 15 | 16 | 2. - [x] 线性表 17 | 18 | - [x] 2.1 线性表的定义及基本操作 19 | - [x] 2.2 线性表的顺序存储 20 | - [x] 2.3 线性表的链接存储 21 | 22 | 3. - [x] 栈和队列 23 | 24 | - [x] 3.1 栈和队列的基本概念 25 | - [x] 3.2 栈和队列的顺序存储结构 26 | - [x] 3.3 栈和队列的链式存储结构 27 | - [x] 3.4 表达式计算 28 | - [x] 3.5 递归 29 | 30 | 4. - [x] 数组 31 | 32 | - [x] 4.1 数组的基本概念 33 | - [x] 4.2 特殊矩阵 34 | - [x] 4.3 稀疏矩阵 35 | 36 | 5. - [x] 树和二叉树 37 | 38 | - [x] 5.1 树的基本概念 39 | - [x] 5.2 二叉树 40 | - [x] 5.2.1 二叉树的定义及主要特征 41 | - [x] 5.2.2 二叉树的顺序存储和链式存储 42 | - [x] 5.2.3 二叉树的遍历 43 | - [x] 5.2.4 线索二叉树的基本概念和构造 44 | - [x] 5.3 树和森林 45 | - [x] 5.3.1 树的存储结构 46 | - [x] 5.3.2 森林和二叉树的转换 47 | - [x] 5.3.3 树和森林的遍历 48 | - [x] 5.4 树和二叉树的应用 49 | - [x] 5.4.1 二叉排序树 50 | - [x] 5.4.2 二叉平衡树 51 | - [x] 5.4.3 哈夫曼(Huffman)树和哈夫曼编码 52 | 53 | 6. - [ ] 图 54 | 55 | - [ ] 6.1 图的基本概念 56 | - [ ] 6.2 图的存储及基本操作 57 | - [ ] 6.2.1 邻接矩阵法 58 | - [ ] 6.2.2 邻接表表示法 59 | - [ ] 6.3 图的遍历 60 | - [ ] 6.3.1 深度优先搜索 61 | - [ ] 6.3.2 广度优先搜索 62 | - [ ] 6.4 图的基本应用 63 | - [ ] 6.4.1 拓扑排序 64 | - [ ] 6.4.2 关键路径 65 | - [ ] 6.4.3 最小代价生成树 66 | - [ ] 6.4.4 最短路径 67 | 68 | 7. - [ ] 搜索(Search) 69 | 70 | - [ ] 7.1 搜索的基本概念 71 | - [ ] 7.2 顺序搜索法 72 | - [ ] 7.3 二分搜索法 73 | - [ ] 7.4 B-树及其基本操作 74 | - [ ] 7.5 散列(Hash)表 75 | - [ ] 7.6 搜索算法的分析及应用 76 | 77 | 8. - [ ] 内排序 78 | 79 | - [ ] 8.1 排序的基本概念 80 | - [ ] 8.2 简单选择排序 81 | - [ ] 8.3 直接插入排序 82 | - [ ] 8.4 冒泡排序(bubble sort) 83 | - [ ] 8.5 希尔排序(shell sort) 84 | - [ ] 8.6 快速排序 85 | - [ ] 8.7 堆排序 86 | - [ ] 8.8 两路合并排序(merge sort) 87 | - [ ] 8.9 基数排序 88 | - [ ] 8.10 各种内部排序算法的比较 89 | - [ ] 8.11 内部排序算法的应用 90 | -------------------------------------------------------------------------------- /src/2_Linear_List/2_1_Sequential_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int ERROR = 0; 7 | const int OK = 1; 8 | 9 | typedef struct { 10 | int n; 11 | int maxLength; 12 | int *element; 13 | } SeqList; 14 | 15 | // 初始化运算。构造一个空的线性表L,若初始化成功,则返回OK,否则返回ERROR。 16 | int Init(SeqList *L, int maxSize) { 17 | L->maxLength = maxSize; 18 | L->n = 0; 19 | L->element = (int *) malloc(sizeof(int) * maxSize); 20 | if (!L->element) 21 | return ERROR; 22 | return OK; 23 | } 24 | 25 | // 撤销运算。判断线性表L是否存在,若已存在,则撤销线性表L;否则,返回ERROR。 26 | int Destroy(SeqList *L) { 27 | if (!L->n) 28 | return ERROR; 29 | (*L).n = 0; 30 | (*L).maxLength = 0; 31 | free((*L).element); 32 | return OK; 33 | } 34 | 35 | // 判空运算。判断线性表L是否为空,若为空,则返回OK;否则返回ERROR。 36 | int IsEmpty(SeqList L) { 37 | if (!L.n) 38 | return OK; 39 | return ERROR; 40 | } 41 | 42 | // 求长度运算。若线性表L已存在,返回线性表L的元素个数;否则返回ERROR。 43 | int Length(SeqList L) { 44 | if (!L.n) 45 | return ERROR; 46 | return L.n; 47 | } 48 | 49 | // 查找运算。若线性表L已存在且0≤i≤n-1,则查找线性表L中元素a_i的值并通过x返回;否则,返回ERROR。 50 | int Find(SeqList L, int i, int *x) { 51 | if (i < 0 || i > L.n - 1) 52 | return ERROR; 53 | *x = L.element[i]; 54 | return OK; 55 | } 56 | 57 | // 插入运算。若线性表L已存在且-1≤i≤n-1,则在元素a_i之后插入新元素x,插入成功后返回OK,否则返回ERROR。 58 | int Insert(SeqList *L, int i, int x) { 59 | if (i < -1 || i > L->n - 1) 60 | return ERROR; 61 | if (L->n == L->maxLength) 62 | return ERROR; 63 | for (int j = L->n - 1; j > i; j--) 64 | L->element[j + 1] = L->element[j]; 65 | L->element[i + 1] = x; 66 | L->n = L->n + 1; 67 | return OK; 68 | } 69 | 70 | // 删除运算。若线性表L非空且0≤i≤n-1,则删除元素a_i,删除成功后返回OK,否则返回ERROR。 71 | int Delete(SeqList *L, int i) { 72 | if (i < 0 || i > L->n - 1) 73 | return ERROR; 74 | if (!L->n) 75 | return ERROR; 76 | for (int j = i + 1; j < L->n; j++) 77 | L->element[j - 1] = L->element[j]; 78 | L->n--; 79 | return OK; 80 | } 81 | 82 | // 更新运算。若线性表L已存在且0≤i≤n-1,则将线性表L元素a_i的值修改为x,否则返回ERROR。 83 | int Update(SeqList *L, int i, int x) { 84 | if (i < 0 || i > L->n - 1) 85 | return ERROR; 86 | L->element[i] = x; 87 | return OK; 88 | } 89 | 90 | // 输出运算。若线性表L已存在,则输出线性表L中所有数据元素,否则返回ERROR。 91 | int Output(SeqList L) { 92 | int i; 93 | if (!L.n) 94 | return ERROR; 95 | for (i = 0; i <= L.n - 1; i++) 96 | printf("%d ", L.element[i]); 97 | printf("\n"); 98 | return OK; 99 | } 100 | 101 | int main() { 102 | SeqList list; 103 | Init(&list, 10); 104 | for (int i = 0; i < 9; i++) 105 | Insert(&list, i - 1, i); 106 | Output(list); 107 | Delete(&list, 0); 108 | Output(list); 109 | Destroy(&list); 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /src/2_Linear_List/2_2_Singly_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int ERROR = 0; 7 | const int OK = 1; 8 | 9 | typedef struct Node { 10 | int element; 11 | struct Node *link; 12 | } Node; 13 | 14 | typedef struct { 15 | struct Node *first; 16 | int n; 17 | } SingleList; 18 | 19 | int Init(SingleList *L) { 20 | L->first = NULL; 21 | L->n = 0; 22 | return OK; 23 | } 24 | 25 | int Find(SingleList L, int i, int *x) { 26 | Node *p; 27 | if (i < 0 || i > L.n - 1) 28 | return ERROR; 29 | p = L.first; 30 | for (int j = 0; j < i; j++) 31 | p = p->link; 32 | *x = p->element; 33 | return OK; 34 | } 35 | 36 | int Insert(SingleList *L, int i, int x) { 37 | Node *p, *q; 38 | if (i < -1 || i > L->n - 1) 39 | return ERROR; 40 | p = L->first; 41 | for (int j = 0; j < i; j++) 42 | p = p->link; 43 | q = (Node *) malloc(sizeof(Node)); 44 | q->element = x; 45 | if (i > -1) { 46 | q->link = p->link; 47 | p->link = q; 48 | } else { 49 | q->link = L->first; 50 | L->first = q; 51 | } 52 | L->n++; 53 | return OK; 54 | } 55 | 56 | int Delete(SingleList *L, int i) { 57 | Node *p, *q; 58 | if (!L->n) 59 | return ERROR; 60 | if (i < 0 || i > L->n - 1) 61 | return ERROR; 62 | q = L->first; 63 | p = L->first; 64 | for (int j = 0; j < i - 1; j++) 65 | q = q->link; 66 | if (i == 0) 67 | L->first = L->first->link; 68 | else { 69 | p = q->link; 70 | q->link = p->link; 71 | } 72 | free(p); 73 | L->n--; 74 | return OK; 75 | } 76 | 77 | int Output(SingleList L) { 78 | Node *p; 79 | if (!L.n) 80 | return ERROR; 81 | p = L.first; 82 | while (p) { 83 | printf("%d ", p->element); 84 | p = p->link; 85 | } 86 | printf("\n"); 87 | return OK; 88 | } 89 | 90 | void Destroy(SingleList *L) { 91 | Node *p; 92 | while (L->first) { 93 | p = L->first->link; 94 | free(L->first); 95 | L->first = p; 96 | } 97 | } 98 | 99 | int main() { 100 | int x; 101 | SingleList list; 102 | Init(&list); 103 | for (int i = 0; i < 9; i++) 104 | Insert(&list, i - 1, i); 105 | printf("The singly linked list is: "); 106 | Output(list); 107 | Delete(&list, 0); 108 | printf("The singly linked list is: "); 109 | Output(list); 110 | Find(list, 0, &x); 111 | printf("The searched value is: %d\n", x); 112 | Destroy(&list); 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /src/2_Linear_List/2_3_Header_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int ERROR = 0; 7 | const int OK = 1; 8 | 9 | typedef struct Node { 10 | int element; 11 | struct Node *link; 12 | } Node; 13 | 14 | typedef struct { 15 | struct Node *head; 16 | int n; 17 | } HeaderList; 18 | 19 | int Init(HeaderList *h) { 20 | h->head = (Node *) malloc(sizeof(Node)); 21 | if (!h->head) 22 | return ERROR; 23 | h->head->link = NULL; 24 | h->n = 0; 25 | return OK; 26 | } 27 | 28 | int Find(HeaderList h, int i, int *x) { 29 | Node *p; 30 | if (i < 0 || i > h.n - 1) 31 | return ERROR; 32 | p = h.head->link; 33 | for (int j = 0; j < i; j++) 34 | p = p->link; 35 | *x = p->element; 36 | return OK; 37 | } 38 | 39 | int Insert(HeaderList *h, int i, int x) { 40 | if (i < -1 || i > h->n - 1) 41 | return ERROR; 42 | Node *p = h->head; 43 | for (int j = 0; j <= i; j++) 44 | p = p->link; 45 | Node *q = (Node *) malloc(sizeof(Node)); 46 | q->element = x; 47 | q->link = p->link; 48 | p->link = q; 49 | h->n++; 50 | return OK; 51 | } 52 | 53 | int Delete(HeaderList *h, int i) { 54 | Node *p, *q; 55 | if (!h->n) 56 | return ERROR; 57 | if (i < 0 || i > h->n - 1) 58 | return ERROR; 59 | q = h->head; 60 | for (int j = 0; j < i; j++) 61 | q = q->link; 62 | p = q->link; 63 | q->link = p->link; 64 | free(p); 65 | h->n--; 66 | return OK; 67 | } 68 | 69 | int Output(HeaderList h) { 70 | Node *p; 71 | if (!h.n) 72 | return ERROR; 73 | p = h.head->link; 74 | while (p) { 75 | printf("%d ", p->element); 76 | p = p->link; 77 | } 78 | printf("\n"); 79 | return OK; 80 | } 81 | 82 | void Destroy(HeaderList *h) { 83 | Node *p, *q; 84 | q = h->head->link; 85 | while (q) { 86 | p = q->link; 87 | free(q); 88 | q = p; 89 | } 90 | free(h->head); 91 | } 92 | 93 | int main() { 94 | int x; 95 | HeaderList list; 96 | Init(&list); 97 | for (int i = 0; i < 9; i++) 98 | Insert(&list, i - 1, i); 99 | printf("The header linked list is: "); 100 | Output(list); 101 | Delete(&list, 0); 102 | printf("The header linked list is: "); 103 | Output(list); 104 | Find(list, 0, &x); 105 | printf("The searched value is: %d\n", x); 106 | Destroy(&list); 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /src/2_Linear_List/2_4_Circular_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int ERROR = 0; 7 | const int OK = 1; 8 | 9 | typedef struct Node { 10 | int element; 11 | struct Node *link; 12 | } Node; 13 | 14 | typedef struct { 15 | struct Node *head; 16 | int n; 17 | } HeaderList; 18 | 19 | int Init(HeaderList *h) { 20 | h->head = (Node *) malloc(sizeof(Node)); 21 | if (!h->head) 22 | return ERROR; 23 | h->head->link = h->head; 24 | h->n = 0; 25 | return OK; 26 | } 27 | 28 | int Find(HeaderList h, int i, int *x) { 29 | Node *p; 30 | if (i < 0 || i > h.n - 1) 31 | return ERROR; 32 | p = h.head->link; 33 | for (int j = 0; j < i; j++) 34 | p = p->link; 35 | *x = p->element; 36 | return OK; 37 | } 38 | 39 | int Insert(HeaderList *h, int i, int x) { 40 | if (i < -1 || i > h->n - 1) 41 | return ERROR; 42 | Node *p = h->head; 43 | for (int j = 0; j <= i; j++) 44 | p = p->link; 45 | Node *q = (Node *) malloc(sizeof(Node)); 46 | q->element = x; 47 | q->link = p->link; 48 | p->link = q; 49 | h->n++; 50 | return OK; 51 | } 52 | 53 | int Delete(HeaderList *h, int i) { 54 | Node *p, *q; 55 | if (!h->n) 56 | return ERROR; 57 | if (i < 0 || i > h->n - 1) 58 | return ERROR; 59 | q = h->head; 60 | for (int j = 0; j < i; j++) 61 | q = q->link; 62 | p = q->link; 63 | q->link = p->link; 64 | free(p); 65 | h->n--; 66 | return OK; 67 | } 68 | 69 | int Output(HeaderList h) { 70 | Node *p; 71 | if (!h.n) 72 | return ERROR; 73 | p = h.head->link; 74 | while (p != h.head) { 75 | printf("%d ", p->element); 76 | p = p->link; 77 | } 78 | printf("\n"); 79 | return OK; 80 | } 81 | 82 | void Destroy(HeaderList *h) { 83 | Node *p, *q; 84 | q = h->head->link; 85 | while (q != h->head) { 86 | p = q->link; 87 | free(q); 88 | q = p; 89 | } 90 | free(h->head); 91 | } 92 | 93 | int main() { 94 | int x; 95 | HeaderList list; 96 | Init(&list); 97 | for (int i = 0; i < 9; i++) 98 | Insert(&list, i - 1, i); 99 | printf("The circular linked list is: "); 100 | Output(list); 101 | Delete(&list, 0); 102 | printf("The circular linked list is: "); 103 | Output(list); 104 | Find(list, 0, &x); 105 | printf("The searched value is: %d\n", x); 106 | Destroy(&list); 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /src/2_Linear_List/2_5_Doubly_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int ERROR = 0; 7 | const int OK = 1; 8 | 9 | typedef struct Node { 10 | int element; 11 | struct Node *lLink; 12 | struct Node *rLink; 13 | } Node; 14 | 15 | typedef struct { 16 | struct Node *first; 17 | int n; 18 | } DoubleList; 19 | 20 | int Init(DoubleList *L) { 21 | L->first = NULL; 22 | L->n = 0; 23 | return OK; 24 | } 25 | 26 | int Find(DoubleList L, int i, int *x) { 27 | Node *p; 28 | if (i < 0 || i > L.n - 1) 29 | return ERROR; 30 | p = L.first; 31 | for (int j = 0; j < i; j++) 32 | p = p->rLink; 33 | *x = p->element; 34 | return OK; 35 | } 36 | 37 | int Insert(DoubleList *L, int i, int x) { 38 | Node *p, *q; 39 | if (i < -1 || i > L->n - 1) 40 | return ERROR; 41 | p = L->first; 42 | for (int j = 0; j < i; j++) 43 | p = p->rLink; 44 | q = (Node *) malloc(sizeof(Node)); 45 | q->element = x; 46 | if (i > -1) { 47 | q->rLink = p->rLink; 48 | q->lLink = p; 49 | if (p->rLink != NULL) 50 | p->rLink->lLink = q; 51 | p->rLink = q; 52 | } else { 53 | q->lLink = NULL; 54 | q->rLink = L->first; 55 | if (q->rLink != NULL) 56 | q->rLink->lLink = q; 57 | L->first = q; 58 | } 59 | L->n++; 60 | return OK; 61 | } 62 | 63 | int Delete(DoubleList *L, int i) { 64 | Node *p, *q; 65 | if (!L->n) 66 | return ERROR; 67 | if (i < 0 || i > L->n - 1) 68 | return ERROR; 69 | q = L->first; 70 | p = L->first; 71 | for (int j = 0; j < i - 1; j++) 72 | q = q->rLink; 73 | if (i == 0) { 74 | if (L->first->rLink != NULL) 75 | L->first->rLink->lLink = NULL; 76 | L->first = L->first->rLink; 77 | } else { 78 | p = q->rLink; 79 | q->rLink = p->rLink; 80 | if (p->rLink != NULL) 81 | p->rLink->lLink = q; 82 | } 83 | free(p); 84 | L->n--; 85 | return OK; 86 | } 87 | 88 | int Output(DoubleList L) { 89 | Node *p; 90 | if (!L.n) 91 | return ERROR; 92 | p = L.first; 93 | while (p) { 94 | printf("%d ", p->element); 95 | p = p->rLink; 96 | } 97 | printf("\n"); 98 | return OK; 99 | } 100 | 101 | void Destroy(DoubleList *L) { 102 | Node *p; 103 | while (L->first) { 104 | p = L->first->rLink; 105 | free(L->first); 106 | L->first = p; 107 | } 108 | } 109 | 110 | int main() { 111 | int x; 112 | DoubleList list; 113 | Init(&list); 114 | for (int i = 0; i < 9; i++) 115 | Insert(&list, i - 1, i); 116 | printf("The doubly linked list is: "); 117 | Output(list); 118 | Delete(&list, 0); 119 | printf("The doubly linked list is: "); 120 | Output(list); 121 | Find(list, 0, &x); 122 | printf("The searched value is: %d\n", x); 123 | Destroy(&list); 124 | return 0; 125 | } -------------------------------------------------------------------------------- /src/2_Linear_List/2_6_Doubly_Circular_Linked_List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int ERROR = 0; 7 | const int OK = 1; 8 | 9 | typedef struct Node { 10 | int element; 11 | struct Node *lLink; 12 | struct Node *rLink; 13 | } Node; 14 | 15 | typedef struct { 16 | struct Node *first; 17 | int n; 18 | } DoubleList; 19 | 20 | int Init(DoubleList *L) { 21 | L->first = NULL; 22 | L->n = 0; 23 | return OK; 24 | } 25 | 26 | int Find(DoubleList L, int i, int *x) { 27 | Node *p; 28 | if (i < 0 || i > L.n - 1) 29 | return ERROR; 30 | p = L.first; 31 | for (int j = 0; j < i; j++) 32 | p = p->rLink; 33 | *x = p->element; 34 | return OK; 35 | } 36 | 37 | int Insert(DoubleList *L, int i, int x) { 38 | Node *p, *q; 39 | if (i < -1 || i > L->n - 1) 40 | return ERROR; 41 | p = L->first; 42 | for (int j = 0; j < i; j++) 43 | p = p->rLink; 44 | q = (Node *) malloc(sizeof(Node)); 45 | q->element = x; 46 | // 注释中的是原始版本,易于理解。 47 | // 但第一个代码块中的代码在第三个代码块的条件下可以实现一样的结果,故对代码进行了简化。 48 | // if (i > -1) { 49 | // q->rLink = p->rLink; 50 | // q->lLink = p; 51 | // p->rLink->lLink = q; 52 | // p->rLink = q; 53 | // } else if (L->first == NULL) { 54 | // q->lLink = q; 55 | // q->rLink = q; 56 | // L->first = q; 57 | // } else { 58 | // q->rLink = L->first; 59 | // q->lLink = L->first->lLink; 60 | // L->first->lLink->rLink = q; 61 | // L->first->lLink = q; 62 | // } 63 | if (L->first != NULL) { 64 | q->rLink = p->rLink; 65 | q->lLink = p; 66 | p->rLink->lLink = q; 67 | p->rLink = q; 68 | } else { 69 | q->lLink = q; 70 | q->rLink = q; 71 | L->first = q; 72 | } 73 | L->n++; 74 | return OK; 75 | } 76 | 77 | int Delete(DoubleList *L, int i) { 78 | Node *p, *q; 79 | if (!L->n) 80 | return ERROR; 81 | if (i < 0 || i > L->n - 1) 82 | return ERROR; 83 | q = L->first; 84 | p = L->first; 85 | for (int j = 0; j < i - 1; j++) 86 | q = q->rLink; 87 | if (i == 0) { 88 | if (L->first == L->first->rLink) { 89 | L->first = NULL; 90 | } else { 91 | L->first->rLink->lLink = L->first->lLink; 92 | L->first->lLink->rLink = L->first->rLink; 93 | L->first = L->first->rLink; 94 | } 95 | } else { 96 | p = q->rLink; 97 | q->rLink = p->rLink; 98 | p->rLink->lLink = q; 99 | } 100 | free(p); 101 | L->n--; 102 | return OK; 103 | } 104 | 105 | int Output(DoubleList L) { 106 | Node *p; 107 | if (!L.n) 108 | return ERROR; 109 | p = L.first; 110 | for (int i = 0; i < L.n; i++) { 111 | printf("%d ", p->element); 112 | p = p->rLink; 113 | } 114 | printf("\n"); 115 | return OK; 116 | } 117 | 118 | void Destroy(DoubleList *L) { 119 | Node *p, *q; 120 | q = L->first; 121 | while (L->first) { 122 | if (L->first->rLink == q) 123 | p = NULL; 124 | else 125 | p = L->first->rLink; 126 | free(L->first); 127 | L->first = p; 128 | } 129 | } 130 | 131 | int main() { 132 | int x; 133 | DoubleList list; 134 | Init(&list); 135 | for (int i = 0; i < 9; i++) 136 | Insert(&list, i - 1, i); 137 | printf("The doubly circular linked list is: "); 138 | Output(list); 139 | Delete(&list, 0); 140 | printf("The doubly circular linked list is: "); 141 | Output(list); 142 | Find(list, 0, &x); 143 | printf("The searched value is: %d\n", x); 144 | Destroy(&list); 145 | return 0; 146 | } 147 | -------------------------------------------------------------------------------- /src/3_Stack_and_Queue/3_1_Sequential_Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef struct Stack { 7 | int top; 8 | int maxSize; 9 | int *element; 10 | } Stack; 11 | 12 | // 创建一个能容纳mSize个单元的空堆栈 13 | void Create(Stack *S, int mSize) { 14 | S->maxSize = mSize; 15 | S->element = (int *) malloc(sizeof(int) * mSize); 16 | S->top = -1; 17 | } 18 | 19 | // 销毁一个已存在的堆栈,释放堆栈占用的空间 20 | void Destroy(Stack *S) { 21 | S->maxSize = -1; 22 | free(S->element); 23 | S->top = -1; 24 | } 25 | 26 | // 清除堆栈中全部元素,但并不释放空间 27 | void Clear(Stack *S) { 28 | S->top = -1; 29 | } 30 | 31 | // 判断堆栈是否已满,若是则返回true,否则返回false 32 | bool IsFull(Stack S) { 33 | return S.top == S.maxSize - 1; 34 | } 35 | 36 | // 判断堆栈是否为空,若是则返回true,否则返回false 37 | bool IsEmpty(Stack S) { 38 | return S.top == -1; 39 | } 40 | 41 | // 获取栈顶元素,并通过x返回。若操作成功,则返回true,否则返回false 42 | bool Top(Stack S, int *x) { 43 | if (IsEmpty(S)) 44 | return false; 45 | *x = S.element[S.top]; 46 | return true; 47 | } 48 | 49 | // 在栈顶位置插入元素x(入栈操作)。若操作成功,则返回true,否则返回false 50 | bool Push(Stack *S, int x) { 51 | if (IsFull(*S)) 52 | return false; 53 | S->top++; 54 | S->element[S->top] = x; 55 | return true; 56 | } 57 | 58 | // 删除栈顶元素(出栈操作)。若操作成功,则返回true,否则返回false 59 | bool Pop(Stack *S) { 60 | if (IsEmpty(*S)) 61 | return false; 62 | S->top--; 63 | return true; 64 | } 65 | 66 | int main() { 67 | int x; 68 | Stack stack; 69 | Create(&stack, 10); 70 | for (int i = 0; i < 9; i++) 71 | Push(&stack, i); 72 | for (int i = 0; i < 5; i++) { 73 | Top(stack, &x); 74 | printf("%d ", x); 75 | Pop(&stack); 76 | } 77 | Destroy(&stack); 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /src/3_Stack_and_Queue/3_2_Linked_Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef struct Node { 7 | int element; 8 | struct Node *link; 9 | } Node; 10 | 11 | typedef struct Stack { 12 | Node *top; 13 | } Stack; 14 | 15 | void Create(Stack *S) { 16 | S->top = NULL; 17 | } 18 | 19 | void Destroy(Stack *S) { 20 | Node *p; 21 | while (S->top) { 22 | p = S->top->link; 23 | free(S->top); 24 | S->top = p; 25 | } 26 | } 27 | 28 | // Clear函数内容和Destroy函数基本一致 29 | //void Clear(Stack *S) { 30 | // Node *p; 31 | // while (S->top) { 32 | // p = S->top->link; 33 | // free(S->top); 34 | // S->top = p; 35 | // } 36 | //} 37 | 38 | //void Clear(Stack *S) { 39 | // while (!IsEmpty(*S)) { 40 | // Pop(S); 41 | // } 42 | //} 43 | 44 | bool IsEmpty(Stack S) { 45 | return S.top == NULL; 46 | } 47 | 48 | bool Top(Stack S, int *x) { 49 | if (IsEmpty(S)) 50 | return false; 51 | *x = S.top->element; 52 | return true; 53 | } 54 | 55 | bool Push(Stack *S, int x) { 56 | Node *p = (Node *) malloc(sizeof(Node)); 57 | p->element = x; 58 | p->link = S->top; 59 | S->top = p; 60 | } 61 | 62 | bool Pop(Stack *S) { 63 | if (IsEmpty(*S)) 64 | return false; 65 | Node *p = S->top; 66 | S->top = p->link; 67 | free(p); 68 | return true; 69 | } 70 | 71 | int main() { 72 | int x; 73 | Stack stack; 74 | Create(&stack); 75 | for (int i = 0; i < 9; i++) 76 | Push(&stack, i); 77 | for (int i = 0; i < 5; i++) { 78 | Top(stack, &x); 79 | printf("%d ", x); 80 | Pop(&stack); 81 | } 82 | Destroy(&stack); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /src/3_Stack_and_Queue/3_3_Circular_Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef struct Queue { 7 | int front; 8 | int rear; 9 | int maxSize; 10 | int *element; 11 | } Queue; 12 | 13 | void Create(Queue *Q, int mSize) { 14 | Q->maxSize = mSize; 15 | Q->element = (int *) malloc(sizeof(int) * mSize); 16 | Q->front = 0; 17 | Q->rear = 0; 18 | } 19 | 20 | void Destroy(Queue *Q) { 21 | free(Q->element); 22 | Q->maxSize = -1; 23 | Q->front = -1; 24 | Q->rear = -1; 25 | } 26 | 27 | void Clear(Queue *Q) { 28 | Q->front = 0; 29 | Q->rear = 0; 30 | } 31 | 32 | bool IsEmpty(Queue Q) { 33 | return Q.front == Q.rear; 34 | } 35 | 36 | bool IsFull(Queue Q) { 37 | return (Q.rear + 1) % Q.maxSize == Q.front; 38 | } 39 | 40 | bool Front(Queue Q, int *x) { 41 | if (IsEmpty(Q)) 42 | return false; 43 | *x = Q.element[(Q.front + 1) % Q.maxSize]; 44 | return true; 45 | } 46 | 47 | bool EnQueue(Queue *Q, int x) { 48 | if (IsFull(*Q)) 49 | return false; 50 | Q->rear = (Q->rear + 1) % Q->maxSize; 51 | Q->element[Q->rear] = x; 52 | return true; 53 | } 54 | 55 | bool DeQueue(Queue *Q) { 56 | if (IsEmpty(*Q)) 57 | return false; 58 | Q->front = (Q->front + 1) % Q->maxSize; 59 | return true; 60 | } 61 | 62 | int main() { 63 | int x; 64 | Queue queue; 65 | Create(&queue, 10); 66 | for (int i = 0; i < 9; i++) 67 | EnQueue(&queue, i); 68 | for (int i = 0; i < 5; i++) { 69 | Front(queue, &x); 70 | printf("%d ", x); 71 | DeQueue(&queue); 72 | } 73 | Destroy(&queue); 74 | return 0; 75 | } -------------------------------------------------------------------------------- /src/3_Stack_and_Queue/3_4_Linked_Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef struct Node { 7 | int element; 8 | struct Node *link; 9 | } Node; 10 | 11 | typedef struct Queue { 12 | Node *front; 13 | Node *rear; 14 | } Queue; 15 | 16 | void Create(Queue *Q) { 17 | Q->front = NULL; 18 | Q->rear = NULL; 19 | } 20 | 21 | void Destroy(Queue *Q) { 22 | Node *p; 23 | while (Q->front != NULL) { 24 | p = Q->front; 25 | Q->front = Q->front->link; 26 | free(p); 27 | } 28 | } 29 | 30 | bool IsEmpty(Queue Q) { 31 | return Q.front == NULL; 32 | } 33 | 34 | bool Front(Queue Q, int *x) { 35 | if (IsEmpty(Q)) 36 | return false; 37 | *x = Q.front->element; 38 | return true; 39 | } 40 | 41 | void EnQueue(Queue *Q, int x) { 42 | Node *p = (Node *) malloc(sizeof(Node)); 43 | p->element = x; 44 | p->link = NULL; 45 | if(Q->front == NULL) { 46 | Q->front = p; 47 | Q->rear = p; 48 | } else { 49 | Q->rear->link = p; 50 | Q->rear = p; 51 | } 52 | } 53 | 54 | bool DeQueue(Queue *Q) { 55 | if (IsEmpty(*Q)) 56 | return false; 57 | Node *p = Q->front; 58 | Q->front = p->link; 59 | free(p); 60 | if (Q->front == NULL) 61 | Q->rear = NULL; 62 | return true; 63 | } 64 | 65 | int main() { 66 | int x; 67 | Queue queue; 68 | Create(&queue); 69 | for (int i = 0; i < 9; i++) 70 | EnQueue(&queue, i); 71 | for (int i = 0; i < 5; i++) { 72 | Front(queue, &x); 73 | printf("%d ", x); 74 | DeQueue(&queue); 75 | } 76 | Destroy(&queue); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /src/4_Array/4_1_Three-dimensional_Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef struct TDArray { 7 | int m1; 8 | int m2; 9 | int m3; 10 | int *array; 11 | } TDArray; 12 | 13 | bool CreateArray(TDArray *tdArray, int m1, int m2, int m3) { 14 | tdArray->m1 = m1; 15 | tdArray->m2 = m2; 16 | tdArray->m3 = m3; 17 | tdArray->array = (int *) malloc(m1 * m2 * m3 * sizeof(int)); 18 | if (!tdArray->array) 19 | return false; 20 | return true; 21 | } 22 | 23 | bool DestroyArray(TDArray *tdArray) { 24 | if (!tdArray) 25 | return false; 26 | if (tdArray->array) 27 | free(tdArray->array); 28 | return true; 29 | } 30 | 31 | bool RetrieveArray(TDArray tdArray, int i1, int i2, int i3, int *x) { 32 | if (!tdArray.array) 33 | return false; 34 | if (i1 < 0 || i2 < 0 || i3 < 0 || i1 >= tdArray.m1 || i2 >= tdArray.m2 || i3 >= tdArray.m3) 35 | return false; 36 | *x = *(tdArray.array + i1 * tdArray.m2 * tdArray.m3 + i2 * tdArray.m3 + i3); 37 | return true; 38 | } 39 | 40 | bool StoreArrayItem(TDArray *tdArray, int i1, int i2, int i3, int x) { 41 | if (!tdArray->array) 42 | return false; 43 | if (i1 < 0 || i2 < 0 || i3 < 0 || i1 >= tdArray->m1 || i2 >= tdArray->m2 || i3 >= tdArray->m3) 44 | return false; 45 | *(tdArray->array + i1 * tdArray->m2 * tdArray->m3 + i2 * tdArray->m3 + i3) = x; 46 | return true; 47 | } 48 | 49 | int main() { 50 | int x; 51 | TDArray tdArray; 52 | CreateArray(&tdArray, 3, 4, 5); 53 | for (int i = 0; i < tdArray.m1; i++) { 54 | for (int j = 0; j < tdArray.m2; j++) { 55 | for (int k = 0; k < tdArray.m3; k++) { 56 | StoreArrayItem(&tdArray, i, j, k, i*31*31 + j*31 + k); 57 | } 58 | } 59 | } 60 | for (int i = 0; i < tdArray.m1; i++) { 61 | for (int j = 0; j < tdArray.m2; j++) { 62 | for (int k = 0; k < tdArray.m3; k++) { 63 | RetrieveArray(tdArray, i, j, k, &x); 64 | printf("%d ", x); 65 | } 66 | printf("\n"); 67 | } 68 | printf("\n"); 69 | } 70 | DestroyArray(&tdArray); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /src/4_Array/4_2_Sparse_Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef struct Triad { 7 | int row; 8 | int col; 9 | int value; 10 | } Triad; 11 | 12 | typedef struct SparseMatrix { 13 | int m; 14 | int n; 15 | int t; 16 | Triad table[1024]; 17 | } SparseMatrix; 18 | 19 | bool Create(SparseMatrix *SM, int m, int n) { 20 | SM->m = m; 21 | SM->n = n; 22 | SM->t = 0; 23 | } 24 | 25 | bool Destroy(SparseMatrix *SM) { 26 | SM->m = -1; 27 | SM->n = -1; 28 | SM->t = -1; 29 | } 30 | 31 | bool Insert(SparseMatrix *SM, int m, int n, int x) { 32 | if (x == 0) 33 | return false; 34 | if (SM->t == 1024) 35 | return false; 36 | if (m < 0 || m >= SM->m || n < 0 || n >= SM->n) 37 | return false; 38 | if (SM->t == 0) { 39 | SM->table[0].row = m; 40 | SM->table[0].col = n; 41 | SM->table[0].value = x; 42 | SM->t++; 43 | return true; 44 | } 45 | for (int i = 0; i < SM->t; i++) { 46 | if (SM->table[i].row > m || SM->table[i].row == m && SM->table[i].col > n) { 47 | for (int j = SM->t - 1; j >= i; j--) 48 | SM->table[j + 1] = SM->table[j]; 49 | SM->table[i].row = m; 50 | SM->table[i].col = n; 51 | SM->table[i].value = x; 52 | SM->t++; 53 | break; 54 | } 55 | if (i == SM->t - 1) { 56 | SM->table[i + 1].row = m; 57 | SM->table[i + 1].col = n; 58 | SM->table[i + 1].value = x; 59 | SM->t++; 60 | break; 61 | } 62 | } 63 | return true; 64 | } 65 | 66 | bool Delete(SparseMatrix *SM, int m, int n) { 67 | for (int i = 0; i < SM->t; i++) { 68 | if (SM->table[i].row == m && SM->table[i].col == n) { 69 | for (int j = i; j < SM->t - 1; j++) 70 | SM->table[j] = SM->table[j + 1]; 71 | SM->t--; 72 | return true; 73 | } 74 | } 75 | return false; 76 | } 77 | 78 | // O(n+t) 79 | void Transpose(SparseMatrix *SM) { 80 | SparseMatrix temp; 81 | int *num = (int *) malloc(sizeof(int) * SM->n); 82 | int *k = (int *) malloc(sizeof(int) * SM->n); 83 | temp.m = SM->n; 84 | temp.n = SM->m; 85 | temp.t = SM->t; 86 | if (SM->t > 0) { 87 | for (int i = 0; i < SM->n; i++) 88 | num[i] = 0; 89 | for (int i = 0; i < SM->t; i++) 90 | num[SM->table[i].col]++; 91 | k[0] = 0; 92 | for (int i = 1; i < SM->n; i++) 93 | k[i] = k[i-1] + num[i-1]; 94 | for (int i = 0; i < SM->t; i++) { 95 | int j = k[SM->table[i].col]++; 96 | temp.table[j].row = SM->table[i].col; 97 | temp.table[j].col = SM->table[i].row; 98 | temp.table[j].value = SM->table[i].value; 99 | } 100 | } 101 | *SM = temp; 102 | free(num); 103 | free(k); 104 | } 105 | 106 | void Output(SparseMatrix SM) { 107 | for (int i = 0; i < SM.t; i++) 108 | printf("%d %d %d\n", SM.table[i].row, SM.table[i].col, SM.table[i].value); 109 | } 110 | 111 | int main() { 112 | SparseMatrix sm; 113 | Create(&sm, 30, 30); 114 | Insert(&sm, 1, 1, 4); 115 | Insert(&sm, 5, 1, 4); 116 | Insert(&sm, 1, 9, 1); 117 | Insert(&sm, 9, 8, 10); 118 | Output(sm); 119 | printf("\n"); 120 | Transpose(&sm); 121 | Output(sm); 122 | Destroy(&sm); 123 | return 0; 124 | } 125 | -------------------------------------------------------------------------------- /src/5_Tree_and_Binary_Tree/5_1_Binary_Tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef struct BTNode { 8 | char element; 9 | struct BTNode *lChild; 10 | struct BTNode *rChild; 11 | } BTNode; 12 | 13 | typedef struct BinaryTree { 14 | BTNode *root; 15 | } BinaryTree; 16 | 17 | // 构造一棵空二叉树BT 18 | void Create(BinaryTree *BT) { 19 | BT->root = NULL; 20 | } 21 | 22 | // 创建新结点,该结点的数据为x,ln和rn为该结点的左右孩子 23 | BTNode* NewNode(char x, BTNode *ln, BTNode *rn) { 24 | BTNode *p = (BTNode *) malloc(sizeof(BTNode)); 25 | p->element = x; 26 | p->lChild = ln; 27 | p->rChild = rn; 28 | return p; 29 | } 30 | 31 | // 若二叉树非空,则x为根结点的值,并返回true,否则返回false 32 | bool Root(BinaryTree *BT, char *x) { 33 | if (BT->root) { 34 | *x = BT->root->element; 35 | return true; 36 | } else 37 | return false; 38 | } 39 | 40 | // 构造二叉树BT,根结点的值为e,left和right为根的左右子树 41 | void MakeTree(BinaryTree *BT, char e, BinaryTree *left, BinaryTree *right) { 42 | if (BT->root || left==right) 43 | return; 44 | BT->root = NewNode(e, left->root, right->root); 45 | left->root = NULL; 46 | right->root = NULL; 47 | } 48 | 49 | void PreOrder(BTNode *t) { 50 | if(!t) 51 | return; 52 | printf("%c ", t->element); 53 | PreOrder(t->lChild); 54 | PreOrder(t->rChild); 55 | } 56 | 57 | void PreOrderTree(BinaryTree *BT) { 58 | PreOrder(BT->root); 59 | } 60 | 61 | void InOrder(BTNode *t) { 62 | if(!t) 63 | return; 64 | InOrder(t->lChild); 65 | printf("%c ", t->element); 66 | InOrder(t->rChild); 67 | } 68 | 69 | void InOrderTree(BinaryTree *BT) { 70 | InOrder(BT->root); 71 | } 72 | 73 | void PostOrder(BTNode *t) { 74 | if(!t) 75 | return; 76 | PostOrder(t->lChild); 77 | PostOrder(t->rChild); 78 | printf("%c ", t->element); 79 | } 80 | 81 | void PostOrderTree(BinaryTree *BT) { 82 | PostOrder(BT->root); 83 | } 84 | 85 | void LevelOrderTree(BinaryTree *tree) { 86 | queue q; 87 | BTNode *p; 88 | if (!tree->root) 89 | return; 90 | q.push(tree->root); 91 | while (!q.empty()) { 92 | p = q.front(); 93 | q.pop(); 94 | printf("%c ", p->element); 95 | if(p->lChild) 96 | q.push(p->lChild); 97 | if(p->rChild) 98 | q.push(p->rChild); 99 | } 100 | } 101 | 102 | void Clear(BTNode *t) { 103 | if(!t) 104 | return; 105 | Clear(t->lChild); 106 | Clear(t->rChild); 107 | free(t); 108 | } 109 | 110 | void ClearTree(BinaryTree *BT) { 111 | Clear(BT->root); 112 | } 113 | 114 | int main() { 115 | BinaryTree a, b, x, y, z; 116 | Create(&a); 117 | Create(&b); 118 | Create(&x); 119 | Create(&y); 120 | Create(&z); 121 | MakeTree(&y, 'E', &a, &b); 122 | MakeTree(&z, 'F', &a, &b); 123 | MakeTree(&x, 'C', &y, &z); 124 | MakeTree(&y, 'D', &a, &b); 125 | MakeTree(&z, 'B', &y, &x); 126 | PreOrderTree(&z); 127 | printf("\n"); 128 | InOrderTree(&z); 129 | printf("\n"); 130 | PostOrderTree(&z); 131 | printf("\n"); 132 | LevelOrderTree(&z); 133 | printf("\n"); 134 | ClearTree(&z); 135 | return 0; 136 | } 137 | --------------------------------------------------------------------------------