├── DataStructureSourceCode ├── 串 │ ├── 块链串.cpp │ ├── 堆串.cpp │ └── 串的模式匹配(采用堆串).cpp ├── 栈与队列 │ ├── 链式栈.cpp │ ├── 链式队列.cpp │ ├── 顺序栈.cpp │ └── 顺序循环队列.cpp ├── 线性表 │ ├── 循环单链表.cpp │ ├── 循环双向链表.cpp │ ├── 静态链表.cpp │ ├── 非循环单链表.cpp │ ├── 非循环双向链表.cpp │ ├── 顺序表(可以动态扩容).cpp │ └── 顺序表(无法动态扩容).cpp ├── 递归 │ ├── 递归 汉诺塔.cpp │ ├── 递归 求阶乘.cpp │ └── 递归 求和.cpp ├── 数组和广义表 │ ├── 十字链表.cpp │ ├── 特殊矩阵压缩存储.cpp │ ├── 稀疏矩阵压缩存储.cpp │ ├── 广义表头尾链表存储.cpp │ ├── 广义表扩展线性链表存储.cpp │ └── n维数组.cpp ├── 图 │ ├── 邻接表-图 │ │ ├── 图的邻接表存储.cpp │ │ ├── ALGraphQueue.cpp │ │ └── ALGraphLinkList.cpp │ ├── 十字链表-图 │ │ ├── 图的十字链表存储.cpp │ │ ├── OLGraphQueue.cpp │ │ └── OLGraphLinkList.cpp │ ├── 邻接矩阵-图 │ │ ├── 图的邻接矩阵存储.cpp │ │ └── MGraphQueue.cpp │ ├── 图-关键路径 │ │ ├── CritiStack.cpp │ │ ├── CriticalPath.cpp │ │ ├── ALGraphFunction.cpp │ │ └── ALGraphLinkList.cpp │ ├── 图-拓扑排序 │ │ ├── TopoStack.cpp │ │ ├── ALGraphFunction.cpp │ │ ├── TopologicalSort.cpp │ │ └── ALGraphLinkList.cpp │ ├── 邻接多重表-图 │ │ ├── 图的邻接多重表存储.cpp │ │ └── AMLGraphQueue.cpp │ ├── 图-最小生成树 │ │ ├── Prim │ │ │ ├── MGraphFunction.cpp │ │ │ └── MiniSpanTree_Prim.cpp │ │ └── Kruskal │ │ │ ├── MGraphFunction.cpp │ │ │ └── MiniSpanTree_Kruskal.cpp │ └── 图-最短路径 │ │ ├── Floyd │ │ ├── MGraphFunction.cpp │ │ └── ShortestPath_Floyd.cpp │ │ └── Dijkstra │ │ ├── MGraphFunction.cpp │ │ └── ShortestPath_Dijkstra.cpp ├── 查找与排序 │ ├── 基数排序 │ │ ├── 基数排序.cpp │ │ └── 静态链表.cpp │ ├── 插入排序 │ │ ├── 希尔排序.cpp │ │ ├── 直接插入排序.cpp │ │ └── 折半插入排序.cpp │ ├── 树型查找 │ │ ├── B树.cpp │ │ ├── 平衡二叉树.cpp │ │ ├── 字典树 │ │ │ ├── Trie树.cpp │ │ │ ├── 双链键树.cpp │ │ │ └── DictionaryTreeStack.cpp │ │ ├── 二叉排序树.cpp │ │ └── 红黑树.cpp │ ├── 哈希表 │ │ └── HashTable.cpp │ ├── 选择排序 │ │ ├── 直接选择排序.cpp │ │ └── 堆排序.cpp │ ├── 交换排序 │ │ ├── 冒泡排序.cpp │ │ └── 快速排序.cpp │ ├── 归并排序 │ │ └── 归并排序.cpp │ └── 线型查找 │ │ └── 顺序查找.cpp └── 树 │ ├── LinkBiTree │ ├── 链式二叉树.cpp │ ├── LinkBiTree.jpg │ ├── LinkBiTreeQueue.cpp │ └── LinkBiTreeStack.cpp │ ├── SqBiTree │ ├── SqBiTree.jpg │ ├── 二叉树的顺序存储.cpp │ └── SqBiTreeQueue.cpp │ ├── SqPCTree │ ├── 树的双亲孩子存储.cpp │ └── SqPCTreeQueue.cpp │ ├── LinkPBiTree │ ├── 二叉树的三叉链表.cpp │ └── LinkPBiTreeQueue.cpp │ ├── HuffmanTree │ ├── HuffmanTree.cpp │ └── 26个英文字母在文献中出现的概率.txt │ ├── LinkCSBiTree │ ├── 树的孩子兄弟存储.cpp │ └── LinkCSBiTreeQueue.cpp │ └── LinkThreadBiTree │ ├── 线索二叉树.cpp │ └── LinkThreadBiTreeQueue.cpp └── README.md /DataStructureSourceCode/串/块链串.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/串/块链串.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/串/堆串.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/串/堆串.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/栈与队列/链式栈.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/栈与队列/链式栈.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/栈与队列/链式队列.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/栈与队列/链式队列.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/栈与队列/顺序栈.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/栈与队列/顺序栈.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/线性表/循环单链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/线性表/循环单链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/线性表/循环双向链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/线性表/循环双向链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/线性表/静态链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/线性表/静态链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/线性表/非循环单链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/线性表/非循环单链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/递归/递归 汉诺塔.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/递归/递归 汉诺塔.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/数组和广义表/十字链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/数组和广义表/十字链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/栈与队列/顺序循环队列.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/栈与队列/顺序循环队列.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/线性表/非循环双向链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/线性表/非循环双向链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/串/串的模式匹配(采用堆串).cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/串/串的模式匹配(采用堆串).cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/邻接表-图/图的邻接表存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/邻接表-图/图的邻接表存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/数组和广义表/特殊矩阵压缩存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/数组和广义表/特殊矩阵压缩存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/数组和广义表/稀疏矩阵压缩存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/数组和广义表/稀疏矩阵压缩存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/基数排序/基数排序.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/基数排序/基数排序.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/基数排序/静态链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/基数排序/静态链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/插入排序/希尔排序.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/插入排序/希尔排序.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/树型查找/B树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/树型查找/B树.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/线性表/顺序表(可以动态扩容).cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/线性表/顺序表(可以动态扩容).cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/线性表/顺序表(无法动态扩容).cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/线性表/顺序表(无法动态扩容).cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/十字链表-图/图的十字链表存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/十字链表-图/图的十字链表存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/邻接矩阵-图/图的邻接矩阵存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/邻接矩阵-图/图的邻接矩阵存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/数组和广义表/广义表头尾链表存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/数组和广义表/广义表头尾链表存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/树型查找/平衡二叉树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/树型查找/平衡二叉树.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-关键路径/CritiStack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-关键路径/CritiStack.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-拓扑排序/TopoStack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-拓扑排序/TopoStack.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/邻接多重表-图/图的邻接多重表存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/邻接多重表-图/图的邻接多重表存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/邻接矩阵-图/MGraphQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/邻接矩阵-图/MGraphQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/邻接表-图/ALGraphQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/邻接表-图/ALGraphQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/数组和广义表/广义表扩展线性链表存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/数组和广义表/广义表扩展线性链表存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/哈希表/HashTable.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/哈希表/HashTable.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/树型查找/字典树/Trie树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/树型查找/字典树/Trie树.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/树型查找/字典树/双链键树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/树型查找/字典树/双链键树.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkBiTree/链式二叉树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkBiTree/链式二叉树.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/SqBiTree/SqBiTree.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/SqBiTree/SqBiTree.jpg -------------------------------------------------------------------------------- /DataStructureSourceCode/树/SqBiTree/二叉树的顺序存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/SqBiTree/二叉树的顺序存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/SqPCTree/树的双亲孩子存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/SqPCTree/树的双亲孩子存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/十字链表-图/OLGraphQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/十字链表-图/OLGraphQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-关键路径/CriticalPath.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-关键路径/CriticalPath.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkPBiTree/二叉树的三叉链表.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkPBiTree/二叉树的三叉链表.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-关键路径/ALGraphFunction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-关键路径/ALGraphFunction.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-拓扑排序/ALGraphFunction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-拓扑排序/ALGraphFunction.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-拓扑排序/TopologicalSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-拓扑排序/TopologicalSort.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/邻接多重表-图/AMLGraphQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/邻接多重表-图/AMLGraphQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/HuffmanTree/HuffmanTree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/HuffmanTree/HuffmanTree.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkBiTree/LinkBiTree.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkBiTree/LinkBiTree.jpg -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkCSBiTree/树的孩子兄弟存储.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkCSBiTree/树的孩子兄弟存储.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkThreadBiTree/线索二叉树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkThreadBiTree/线索二叉树.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/SqBiTree/SqBiTreeQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/SqBiTree/SqBiTreeQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/SqPCTree/SqPCTreeQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/SqPCTree/SqPCTreeQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最小生成树/Prim/MGraphFunction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最小生成树/Prim/MGraphFunction.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最短路径/Floyd/MGraphFunction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最短路径/Floyd/MGraphFunction.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkBiTree/LinkBiTreeQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkBiTree/LinkBiTreeQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkBiTree/LinkBiTreeStack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkBiTree/LinkBiTreeStack.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkPBiTree/LinkPBiTreeQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkPBiTree/LinkPBiTreeQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最小生成树/Kruskal/MGraphFunction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最小生成树/Kruskal/MGraphFunction.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最小生成树/Prim/MiniSpanTree_Prim.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最小生成树/Prim/MiniSpanTree_Prim.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最短路径/Dijkstra/MGraphFunction.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最短路径/Dijkstra/MGraphFunction.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkCSBiTree/LinkCSBiTreeQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkCSBiTree/LinkCSBiTreeQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最短路径/Floyd/ShortestPath_Floyd.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最短路径/Floyd/ShortestPath_Floyd.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/树型查找/字典树/DictionaryTreeStack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/查找与排序/树型查找/字典树/DictionaryTreeStack.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最小生成树/Kruskal/MiniSpanTree_Kruskal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最小生成树/Kruskal/MiniSpanTree_Kruskal.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-最短路径/Dijkstra/ShortestPath_Dijkstra.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/图/图-最短路径/Dijkstra/ShortestPath_Dijkstra.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/树/LinkThreadBiTree/LinkThreadBiTreeQueue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SQL-JQC/DataStructure/HEAD/DataStructureSourceCode/树/LinkThreadBiTree/LinkThreadBiTreeQueue.cpp -------------------------------------------------------------------------------- /DataStructureSourceCode/递归/递归 求阶乘.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | 3 | long f(long i) 4 | { 5 | if(1 == i) 6 | { 7 | return 1; 8 | } 9 | else 10 | { 11 | return f(i-1)*i; 12 | } 13 | } 14 | 15 | int main(void) 16 | { 17 | printf("%ld\n", f(5)); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /DataStructureSourceCode/递归/递归 求和.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | 3 | long sum(long i) 4 | { 5 | if(1 == i) 6 | { 7 | return 1; 8 | } 9 | else 10 | { 11 | return sum(i-1)+i; 12 | } 13 | } 14 | 15 | int main(void) 16 | { 17 | printf("%ld\n", sum(100)); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DataStructure 2 | 使用C语言实现重要数据结构 3 | 数据结构的实现前期参考郝斌数据结构的教程,中期代码主要参考高一凡的数据结构源代码,后期参照严蔚敏《数据结构》中的要点,并全程通过理解后上机编写。 4 | 本数据结构使用C语言实现,需熟悉C语言语法和环境,重点掌握C中指针和内存分配的知识点。 5 | 关于本源代码虽然采用C语言编写,但编写环境为C++环境,文件后缀名均为.cpp,这个不用在意,所有C++环境都对C完全兼容,这样只会有利于我们的编程。 6 | 以上提到的C语言与数据结构均有很好的教程,只需在BiLiBiLi播放器中搜索“郝斌C语言”与“郝斌数据结构”即可,此人讲授水平很高。 7 | -------------------------------------------------------------------------------- /DataStructureSourceCode/树/HuffmanTree/26个英文字母在文献中出现的概率.txt: -------------------------------------------------------------------------------- 1 | E 0.1268 13% 30 2 | T 0.0978 10% 24 3 | A 0.0788 8% 20 4 | O 0.0776 8% 19 5 | I 0.0707 7% 18 6 | N 0.0706 7% 18 7 | S 0.0634 6% 16 8 | R 0.0594 6% 14 9 | H 0.0573 6% 14 10 | L 0.0394 4% 12 11 | D 0.0389 4% 12 12 | U 0.0280 3% 10 13 | C 0.0268 3% 10 14 | F 0.0256 3% 9 15 | M 0.0244 2% 8 16 | W 0.0214 2% 8 17 | Y 0.0202 2% 7 18 | G 0.0187 2% 6 19 | P 0.0186 2% 6 20 | B 0.0156 2% 5 21 | V 0.0102 1% 3 22 | K 0.0060 1% 2 23 | X 0.0016 0% 1 24 | J 0.0010 0% 1 25 | Q 0.0009 0% 1 26 | Z 0.0006 0% 1 -------------------------------------------------------------------------------- /DataStructureSourceCode/图/十字链表-图/OLGraphLinkList.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | int LocateElem(LinkList, ElemType, bool(*compare)(ElemType, ElemType)); 6 | bool ListDelete(LinkList &, int, ElemType &); 7 | 8 | int LocateElem(LinkList L, ElemType e, bool(*compare)(ElemType, ElemType)) 9 | { 10 | int i = 0; 11 | LinkList p = L; 12 | 13 | while(p) 14 | { 15 | i++; 16 | if(compare(p->e, e)) 17 | { 18 | return i; 19 | } 20 | p = p->next; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | bool ListDelete(LinkList & L, int i, ElemType & e) 27 | { 28 | int j = 1; 29 | LinkList p = L; 30 | LinkList q; 31 | 32 | if(i == 1) 33 | { 34 | L = p->next; 35 | e = p->e; 36 | free(p); 37 | } 38 | else 39 | { 40 | while(p->next && j < i - 1) 41 | { 42 | p = p->next; 43 | j++; 44 | } 45 | 46 | if(!p->next || j > i - 1) 47 | { 48 | return false; 49 | } 50 | 51 | q = p->next; 52 | p->next = q->next; 53 | e = q->e; 54 | free(q); 55 | } 56 | 57 | return true; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/插入排序/直接插入排序.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define MAX_SIZE 20 6 | # define N 8 7 | 8 | typedef int KeyType; 9 | typedef int InfoType; 10 | 11 | typedef struct RedType 12 | { 13 | KeyType key; 14 | InfoType otherinfo; 15 | } RedType; 16 | 17 | typedef struct SqList 18 | { 19 | RedType r[MAX_SIZE + 1]; 20 | int length; 21 | } SqList; 22 | 23 | # define EQ(a,b) ((a)==(b)) 24 | # define LT(a,b) ((a)<(b)) 25 | # define LQ(a,b) ((a)<=(b)) 26 | 27 | void InsertSort(SqList &); 28 | 29 | void print(SqList L) 30 | { 31 | int i; 32 | 33 | for(i = 1; i <= L.length; ++i) 34 | { 35 | printf("(%d, %d) ", L.r[i].key, L.r[i].otherinfo); 36 | } 37 | printf("\n"); 38 | } 39 | 40 | int main(void) 41 | { 42 | SqList L; 43 | int i; 44 | RedType r[N] = {{49, 1}, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}, {49, 8}}; 45 | 46 | for(i = 0; i < N; ++i) 47 | { 48 | L.r[i + 1] = r[i]; 49 | } 50 | L.length = N; 51 | 52 | print(L); 53 | InsertSort(L); 54 | print(L); 55 | 56 | return 0; 57 | } 58 | 59 | void InsertSort(SqList & L) 60 | { 61 | int i, j; 62 | 63 | for(i = 2; i <= L.length; ++i) 64 | { 65 | if LT(L.r[i].key,L.r[i-1].key) 66 | { 67 | L.r[0] = L.r[i]; 68 | 69 | for(j = i - 1; LT(L.r[0].key,L.r[j].key); --j) 70 | { 71 | L.r[j + 1] = L.r[j]; 72 | } 73 | 74 | L.r[j + 1] = L.r[0]; 75 | } 76 | } 77 | } 78 | 79 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/选择排序/直接选择排序.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define MAX_SIZE 20 6 | # define N 8 7 | 8 | typedef int KeyType; 9 | typedef int InfoType; 10 | 11 | typedef struct RedType 12 | { 13 | KeyType key; 14 | InfoType otherinfo; 15 | } RedType; 16 | 17 | typedef struct SqList 18 | { 19 | RedType r[MAX_SIZE + 1]; 20 | int length; 21 | } SqList; 22 | 23 | # define EQ(a,b) ((a)==(b)) 24 | # define LT(a,b) ((a)<(b)) 25 | # define LQ(a,b) ((a)<=(b)) 26 | 27 | void SelectSort(SqList &); 28 | 29 | void print(SqList L) 30 | { 31 | int i; 32 | 33 | for(i = 1; i <= L.length; ++i) 34 | { 35 | printf("(%d, %d) ", L.r[i].key, L.r[i].otherinfo); 36 | } 37 | printf("\n"); 38 | } 39 | 40 | int main(void) 41 | { 42 | SqList L; 43 | int i; 44 | RedType r[N] = {{49, 1}, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}, {49, 8}}; 45 | 46 | for(i = 0; i < N; ++i) 47 | { 48 | L.r[i + 1] = r[i]; 49 | } 50 | L.length = N; 51 | 52 | print(L); 53 | SelectSort(L); 54 | print(L); 55 | 56 | return 0; 57 | } 58 | 59 | void SelectSort(SqList & L) 60 | { 61 | int i, j, min; 62 | 63 | for(i = 1; i <= L.length - 1; ++i) 64 | { 65 | for(min = i, j = i + 1; j <= L.length; ++j) 66 | { 67 | if LT(L.r[j].key,L.r[min].key) 68 | { 69 | min = j; 70 | } 71 | } 72 | 73 | if(min != i) 74 | { 75 | L.r[0] = L.r[i]; 76 | L.r[i] = L.r[min]; 77 | L.r[min] = L.r[0]; 78 | } 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/交换排序/冒泡排序.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define MAX_SIZE 20 6 | # define N 8 7 | 8 | typedef int KeyType; 9 | typedef int InfoType; 10 | 11 | typedef struct RedType 12 | { 13 | KeyType key; 14 | InfoType otherinfo; 15 | } RedType; 16 | 17 | typedef struct SqList 18 | { 19 | RedType r[MAX_SIZE + 1]; 20 | int length; 21 | } SqList; 22 | 23 | # define EQ(a,b) ((a)==(b)) 24 | # define LT(a,b) ((a)<(b)) 25 | # define LQ(a,b) ((a)<=(b)) 26 | 27 | void BubbleSort(SqList &); 28 | 29 | void print(SqList L) 30 | { 31 | int i; 32 | 33 | for(i = 1; i <= L.length; ++i) 34 | { 35 | printf("(%d, %d) ", L.r[i].key, L.r[i].otherinfo); 36 | } 37 | printf("\n"); 38 | } 39 | 40 | int main(void) 41 | { 42 | SqList L; 43 | int i; 44 | RedType r[N] = {{49, 1}, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}, {49, 8}}; 45 | 46 | for(i = 0; i < N; ++i) 47 | { 48 | L.r[i + 1] = r[i]; 49 | } 50 | L.length = N; 51 | 52 | print(L); 53 | BubbleSort(L); 54 | print(L); 55 | 56 | return 0; 57 | } 58 | 59 | void BubbleSort(SqList & L) 60 | { 61 | int i, j; 62 | bool change; 63 | 64 | for(i = 1, change = true; i <= L.length - 1 && change; ++i) 65 | { 66 | change = false; 67 | 68 | for(j = 1; j <= L.length - i; ++j) 69 | { 70 | if LT(L.r[j+1].key,L.r[j].key) 71 | { 72 | L.r[0] = L.r[j]; 73 | L.r[j] = L.r[j + 1]; 74 | L.r[j + 1] = L.r[0]; 75 | change = true; 76 | } 77 | } 78 | } 79 | } 80 | 81 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/插入排序/折半插入排序.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define MAX_SIZE 20 6 | # define N 8 7 | 8 | typedef int KeyType; 9 | typedef int InfoType; 10 | 11 | typedef struct RedType 12 | { 13 | KeyType key; 14 | InfoType otherinfo; 15 | } RedType; 16 | 17 | typedef struct SqList 18 | { 19 | RedType r[MAX_SIZE + 1]; 20 | int length; 21 | } SqList; 22 | 23 | # define EQ(a,b) ((a)==(b)) 24 | # define LT(a,b) ((a)<(b)) 25 | # define LQ(a,b) ((a)<=(b)) 26 | 27 | void BInsertSort(SqList &); 28 | 29 | void print(SqList L) 30 | { 31 | int i; 32 | 33 | for(i = 1; i <= L.length; ++i) 34 | { 35 | printf("(%d, %d) ", L.r[i].key, L.r[i].otherinfo); 36 | } 37 | printf("\n"); 38 | } 39 | 40 | int main(void) 41 | { 42 | SqList L; 43 | int i; 44 | RedType r[N] = {{49, 1}, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}, {49, 8}}; 45 | 46 | for(i = 0; i < N; ++i) 47 | { 48 | L.r[i + 1] = r[i]; 49 | } 50 | L.length = N; 51 | 52 | print(L); 53 | BInsertSort(L); 54 | print(L); 55 | 56 | return 0; 57 | } 58 | 59 | void BInsertSort(SqList & L) 60 | { 61 | int i, j; 62 | int low, high, mid; 63 | 64 | for(i = 2; i <= L.length; ++i) 65 | { 66 | L.r[0] = L.r[i]; 67 | 68 | low = 1; 69 | high = i - 1; 70 | 71 | while(low <= high) 72 | { 73 | mid = (low + high) / 2; 74 | if LT(L.r[0].key,L.r[mid].key) 75 | { 76 | high = mid - 1; 77 | } 78 | else 79 | { 80 | low = mid + 1; 81 | } 82 | } 83 | 84 | for(j = i - 1; j >= high + 1; --j) 85 | { 86 | L.r[j + 1] = L.r[j]; 87 | } 88 | 89 | L.r[high + 1] = L.r[0]; 90 | } 91 | } 92 | 93 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/选择排序/堆排序.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define MAX_SIZE 20 6 | # define N 8 7 | 8 | typedef int KeyType; 9 | typedef int InfoType; 10 | 11 | typedef struct RedType 12 | { 13 | KeyType key; 14 | InfoType otherinfo; 15 | } RedType; 16 | 17 | typedef struct SqList 18 | { 19 | RedType r[MAX_SIZE + 1]; 20 | int length; 21 | } SqList; 22 | 23 | typedef SqList HeapType; 24 | 25 | # define EQ(a,b) ((a)==(b)) 26 | # define LT(a,b) ((a)<(b)) 27 | # define LQ(a,b) ((a)<=(b)) 28 | 29 | void HeapAdjust(HeapType &, int, int); 30 | void HeapSort(HeapType &); 31 | 32 | void print(SqList L) 33 | { 34 | int i; 35 | 36 | for(i = 1; i <= L.length; ++i) 37 | { 38 | printf("(%d, %d) ", L.r[i].key, L.r[i].otherinfo); 39 | } 40 | printf("\n"); 41 | } 42 | 43 | int main(void) 44 | { 45 | HeapType H; 46 | int i; 47 | RedType r[N] = {{49, 1}, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}, {49, 8}}; 48 | 49 | for(i = 0; i < N; ++i) 50 | { 51 | H.r[i + 1] = r[i]; 52 | } 53 | H.length = N; 54 | 55 | print(H); 56 | HeapSort(H); 57 | print(H); 58 | 59 | return 0; 60 | } 61 | 62 | void HeapAdjust(HeapType & H, int s, int m) 63 | { 64 | RedType rc; 65 | int j; 66 | 67 | rc = H.r[s]; 68 | 69 | for(j = s * 2; j <= m; j *= 2) 70 | { 71 | if(j < m && LT(H.r[j].key,H.r[j+1].key)) 72 | { 73 | ++j; 74 | } 75 | 76 | if(!LT(rc.key,H.r[j].key)) 77 | { 78 | break; 79 | } 80 | 81 | H.r[s] = H.r[j]; 82 | 83 | s = j; 84 | } 85 | 86 | H.r[s] = rc; 87 | } 88 | 89 | void HeapSort(HeapType & H) 90 | { 91 | RedType t; 92 | int i; 93 | 94 | for(i = H.length / 2; i > 0; --i) 95 | { 96 | HeapAdjust(H, i, H.length); 97 | } 98 | 99 | for(i = H.length; i > 1; --i) 100 | { 101 | t = H.r[1]; 102 | H.r[1] = H.r[i]; 103 | H.r[i] = t; 104 | 105 | HeapAdjust(H, 1, i - 1); 106 | } 107 | } 108 | 109 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/交换排序/快速排序.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define MAX_SIZE 20 6 | # define N 8 7 | 8 | typedef int KeyType; 9 | typedef int InfoType; 10 | 11 | typedef struct RedType 12 | { 13 | KeyType key; 14 | InfoType otherinfo; 15 | } RedType; 16 | 17 | typedef struct SqList 18 | { 19 | RedType r[MAX_SIZE + 1]; 20 | int length; 21 | } SqList; 22 | 23 | # define EQ(a,b) ((a)==(b)) 24 | # define LT(a,b) ((a)<(b)) 25 | # define LQ(a,b) ((a)<=(b)) 26 | 27 | int Partition(SqList &, int, int); 28 | void QSort(SqList &, int, int); 29 | void QuickSort(SqList &); 30 | 31 | void print(SqList L) 32 | { 33 | int i; 34 | 35 | for(i = 1; i <= L.length; ++i) 36 | { 37 | printf("(%d, %d) ", L.r[i].key, L.r[i].otherinfo); 38 | } 39 | printf("\n"); 40 | } 41 | 42 | int main(void) 43 | { 44 | SqList L; 45 | int i; 46 | RedType r[N] = {{49, 1}, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}, {49, 8}}; 47 | 48 | for(i = 0; i < N; ++i) 49 | { 50 | L.r[i + 1] = r[i]; 51 | } 52 | L.length = N; 53 | 54 | print(L); 55 | QuickSort(L); 56 | print(L); 57 | 58 | return 0; 59 | } 60 | 61 | int Partition(SqList & L, int low, int high) 62 | { 63 | KeyType pivotkey; 64 | 65 | L.r[0] = L.r[low]; 66 | pivotkey = L.r[low].key; 67 | 68 | while(low < high) 69 | { 70 | while(low < high && L.r[high].key >= pivotkey) 71 | { 72 | --high; 73 | } 74 | L.r[low] = L.r[high]; 75 | 76 | while(low < high && L.r[low].key <= pivotkey) 77 | { 78 | ++low; 79 | } 80 | L.r[high] = L.r[low]; 81 | } 82 | 83 | L.r[low] = L.r[0]; 84 | 85 | return low; 86 | } 87 | 88 | void QSort(SqList & L, int low, int high) 89 | { 90 | int pos; 91 | 92 | if(low < high) 93 | { 94 | pos = Partition(L, low, high); 95 | QSort(L, low, pos-1); 96 | QSort(L, pos+1, high); 97 | } 98 | } 99 | 100 | void QuickSort(SqList & L) 101 | { 102 | QSort(L, 1, L.length); 103 | } 104 | 105 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/归并排序/归并排序.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define MAX_SIZE 20 6 | # define N 7 7 | 8 | typedef int KeyType; 9 | typedef int InfoType; 10 | 11 | typedef struct RedType 12 | { 13 | KeyType key; 14 | InfoType otherinfo; 15 | } RedType; 16 | 17 | typedef struct SqList 18 | { 19 | RedType r[MAX_SIZE + 1]; 20 | int length; 21 | } SqList; 22 | 23 | # define EQ(a,b) ((a)==(b)) 24 | # define LT(a,b) ((a)<(b)) 25 | # define LQ(a,b) ((a)<=(b)) 26 | 27 | void Merge(RedType SR[], RedType TR[], int, int, int); 28 | void MSort(RedType SR[], RedType TR1[], int, int); 29 | void MergeSort(SqList &); 30 | 31 | void print(SqList L) 32 | { 33 | int i; 34 | 35 | for(i = 1; i <= L.length; ++i) 36 | { 37 | printf("(%d, %d) ", L.r[i].key, L.r[i].otherinfo); 38 | } 39 | printf("\n"); 40 | } 41 | 42 | int main(void) 43 | { 44 | SqList L; 45 | int i; 46 | RedType r[N] = {{49, 1}, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}}; 47 | 48 | for(i = 0; i < N; ++i) 49 | { 50 | L.r[i + 1] = r[i]; 51 | } 52 | L.length = N; 53 | 54 | print(L); 55 | MergeSort(L); 56 | print(L); 57 | 58 | return 0; 59 | } 60 | 61 | void Merge(RedType SR[], RedType TR[], int i, int m, int n) 62 | { 63 | int j, k, l; 64 | 65 | for(j = m + 1, k = i; i <= m && j <= n; ++k) 66 | { 67 | if LQ(SR[i].key,SR[j].key) 68 | { 69 | TR[k] = SR[i++]; 70 | } 71 | else 72 | { 73 | TR[k] = SR[j++]; 74 | } 75 | } 76 | 77 | if(i <= m) 78 | { 79 | for(l = 0; l <= m - i; ++l) 80 | { 81 | TR[k + l] = SR[i + l]; 82 | } 83 | } 84 | if(j <= n) 85 | { 86 | for(l = 0; l <= n - j; ++l) 87 | { 88 | TR[k + l] = SR[j + l]; 89 | } 90 | } 91 | } 92 | 93 | void MSort(RedType SR[], RedType TR1[], int s, int t) 94 | { 95 | int m; 96 | RedType TR2[MAX_SIZE + 1]; 97 | 98 | if(s == t) 99 | { 100 | TR1[s] = SR[s]; 101 | } 102 | else 103 | { 104 | m = (s + t) / 2; 105 | 106 | MSort(SR, TR2, s, m); 107 | MSort(SR, TR2, m+1, t); 108 | Merge(TR2, TR1, s, m, t); 109 | } 110 | } 111 | 112 | void MergeSort(SqList & L) 113 | { 114 | MSort(L.r, L.r, 1, L.length); 115 | } 116 | 117 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/线型查找/顺序查找.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define N 11 6 | 7 | typedef int KeyType; 8 | 9 | typedef struct ElemType 10 | { 11 | KeyType key; 12 | } ElemType; 13 | 14 | typedef struct SSTable 15 | { 16 | ElemType * elem; 17 | int length; 18 | } SSTable; 19 | 20 | # define EQ(a,b) ((a)==(b)) 21 | # define LT(a,b) ((a)<(b)) 22 | # define LQ(a,b) ((a)<=(b)) 23 | 24 | void Create_Seq(SSTable &, ElemType e[], int); 25 | void Ascend(SSTable &); 26 | void Create_Ord(SSTable &, ElemType e[], int); 27 | bool Destroy(SSTable &); 28 | int Search_Seq(SSTable, KeyType); 29 | int Search_Bin(SSTable, KeyType); 30 | void Traverse(SSTable, void(*Visit)(ElemType)); 31 | 32 | void print(ElemType e) 33 | { 34 | printf("%d ", e.key); 35 | } 36 | 37 | int main(void) 38 | { 39 | SSTable ST; 40 | ElemType e[N] = {3, 2, 1, 8, 6, 4, 5, 7, 11, 10, 9}; 41 | int pos; 42 | 43 | // Create_Seq(ST, e, N); 44 | // Traverse(ST, print); 45 | // pos = Search_Seq(ST, 4); 46 | // printf("\n%d\n", pos); 47 | 48 | // Create_Ord(ST, e, N); 49 | // Traverse(ST, print); 50 | // pos = Search_Seq(ST, 4); 51 | // printf("\n%d\n", pos); 52 | 53 | // Create_Ord(ST, e, N); 54 | // Traverse(ST, print); 55 | // pos = Search_Bin(ST, 4); 56 | // printf("\n%d\n", pos); 57 | 58 | Destroy(ST); 59 | 60 | return 0; 61 | } 62 | 63 | void Create_Seq(SSTable & ST, ElemType e[], int n) 64 | { 65 | int i; 66 | 67 | ST.elem = (ElemType *)calloc(n + 1, sizeof(ElemType)); 68 | if(!ST.elem) 69 | { 70 | exit(-1); 71 | } 72 | 73 | for(i = 1; i <= n; ++i) 74 | { 75 | ST.elem[i] = e[i - 1]; 76 | } 77 | 78 | ST.length = n; 79 | } 80 | 81 | void Ascend(SSTable & ST) 82 | { 83 | int i, j, k; 84 | 85 | for(i = 1; i < ST.length; ++i) 86 | { 87 | k = i; 88 | 89 | ST.elem[0] = ST.elem[i]; 90 | 91 | for(j = i + 1; j <= ST.length; ++j) 92 | { 93 | if LT(ST.elem[j].key,ST.elem[0].key) 94 | { 95 | k = j; 96 | ST.elem[0] = ST.elem[j]; 97 | } 98 | } 99 | 100 | if(k != i) 101 | { 102 | ST.elem[k] = ST.elem[i]; 103 | ST.elem[i] = ST.elem[0]; 104 | } 105 | } 106 | } 107 | 108 | void Create_Ord(SSTable & ST, ElemType e[], int n) 109 | { 110 | Create_Seq(ST, e, n); 111 | Ascend(ST); 112 | } 113 | 114 | bool Destroy(SSTable & ST) 115 | { 116 | free(ST.elem); 117 | ST.elem = NULL; 118 | ST.length = 0; 119 | 120 | return true; 121 | } 122 | 123 | int Search_Seq(SSTable ST, KeyType key) 124 | { 125 | int i; 126 | 127 | ST.elem[0].key = key; 128 | 129 | for(i = ST.length; !EQ(ST.elem[i].key,key); --i); 130 | 131 | return i; 132 | } 133 | 134 | int Search_Bin(SSTable ST, KeyType key) 135 | { 136 | int low, high, mid; 137 | 138 | low = 1; 139 | high = ST.length; 140 | 141 | while(low <= high) 142 | { 143 | mid = (low + high) / 2; 144 | 145 | if EQ(key,ST.elem[mid].key) 146 | { 147 | return mid; 148 | } 149 | else if LT(key,ST.elem[mid].key) 150 | { 151 | high = mid - 1; 152 | } 153 | else 154 | { 155 | low = mid + 1; 156 | } 157 | } 158 | 159 | return 0; 160 | } 161 | 162 | void Traverse(SSTable ST, void(*Visit)(ElemType)) 163 | { 164 | ElemType * p; 165 | int i; 166 | 167 | p = ++ST.elem; 168 | 169 | for(i = 1; i <= ST.length; ++i) 170 | { 171 | Visit(*p++); 172 | } 173 | } 174 | 175 | -------------------------------------------------------------------------------- /DataStructureSourceCode/数组和广义表/n维数组.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | # include 5 | 6 | # define MAX_ARRAY_DIM 8 7 | # define ElementType int 8 | 9 | typedef struct Array 10 | { 11 | ElementType * base; 12 | int dim; 13 | int * bounds; 14 | int * constants; 15 | } Array; 16 | 17 | void InitArray(Array *, int, ...); 18 | void DestroyArray(Array *); 19 | void Value(Array *, int *, ...); 20 | void Assign(Array *, int, ...); 21 | bool Locate(Array *, va_list, int *); 22 | 23 | int main(void) 24 | { 25 | Array array; 26 | InitArray(&array, 3, 3, 4, 5); 27 | 28 | int i, j, k; 29 | for(i = 0; i < 3; ++i) 30 | { 31 | for(j = 0; j < 4; ++j) 32 | { 33 | for(k = 0; k < 5; ++k) 34 | { 35 | Assign(&array, i + j + k, i, j, k); 36 | } 37 | } 38 | } 39 | 40 | int l, m, n; 41 | for(l = 0; l < 3; ++l) 42 | { 43 | for(m = 0; m < 4; ++m) 44 | { 45 | for(n = 0; n < 5; ++n) 46 | { 47 | int tmp = 1; 48 | Value(&array, &tmp, l, m, n); 49 | printf("%5d", tmp); 50 | } 51 | printf("\n"); 52 | } 53 | printf("\n"); 54 | } 55 | printf("\n"); 56 | 57 | DestroyArray(&array); 58 | 59 | return 0; 60 | } 61 | 62 | void InitArray(Array * array, int dim, ...) 63 | { 64 | int i, j; 65 | 66 | if(dim < 1 || dim > MAX_ARRAY_DIM) 67 | { 68 | return; 69 | } 70 | 71 | array->dim = dim; 72 | array->bounds = (int *)malloc(sizeof(int) * dim); 73 | 74 | int elemtotal = 1; 75 | va_list ap; 76 | va_start(ap, dim); 77 | for(i = 0; i < dim; ++i) 78 | { 79 | array->bounds[i] = va_arg(ap, int); 80 | elemtotal *= array->bounds[i]; 81 | } 82 | va_end(ap); 83 | 84 | array->base = (int *)malloc(sizeof(int) * elemtotal); 85 | array->constants = (int *)malloc(sizeof(int) * dim); 86 | 87 | array->constants[dim - 1] = 1; 88 | for(j = dim - 2; j >= 0; --j) 89 | { 90 | array->constants[j] = array->bounds[j + 1] * array->constants[j + 1]; 91 | } 92 | 93 | return; 94 | } 95 | 96 | void DestroyArray(Array * array) 97 | { 98 | if(array->base == NULL) 99 | { 100 | return; 101 | } 102 | free(array->base); 103 | array->base = NULL; 104 | 105 | if(array->bounds == NULL) 106 | { 107 | return; 108 | } 109 | free(array->bounds); 110 | array->bounds = NULL; 111 | 112 | if(array->constants == NULL) 113 | { 114 | return; 115 | } 116 | free(array->constants); 117 | array->constants = NULL; 118 | 119 | return; 120 | } 121 | 122 | bool Locate(Array * array, va_list ap, int * off) 123 | { 124 | int i; 125 | 126 | *off = 0; 127 | for(i = 0; i < array->dim; ++i) 128 | { 129 | int index = va_arg(ap, int); 130 | if(index < 0 || index > (array->bounds[i] - 1)) 131 | { 132 | return false; 133 | } 134 | (*off) += array->constants[i] * index; 135 | } 136 | 137 | return true; 138 | } 139 | 140 | void Value(Array * array, int * e, ...) 141 | { 142 | va_list ap; 143 | va_start(ap, e); 144 | 145 | int off; 146 | if(!Locate(array, ap, &off)) 147 | { 148 | return; 149 | } 150 | *e = *(array->base + off); 151 | va_end(ap); 152 | 153 | return; 154 | } 155 | 156 | void Assign(Array * array, int e, ...) 157 | { 158 | va_list ap; 159 | va_start(ap, e); 160 | 161 | int off; 162 | if(!Locate(array, ap, &off)) 163 | { 164 | return; 165 | } 166 | *(array->base + off) = e; 167 | va_end(ap); 168 | 169 | return; 170 | } 171 | 172 | -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-关键路径/ALGraphLinkList.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | void InitList(LinkList &); 6 | void DestroyList(LinkList &); 7 | bool ListEmpty(LinkList); 8 | int ListLength(LinkList); 9 | int LocateElem(LinkList, ElemType, bool(*compare)(ElemType, ElemType)); 10 | bool ListInsert(LinkList &, int, ElemType); 11 | bool ListDelete(LinkList &, int, ElemType &); 12 | LinkList Point(LinkList, ElemType, bool(*equal)(ElemType, ElemType), LinkList &); 13 | bool DeleteElem(LinkList &, ElemType &, bool(*equal)(ElemType, ElemType)); 14 | 15 | void InitList(LinkList & L) 16 | { 17 | L = NULL; 18 | } 19 | 20 | void DestroyList(LinkList & L) 21 | { 22 | LinkList p; 23 | 24 | while(L) 25 | { 26 | p = L; 27 | L = L->next; 28 | free(p); 29 | } 30 | } 31 | 32 | bool ListEmpty(LinkList L) 33 | { 34 | if(L) 35 | { 36 | return false; 37 | } 38 | else 39 | { 40 | return true; 41 | } 42 | } 43 | 44 | int ListLength(LinkList L) 45 | { 46 | int i = 0; 47 | LinkList p = L; 48 | 49 | while(p) 50 | { 51 | i++; 52 | p = p->next; 53 | } 54 | 55 | return i; 56 | } 57 | 58 | int LocateElem(LinkList L, ElemType e, bool(*compare)(ElemType, ElemType)) 59 | { 60 | int i = 0; 61 | LinkList p = L; 62 | 63 | while(p) 64 | { 65 | i++; 66 | if(compare(p->e, e)) 67 | { 68 | return i; 69 | } 70 | p = p->next; 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | bool ListInsert(LinkList & L, int i, ElemType e) 77 | { 78 | int j = 1; 79 | LinkList p = L; 80 | LinkList q; 81 | 82 | if(i < 1) 83 | { 84 | return false; 85 | } 86 | 87 | q = (LinkList)malloc(sizeof(LNode)); 88 | q->e = e; 89 | 90 | if(i == 1) 91 | { 92 | q->next = L; 93 | L = q; 94 | } 95 | else 96 | { 97 | while(p && j < i - 1) 98 | { 99 | p = p->next; 100 | j++; 101 | } 102 | 103 | if(!p) 104 | { 105 | return false; 106 | } 107 | 108 | q->next = p->next; 109 | p->next = q; 110 | } 111 | 112 | return true; 113 | } 114 | 115 | bool ListDelete(LinkList & L, int i, ElemType & e) 116 | { 117 | int j = 1; 118 | LinkList p = L; 119 | LinkList q; 120 | 121 | if(i == 1) 122 | { 123 | L = p->next; 124 | e = p->e; 125 | free(p); 126 | } 127 | else 128 | { 129 | while(p->next && j < i - 1) 130 | { 131 | p = p->next; 132 | j++; 133 | } 134 | 135 | if(!p->next || j > i - 1) 136 | { 137 | return false; 138 | } 139 | 140 | q = p->next; 141 | p->next = q->next; 142 | e = q->e; 143 | free(q); 144 | } 145 | 146 | return true; 147 | } 148 | 149 | LinkList Point(LinkList L, ElemType e, bool(*equal)(ElemType, ElemType), LinkList & p) 150 | { 151 | int i, j; 152 | 153 | i = LocateElem(L, e, equal); 154 | 155 | if(i) 156 | { 157 | if(i == 1) 158 | { 159 | p = NULL; 160 | return L; 161 | } 162 | else 163 | { 164 | p = L; 165 | for(j = 2; j < i; ++j) 166 | { 167 | p = p->next; 168 | } 169 | return p->next; 170 | } 171 | } 172 | 173 | return NULL; 174 | } 175 | 176 | bool DeleteElem(LinkList & L, ElemType & e, bool(*equal)(ElemType, ElemType)) 177 | { 178 | LinkList p, q; 179 | 180 | q = Point(L, e, equal, p); 181 | 182 | if(q) 183 | { 184 | if(p) 185 | { 186 | ListDelete(p, 2, e); 187 | } 188 | else 189 | { 190 | ListDelete(L, 1, e); 191 | } 192 | 193 | return true; 194 | } 195 | 196 | return false; 197 | } 198 | 199 | -------------------------------------------------------------------------------- /DataStructureSourceCode/图/图-拓扑排序/ALGraphLinkList.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | void InitList(LinkList &); 6 | void DestroyList(LinkList &); 7 | bool ListEmpty(LinkList); 8 | int ListLength(LinkList); 9 | int LocateElem(LinkList, ElemType, bool(*compare)(ElemType, ElemType)); 10 | bool ListInsert(LinkList &, int, ElemType); 11 | bool ListDelete(LinkList &, int, ElemType &); 12 | LinkList Point(LinkList, ElemType, bool(*equal)(ElemType, ElemType), LinkList &); 13 | bool DeleteElem(LinkList &, ElemType &, bool(*equal)(ElemType, ElemType)); 14 | 15 | void InitList(LinkList & L) 16 | { 17 | L = NULL; 18 | } 19 | 20 | void DestroyList(LinkList & L) 21 | { 22 | LinkList p; 23 | 24 | while(L) 25 | { 26 | p = L; 27 | L = L->next; 28 | free(p); 29 | } 30 | } 31 | 32 | bool ListEmpty(LinkList L) 33 | { 34 | if(L) 35 | { 36 | return false; 37 | } 38 | else 39 | { 40 | return true; 41 | } 42 | } 43 | 44 | int ListLength(LinkList L) 45 | { 46 | int i = 0; 47 | LinkList p = L; 48 | 49 | while(p) 50 | { 51 | i++; 52 | p = p->next; 53 | } 54 | 55 | return i; 56 | } 57 | 58 | int LocateElem(LinkList L, ElemType e, bool(*compare)(ElemType, ElemType)) 59 | { 60 | int i = 0; 61 | LinkList p = L; 62 | 63 | while(p) 64 | { 65 | i++; 66 | if(compare(p->e, e)) 67 | { 68 | return i; 69 | } 70 | p = p->next; 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | bool ListInsert(LinkList & L, int i, ElemType e) 77 | { 78 | int j = 1; 79 | LinkList p = L; 80 | LinkList q; 81 | 82 | if(i < 1) 83 | { 84 | return false; 85 | } 86 | 87 | q = (LinkList)malloc(sizeof(LNode)); 88 | q->e = e; 89 | 90 | if(i == 1) 91 | { 92 | q->next = L; 93 | L = q; 94 | } 95 | else 96 | { 97 | while(p && j < i - 1) 98 | { 99 | p = p->next; 100 | j++; 101 | } 102 | 103 | if(!p) 104 | { 105 | return false; 106 | } 107 | 108 | q->next = p->next; 109 | p->next = q; 110 | } 111 | 112 | return true; 113 | } 114 | 115 | bool ListDelete(LinkList & L, int i, ElemType & e) 116 | { 117 | int j = 1; 118 | LinkList p = L; 119 | LinkList q; 120 | 121 | if(i == 1) 122 | { 123 | L = p->next; 124 | e = p->e; 125 | free(p); 126 | } 127 | else 128 | { 129 | while(p->next && j < i - 1) 130 | { 131 | p = p->next; 132 | j++; 133 | } 134 | 135 | if(!p->next || j > i - 1) 136 | { 137 | return false; 138 | } 139 | 140 | q = p->next; 141 | p->next = q->next; 142 | e = q->e; 143 | free(q); 144 | } 145 | 146 | return true; 147 | } 148 | 149 | LinkList Point(LinkList L, ElemType e, bool(*equal)(ElemType, ElemType), LinkList & p) 150 | { 151 | int i, j; 152 | 153 | i = LocateElem(L, e, equal); 154 | 155 | if(i) 156 | { 157 | if(i == 1) 158 | { 159 | p = NULL; 160 | return L; 161 | } 162 | else 163 | { 164 | p = L; 165 | for(j = 2; j < i; ++j) 166 | { 167 | p = p->next; 168 | } 169 | return p->next; 170 | } 171 | } 172 | 173 | return NULL; 174 | } 175 | 176 | bool DeleteElem(LinkList & L, ElemType & e, bool(*equal)(ElemType, ElemType)) 177 | { 178 | LinkList p, q; 179 | 180 | q = Point(L, e, equal, p); 181 | 182 | if(q) 183 | { 184 | if(p) 185 | { 186 | ListDelete(p, 2, e); 187 | } 188 | else 189 | { 190 | ListDelete(L, 1, e); 191 | } 192 | 193 | return true; 194 | } 195 | 196 | return false; 197 | } 198 | 199 | -------------------------------------------------------------------------------- /DataStructureSourceCode/图/邻接表-图/ALGraphLinkList.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | void InitList(LinkList &); 6 | void DestroyList(LinkList &); 7 | bool ListEmpty(LinkList); 8 | int ListLength(LinkList); 9 | int LocateElem(LinkList, ElemType, bool(*compare)(ElemType, ElemType)); 10 | bool ListInsert(LinkList &, int, ElemType); 11 | bool ListDelete(LinkList &, int, ElemType &); 12 | LinkList Point(LinkList, ElemType, bool(*equal)(ElemType, ElemType), LinkList &); 13 | bool DeleteElem(LinkList &, ElemType &, bool(*equal)(ElemType, ElemType)); 14 | 15 | void InitList(LinkList & L) 16 | { 17 | L = NULL; 18 | } 19 | 20 | void DestroyList(LinkList & L) 21 | { 22 | LinkList p; 23 | 24 | while(L) 25 | { 26 | p = L; 27 | L = L->next; 28 | free(p); 29 | } 30 | } 31 | 32 | bool ListEmpty(LinkList L) 33 | { 34 | if(L) 35 | { 36 | return false; 37 | } 38 | else 39 | { 40 | return true; 41 | } 42 | } 43 | 44 | int ListLength(LinkList L) 45 | { 46 | int i = 0; 47 | LinkList p = L; 48 | 49 | while(p) 50 | { 51 | i++; 52 | p = p->next; 53 | } 54 | 55 | return i; 56 | } 57 | 58 | int LocateElem(LinkList L, ElemType e, bool(*compare)(ElemType, ElemType)) 59 | { 60 | int i = 0; 61 | LinkList p = L; 62 | 63 | while(p) 64 | { 65 | i++; 66 | if(compare(p->e, e)) 67 | { 68 | return i; 69 | } 70 | p = p->next; 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | bool ListInsert(LinkList & L, int i, ElemType e) 77 | { 78 | int j = 1; 79 | LinkList p = L; 80 | LinkList q; 81 | 82 | if(i < 1) 83 | { 84 | return false; 85 | } 86 | 87 | q = (LinkList)malloc(sizeof(LNode)); 88 | q->e = e; 89 | 90 | if(i == 1) 91 | { 92 | q->next = L; 93 | L = q; 94 | } 95 | else 96 | { 97 | while(p && j < i - 1) 98 | { 99 | p = p->next; 100 | j++; 101 | } 102 | 103 | if(!p) 104 | { 105 | return false; 106 | } 107 | 108 | q->next = p->next; 109 | p->next = q; 110 | } 111 | 112 | return true; 113 | } 114 | 115 | bool ListDelete(LinkList & L, int i, ElemType & e) 116 | { 117 | int j = 1; 118 | LinkList p = L; 119 | LinkList q; 120 | 121 | if(i == 1) 122 | { 123 | L = p->next; 124 | e = p->e; 125 | free(p); 126 | } 127 | else 128 | { 129 | while(p->next && j < i - 1) 130 | { 131 | p = p->next; 132 | j++; 133 | } 134 | 135 | if(!p->next || j > i - 1) 136 | { 137 | return false; 138 | } 139 | 140 | q = p->next; 141 | p->next = q->next; 142 | e = q->e; 143 | free(q); 144 | } 145 | 146 | return true; 147 | } 148 | 149 | LinkList Point(LinkList L, ElemType e, bool(*equal)(ElemType, ElemType), LinkList & p) 150 | { 151 | int i, j; 152 | 153 | i = LocateElem(L, e, equal); 154 | 155 | if(i) 156 | { 157 | if(i == 1) 158 | { 159 | p = NULL; 160 | return L; 161 | } 162 | else 163 | { 164 | p = L; 165 | for(j = 2; j < i; ++j) 166 | { 167 | p = p->next; 168 | } 169 | return p->next; 170 | } 171 | } 172 | 173 | return NULL; 174 | } 175 | 176 | bool DeleteElem(LinkList & L, ElemType & e, bool(*equal)(ElemType, ElemType)) 177 | { 178 | LinkList p, q; 179 | 180 | q = Point(L, e, equal, p); 181 | 182 | if(q) 183 | { 184 | if(p) 185 | { 186 | ListDelete(p, 2, e); 187 | } 188 | else 189 | { 190 | ListDelete(L, 1, e); 191 | } 192 | 193 | return true; 194 | } 195 | 196 | return false; 197 | } 198 | 199 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/树型查找/二叉排序树.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define N 10 6 | 7 | typedef int KeyType; 8 | 9 | typedef struct ElemType 10 | { 11 | KeyType key; 12 | int others; 13 | } ElemType; 14 | 15 | typedef ElemType TElemType; 16 | 17 | typedef struct BiTNode 18 | { 19 | TElemType data; 20 | struct BiTNode * lchild; 21 | struct BiTNode * rchild; 22 | } BiTNode, * BiTree; 23 | 24 | # define EQ(a,b) ((a)==(b)) 25 | # define LT(a,b) ((a)<(b)) 26 | # define LQ(a,b) ((a)<=(b)) 27 | 28 | void InitBST(BiTree &); 29 | BiTree SearchBST(BiTree, KeyType); 30 | bool SearchBST(BiTree &, KeyType, BiTree, BiTree &); 31 | bool InsertBST(BiTree &, ElemType); 32 | void Delete(BiTree &); 33 | bool DeleteBST(BiTree &, KeyType); 34 | void DestroyBST(BiTree &); 35 | void TraverseBST(BiTree, void(*Visit)(ElemType)); 36 | void PreOrderTraverse(BiTree, void(*Visit)(ElemType)); 37 | 38 | void print(ElemType e) 39 | { 40 | printf("(%d,%d) ", e.key, e.others); 41 | } 42 | 43 | int main(void) 44 | { 45 | BiTree T; 46 | BiTree p; 47 | ElemType e[N] = {{3, 1}, {2, 2}, {1, 3}, {7, 4}, {5, 5}, {10, 6}, {4, 7}, {9, 8}, {8, 9}, {6, 10}}; 48 | int i; 49 | 50 | // InitBST(T); 51 | // for(i = 0; i < N; ++i) 52 | // { 53 | // InsertBST(T, e[i]); 54 | // } 55 | // TraverseBST(T, print); 56 | // printf("\n"); 57 | // PreOrderTraverse(T, print); 58 | 59 | // p = SearchBST(T, 4); 60 | // printf("\n(%d, %d)\n", p->data.key, p->data.others); 61 | 62 | // DeleteBST(T, 4); 63 | // TraverseBST(T, print); 64 | 65 | DestroyBST(T); 66 | 67 | return 0; 68 | } 69 | 70 | void InitBST(BiTree & T) 71 | { 72 | T = NULL; 73 | } 74 | 75 | BiTree SearchBST(BiTree T, KeyType key) 76 | { 77 | if(!T || EQ(key,T->data.key)) 78 | { 79 | return T; 80 | } 81 | else if LT(key,T->data.key) 82 | { 83 | return SearchBST(T->lchild, key); 84 | } 85 | else 86 | { 87 | return SearchBST(T->rchild, key); 88 | } 89 | } 90 | 91 | bool SearchBST(BiTree & T, KeyType key, BiTree f, BiTree & p) 92 | { 93 | if(!T) 94 | { 95 | p = f; 96 | return false; 97 | } 98 | else if EQ(key,T->data.key) 99 | { 100 | p = T; 101 | return true; 102 | } 103 | else if LT(key,T->data.key) 104 | { 105 | return SearchBST(T->lchild, key, T, p); 106 | } 107 | else 108 | { 109 | return SearchBST(T->rchild, key, T, p); 110 | } 111 | } 112 | 113 | bool InsertBST(BiTree & T, ElemType e) 114 | { 115 | BiTree p, s; 116 | 117 | if(!SearchBST(T, e.key, NULL, p)) 118 | { 119 | s = (BiTree)malloc(sizeof(BiTNode)); 120 | s->data = e; 121 | s->lchild = NULL; 122 | s->rchild = NULL; 123 | 124 | if(!p) 125 | { 126 | T = s; 127 | } 128 | else if LT(e.key,p->data.key) 129 | { 130 | p->lchild = s; 131 | } 132 | else 133 | { 134 | p->rchild = s; 135 | } 136 | 137 | return true; 138 | } 139 | else 140 | { 141 | return false; 142 | } 143 | } 144 | 145 | void Delete(BiTree & p) 146 | { 147 | BiTree q, s; 148 | 149 | if(!p->rchild) 150 | { 151 | q = p; 152 | p = p->lchild; 153 | free(q); 154 | } 155 | else if(!p->lchild) 156 | { 157 | q = p; 158 | p = p->rchild; 159 | free(q); 160 | } 161 | else 162 | { 163 | q = p; 164 | s = p->lchild; 165 | 166 | while(s->rchild) 167 | { 168 | q = s; 169 | s = s->rchild; 170 | } 171 | 172 | p->data = s->data; 173 | 174 | if(q != p) 175 | { 176 | q->rchild = s->lchild; 177 | } 178 | else 179 | { 180 | q->lchild = s->lchild; 181 | } 182 | 183 | free(s); 184 | } 185 | } 186 | 187 | bool DeleteBST(BiTree & T, KeyType key) 188 | { 189 | if(!T) 190 | { 191 | return false; 192 | } 193 | else 194 | { 195 | if EQ(key,T->data.key) 196 | { 197 | Delete(T); 198 | } 199 | else if LT(key,T->data.key) 200 | { 201 | DeleteBST(T->lchild, key); 202 | } 203 | else 204 | { 205 | DeleteBST(T->rchild, key); 206 | } 207 | 208 | return true; 209 | } 210 | } 211 | 212 | void DestroyBST(BiTree & T) 213 | { 214 | if(T) 215 | { 216 | if(T->lchild) 217 | { 218 | DestroyBST(T->lchild); 219 | } 220 | if(T->rchild) 221 | { 222 | DestroyBST(T->rchild); 223 | } 224 | free(T); 225 | T = NULL; 226 | } 227 | } 228 | 229 | void TraverseBST(BiTree T, void(*Visit)(ElemType)) 230 | { 231 | if(T) 232 | { 233 | TraverseBST(T->lchild, Visit); 234 | Visit(T->data); 235 | TraverseBST(T->rchild, Visit); 236 | } 237 | } 238 | 239 | void PreOrderTraverse(BiTree T, void(*Visit)(ElemType)) 240 | { 241 | if(T) 242 | { 243 | Visit(T->data); 244 | PreOrderTraverse(T->lchild, Visit); 245 | PreOrderTraverse(T->rchild, Visit); 246 | } 247 | } 248 | 249 | -------------------------------------------------------------------------------- /DataStructureSourceCode/查找与排序/树型查找/红黑树.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | 5 | # define N 10 6 | 7 | typedef enum Color 8 | { 9 | RED, BLACK 10 | } Color; 11 | 12 | typedef int KeyType; 13 | 14 | typedef struct ElemType 15 | { 16 | KeyType key; 17 | int order; 18 | } ElemType; 19 | 20 | typedef struct RBTNode 21 | { 22 | ElemType data; 23 | Color color; 24 | struct RBTNode * parent; 25 | struct RBTNode * left; 26 | struct RBTNode * right; 27 | } RBTNode, * RBTree; 28 | 29 | typedef struct RBRoot 30 | { 31 | RBTree root; 32 | } RBRoot; 33 | 34 | # define EQ(a,b) ((a)==(b)) 35 | # define LT(a,b) ((a)<(b)) 36 | # define LQ(a,b) ((a)<=(b)) 37 | 38 | RBRoot * CreateRBTree(void); 39 | void RBTreeDestroy(RBTree); 40 | void DestroyRBTree(RBRoot *); 41 | void PreOrder(RBTree); 42 | void PreOrderTraverse(RBRoot *); 43 | void InOrder(RBTree); 44 | void InOrderTraverse(RBRoot *); 45 | RBTree Search(RBTree, KeyType); 46 | bool SearchRBTree(RBRoot *, KeyType); 47 | void L_Rotate(RBRoot *, RBTree); 48 | void R_Rotate(RBRoot *, RBTree); 49 | void RBTreeInsertFixup(RBRoot *, RBTree); 50 | void RBTreeInsert(RBRoot *, RBTree); 51 | RBTree CreateRBTNode(ElemType, RBTree, RBTree, RBTree); 52 | bool InsertRBTree(RBRoot *, ElemType); 53 | void RBTreeDeleteFixup(RBRoot *, RBTree, RBTree); 54 | void RBTreeDelete(RBRoot *, RBTree); 55 | void DeleteRBTree(RBRoot *, KeyType); 56 | 57 | int main(void) 58 | { 59 | RBRoot * RBT; 60 | ElemType e[N] = {{3, 1}, {2, 2}, {1, 3}, {7, 4}, {5, 5}, {10, 6}, {4, 7}, {9, 8}, {8, 9}, {6, 10}}; 61 | int i; 62 | 63 | RBT = CreateRBTree(); 64 | 65 | // for(i = 0; i < N; ++i) 66 | // { 67 | // InsertRBTree(RBT, e[i]); 68 | // } 69 | // InOrderTraverse(RBT); 70 | // printf("\n"); 71 | // PreOrderTraverse(RBT); 72 | // printf("\n"); 73 | 74 | // DeleteRBTree(RBT, 3); 75 | // InOrderTraverse(RBT); 76 | // printf("\n"); 77 | // PreOrderTraverse(RBT); 78 | // printf("\n"); 79 | 80 | DestroyRBTree(RBT); 81 | 82 | return 0; 83 | } 84 | 85 | RBRoot * CreateRBTree(void) 86 | { 87 | RBRoot * root = (RBRoot *)malloc(sizeof(RBRoot)); 88 | root->root = NULL; 89 | 90 | return root; 91 | } 92 | 93 | void RBTreeDestroy(RBTree tree) 94 | { 95 | if(tree) 96 | { 97 | if(tree->left) 98 | { 99 | RBTreeDestroy(tree->left); 100 | } 101 | if(tree->right) 102 | { 103 | RBTreeDestroy(tree->right); 104 | } 105 | free(tree); 106 | tree = NULL; 107 | } 108 | } 109 | 110 | void DestroyRBTree(RBRoot * root) 111 | { 112 | if(root) 113 | { 114 | RBTreeDestroy(root->root); 115 | } 116 | 117 | free(root); 118 | } 119 | 120 | void PreOrder(RBTree tree) 121 | { 122 | if(tree) 123 | { 124 | printf("%s(%d, %d) ", tree->color == RED ? "R" : "B", tree->data.key, tree->data.order); 125 | PreOrder(tree->left); 126 | PreOrder(tree->right); 127 | } 128 | } 129 | 130 | void PreOrderTraverse(RBRoot * root) 131 | { 132 | if(root) 133 | { 134 | PreOrder(root->root); 135 | } 136 | } 137 | 138 | void InOrder(RBTree tree) 139 | { 140 | if(tree) 141 | { 142 | InOrder(tree->left); 143 | printf("%s(%d, %d) ", tree->color == RED ? "R" : "B", tree->data.key, tree->data.order); 144 | InOrder(tree->right); 145 | } 146 | } 147 | 148 | void InOrderTraverse(RBRoot * root) 149 | { 150 | if(root) 151 | { 152 | InOrder(root->root); 153 | } 154 | } 155 | 156 | RBTree Search(RBTree tree, KeyType key) 157 | { 158 | if(!tree || EQ(key,tree->data.key)) 159 | { 160 | return tree; 161 | } 162 | else if LT(key,tree->data.key) 163 | { 164 | return Search(tree->left, key); 165 | } 166 | else 167 | { 168 | return Search(tree->right, key); 169 | } 170 | } 171 | 172 | bool SearchRBTree(RBRoot * root, KeyType key) 173 | { 174 | if(root) 175 | { 176 | return Search(root->root, key) ? true : false; 177 | } 178 | } 179 | 180 | void L_Rotate(RBRoot * root, RBTree p) 181 | { 182 | RBTree rc; 183 | 184 | rc = p->right; 185 | 186 | p->right = rc->left; 187 | if(rc->left) 188 | { 189 | rc->left->parent = p; 190 | } 191 | 192 | rc->parent = p->parent; 193 | 194 | if(!p->parent) 195 | { 196 | root->root = rc; 197 | } 198 | else 199 | { 200 | if(p->parent->left == p) 201 | { 202 | p->parent->left = rc; 203 | } 204 | else 205 | { 206 | p->parent->right = rc; 207 | } 208 | } 209 | 210 | rc->left = p; 211 | 212 | p->parent = rc; 213 | } 214 | 215 | void R_Rotate(RBRoot * root, RBTree p) 216 | { 217 | RBTree lc; 218 | 219 | lc = p->left; 220 | 221 | p->left = lc->right; 222 | if(lc->right) 223 | { 224 | lc->right->parent = p; 225 | } 226 | 227 | lc->parent = p->parent; 228 | 229 | if(!p->parent) 230 | { 231 | root->root = lc; 232 | } 233 | else 234 | { 235 | if(p->parent->left == p) 236 | { 237 | p->parent->left = lc; 238 | } 239 | else 240 | { 241 | p->parent->right = lc; 242 | } 243 | } 244 | 245 | lc->right = p; 246 | 247 | p->parent = lc; 248 | } 249 | 250 | void RBTreeInsertFixup(RBRoot * root, RBTree node) 251 | { 252 | RBTree parent; 253 | RBTree gparent; 254 | 255 | while((parent = node->parent) && (parent->color == RED)) 256 | { 257 | gparent = parent->parent; 258 | 259 | if(gparent->left == parent) 260 | { 261 | RBTree uncle = gparent->right; 262 | if(uncle && uncle->color == RED) 263 | { 264 | uncle->color = BLACK; 265 | parent->color = BLACK; 266 | gparent->color = RED; 267 | node = gparent; 268 | 269 | continue; 270 | } 271 | 272 | if(parent->right == node) 273 | { 274 | RBTree temp; 275 | L_Rotate(root, parent); 276 | temp = parent; 277 | parent = node; 278 | node = temp; 279 | } 280 | 281 | parent->color = BLACK; 282 | gparent->color = RED; 283 | R_Rotate(root, gparent); 284 | } 285 | else 286 | { 287 | RBTree uncle = gparent->left; 288 | if(uncle && uncle->color == RED) 289 | { 290 | uncle->color = BLACK; 291 | parent->color = BLACK; 292 | gparent->color = RED; 293 | node = gparent; 294 | 295 | continue; 296 | } 297 | 298 | if(parent->left == node) 299 | { 300 | RBTree temp; 301 | R_Rotate(root, parent); 302 | temp = parent; 303 | parent = node; 304 | node = temp; 305 | } 306 | 307 | parent->color = BLACK; 308 | gparent->color = RED; 309 | L_Rotate(root, gparent); 310 | } 311 | } 312 | 313 | root->root->color = BLACK; 314 | } 315 | 316 | void RBTreeInsert(RBRoot * root, RBTree node) 317 | { 318 | RBTree p = root->root; 319 | RBTree q = NULL; 320 | 321 | while(p) 322 | { 323 | q = p; 324 | if LT(node->data.key,p->data.key) 325 | { 326 | p = p->left; 327 | } 328 | else 329 | { 330 | p = p->right; 331 | } 332 | } 333 | 334 | node->parent = q; 335 | 336 | if(q) 337 | { 338 | if LT(node->data.key,q->data.key) 339 | { 340 | q->left = node; 341 | } 342 | else 343 | { 344 | q->right = node; 345 | } 346 | } 347 | else 348 | { 349 | root->root = node; 350 | } 351 | 352 | node->color = RED; 353 | 354 | RBTreeInsertFixup(root, node); 355 | } 356 | 357 | RBTree CreateRBTNode(ElemType e, RBTree parent, RBTree left, RBTree right) 358 | { 359 | RBTree p; 360 | 361 | p = (RBTree)malloc(sizeof(RBTNode)); 362 | if(!p) 363 | { 364 | return NULL; 365 | } 366 | 367 | p->data = e; 368 | p->parent = parent; 369 | p->left = left; 370 | p->right = right; 371 | p->color = BLACK; 372 | 373 | return p; 374 | } 375 | 376 | bool InsertRBTree(RBRoot * root, ElemType e) 377 | { 378 | RBTree node; 379 | 380 | if(Search(root->root, e.key)) 381 | { 382 | return false; 383 | } 384 | 385 | node = CreateRBTNode(e, NULL, NULL, NULL); 386 | if(!node) 387 | { 388 | return false; 389 | } 390 | 391 | RBTreeInsert(root, node); 392 | 393 | return true; 394 | } 395 | 396 | void RBTreeDeleteFixup(RBRoot * root, RBTree node, RBTree parent) 397 | { 398 | RBTree other; 399 | 400 | while((!node || node->color == BLACK) && node != root->root) 401 | { 402 | if(parent->left == node) 403 | { 404 | other = parent->right; 405 | if(other->color == RED) 406 | { 407 | other->color = BLACK; 408 | parent->color = RED; 409 | L_Rotate(root, parent); 410 | other = parent->right; 411 | } 412 | 413 | if((!other->left || other->left->color == BLACK) && (!other->right || other->right->color == BLACK)) 414 | { 415 | other->color = RED; 416 | node = parent; 417 | parent = node->parent; 418 | } 419 | else 420 | { 421 | if(!other->right || other->right->color == BLACK) 422 | { 423 | other->left->color = BLACK; 424 | other->color = RED; 425 | R_Rotate(root, other); 426 | other = parent->right; 427 | } 428 | 429 | other->color = parent->color; 430 | parent->color = BLACK; 431 | other->right->color = BLACK; 432 | L_Rotate(root, parent); 433 | node = root->root; 434 | 435 | break; 436 | } 437 | } 438 | else 439 | { 440 | other = parent->left; 441 | if(other->color == RED) 442 | { 443 | other->color = BLACK; 444 | parent->color = RED; 445 | R_Rotate(root, parent); 446 | other = parent->left; 447 | } 448 | 449 | if((!other->left || other->left->color == BLACK) && (!other->right || other->right->color == BLACK)) 450 | { 451 | other->color = RED; 452 | node = parent; 453 | parent = node->parent; 454 | } 455 | else 456 | { 457 | if(!other->left || other->left->color == BLACK) 458 | { 459 | other->right->color = BLACK; 460 | other->color = RED; 461 | L_Rotate(root, other); 462 | other = parent->left; 463 | } 464 | 465 | other->color = parent->color; 466 | parent->color = BLACK; 467 | other->left->color = BLACK; 468 | R_Rotate(root, parent); 469 | node = root->root; 470 | 471 | break; 472 | } 473 | } 474 | } 475 | 476 | if(node) 477 | { 478 | node->color = BLACK; 479 | } 480 | } 481 | 482 | void RBTreeDelete(RBRoot * root, RBTree node) 483 | { 484 | RBTree child; 485 | RBTree parent; 486 | Color color; 487 | 488 | if(node->left && node->right) 489 | { 490 | RBTree replace = node; 491 | 492 | replace = replace->right; 493 | while(replace->left) 494 | { 495 | replace = replace->left; 496 | } 497 | 498 | if(node->parent) 499 | { 500 | if(node->parent->left == node) 501 | { 502 | node->parent->left = replace; 503 | } 504 | else 505 | { 506 | node->parent->right = replace; 507 | } 508 | } 509 | else 510 | { 511 | root->root = replace; 512 | } 513 | 514 | child = replace->right; 515 | parent = replace->parent; 516 | color = replace->color; 517 | 518 | if(parent == node) 519 | { 520 | parent = replace; 521 | } 522 | else 523 | { 524 | if(child) 525 | { 526 | child->parent = parent; 527 | } 528 | parent->left = child; 529 | 530 | replace->right = node->right; 531 | node->right->parent = replace; 532 | } 533 | 534 | replace->parent = node->parent; 535 | replace->color = node->color; 536 | replace->left = node->left; 537 | node->left->parent = replace; 538 | 539 | if(color == BLACK) 540 | { 541 | RBTreeDeleteFixup(root, child, parent); 542 | } 543 | 544 | free(node); 545 | 546 | return; 547 | } 548 | 549 | if(node->left) 550 | { 551 | child = node->left; 552 | } 553 | else 554 | { 555 | child = node->right; 556 | } 557 | 558 | parent = node->parent; 559 | color = node->color; 560 | 561 | if(child) 562 | { 563 | child->parent = parent; 564 | } 565 | 566 | if(parent) 567 | { 568 | if(parent->left == node) 569 | { 570 | parent->left = child; 571 | } 572 | else 573 | { 574 | parent->right = child; 575 | } 576 | } 577 | else 578 | { 579 | root->root = child; 580 | } 581 | 582 | if(color == BLACK) 583 | { 584 | RBTreeDeleteFixup(root, child, parent); 585 | } 586 | 587 | free(node); 588 | } 589 | 590 | void DeleteRBTree(RBRoot * root, KeyType key) 591 | { 592 | RBTree node; 593 | 594 | node = Search(root->root, key); 595 | if(node) 596 | { 597 | RBTreeDelete(root, node); 598 | } 599 | } 600 | 601 | --------------------------------------------------------------------------------