![](https://img-blog.csdnimg.cn/20200112231734273.jpeg?x-oss-process=image/resize,m_fixed,h_224,w_224)
数据结构(C语言版)
数据结构,C语言实现。
楼主将本科期间学习数据结构积累的代码搬上博客,供自己在需要的时候备忘查找。
热爱艺术的程序员
github:https://github.com/AndrewLauGithub
展开
-
数据结构常用算法清单
数据结构-C语言实现常用算法清单:数据结构(C语言版)顺序表:https://blog.csdn.net/qq_16261421/article/details/103951230单链表:https://blog.csdn.net/qq_16261421/article/details/105958763双链表:https://blog.csdn.net/qq_16261421...原创 2020-05-06 21:53:51 · 568 阅读 · 1 评论 -
拓扑排序
拓扑排序算法的实现:#include "GraphBasicOperation.cpp"void TopSort(ALGraph *G){ int i,j; int St[MAXV],top=-1; //栈St的指针为top ArcNode *p; for (i=0;i<G->n;i++) //入度置初值0 G->adjlist[i].count=0; for (i=0;i<G->n;i++) //求所有顶点的入度 { p=G-&g原创 2020-05-12 11:24:41 · 609 阅读 · 0 评论 -
希尔排序
希尔排序算法实现如下:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void ShellSort(Re原创 2020-05-11 06:46:22 · 199 阅读 · 0 评论 -
直接选择排序
直接选择排序算法实现如下:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void SelectSort(原创 2020-05-10 15:33:21 · 196 阅读 · 0 评论 -
基数排序
基数排序算法实现如下:#include <stdio.h>#include <malloc.h>#include <string.h>#define MAXE 20 //线性表中最多元素个数#define MAXR 10 //基数的最大取值#define MAXD 8 //关键字位数的最大取值typedef struct node{ char data[MAXD]; //记录的关键字定义的字符串 struct node *nex原创 2020-05-11 06:49:18 · 134 阅读 · 0 评论 -
快速排序
快速排序算法实现如下:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void QuickSort(Rec原创 2020-05-10 13:14:24 · 202 阅读 · 0 评论 -
归并排序-自顶向下的二路归并
自顶向下二路归并排序算法实现如下:#include <stdio.h>#include <malloc.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType;原创 2020-05-10 17:35:46 · 2035 阅读 · 1 评论 -
归并排序-自底向上的二路归并
自底向上二路归并算法的实现:#include <stdio.h>#include <malloc.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; /原创 2020-05-10 17:36:20 · 997 阅读 · 0 评论 -
折半插入排序
折半插入排序:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void InsertSort1(RecT原创 2020-05-10 16:53:31 · 328 阅读 · 1 评论 -
直接插入排序
直接插入排序算法实现:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void InsertSort(R原创 2020-05-10 16:04:12 · 226 阅读 · 0 评论 -
堆排序
堆排序算法的实现:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void sift(RecType R[原创 2020-05-11 06:50:55 · 178 阅读 · 0 评论 -
冒泡排序-改进版
冒泡排序算法改进版:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void BubbleSort1(Re原创 2020-05-10 12:18:55 · 226 阅读 · 0 评论 -
冒泡排序
冒泡排序算法的实现:#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void BubbleSort(Re原创 2020-05-10 11:54:36 · 121 阅读 · 0 评论 -
B树
B树的基本运算算法实现如下:#include <stdio.h>#include <malloc.h>#define MAXM 10 //定义B-树的最大的阶数typedef int KeyType; //KeyType为关键字类型typedef struct node //B-树结点类型定义{ int keynum; //结点当前拥有的关键字的个数 KeyType key[MA原创 2020-05-11 06:55:35 · 139 阅读 · 0 评论 -
平衡二叉树(AVL树)
平衡二叉树的实现过程如下:#include <stdio.h>#include <malloc.h>typedef int KeyType; //定义关键字类型typedef char InfoType;typedef struct node //记录类型{ KeyType key; //关键字项 int bf; //平衡因子 InfoType data;原创 2020-05-11 06:55:58 · 185 阅读 · 1 评论 -
二叉排序树
二叉排序树的基本运算实现如下:#include <stdio.h>#include <malloc.h>typedef int KeyType;typedef char InfoType[10];typedef struct node //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据域 struct node *lchild,*rc原创 2020-05-11 06:56:24 · 272 阅读 · 0 评论 -
哈希表
哈希表基本运算实现:#include <stdio.h>#define MaxSize 100 //定义最大哈希表长度#define NULLKEY -1 //定义空关键字值#define DELKEY -2 //定义被删关键字值typedef int KeyType; //关键字类型typedef char * InfoType; //其他数据类型typedef struct{ KeyType key; //关键字域 InfoType data;原创 2020-05-11 06:54:47 · 225 阅读 · 0 评论 -
分块查找
分块查找算法:实现如下:#include <stdio.h>#define MAXL 100 //数据表的最大长度#define MAXI 20 //索引表的最大长度typedef int KeyType;typedef char InfoType[10];typedef struct { KeyType key; //KeyType为关键字的数据类型 InfoType data; //其他数据} NodeT原创 2020-05-11 06:53:58 · 436 阅读 · 0 评论 -
折半/二分查找
折半查找算法:实现如下:#include <stdio.h>#define MAXL 100typedef int KeyType;typedef char InfoType[10];typedef struct { KeyType key; //KeyType为关键字的数据类型 InfoType data; //其他数据} NodeType;typedef NodeType SeqList[MAXL]; //原创 2020-05-10 11:39:53 · 312 阅读 · 0 评论 -
顺序查找算法
顺序查找算法代码实现如下:#include <stdio.h>#define MAXL 100typedef int KeyType;typedef char InfoType[10];typedef struct { KeyType key; //KeyType为关键字的数据类型 InfoType data; //其他数据} NodeType;typedef NodeType SeqList[MAXL];原创 2020-05-10 10:16:17 · 2673 阅读 · 0 评论 -
环形队列2-基于队头指针和队列中元素的个数的另一种实现
由队头指针和元素个数进行如下计算:已知front、rear,求队中元素个数: count=(rear-front+MaxSize)%MaxSize已知front、count,求rear: rear=(front+rear)%MaxSize已知rear、count,求front: front=(rear-count+MaxSize)%MaxSize环形队列的基本操作:队空条件:count=0队满条件:count=MaxSize进队e操作:rear=(re原创 2020-05-08 22:03:03 · 3411 阅读 · 1 评论 -
顺序队列
顺序队中实现队列的基本运算#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct { ElemType data[MaxSize]; int front,rear; //队首和队尾指针} SqQueue;void InitQueue(SqQueue *&q){ q=(SqQueue *)malloc (sizeof(S原创 2020-05-08 21:46:34 · 926 阅读 · 0 评论 -
图的遍历之广度优先遍历BFS
GraphBasicOperation.cpp文件链接:https://blog.csdn.net/qq_16261421/article/details/106005857广度优先搜索遍历的过程是: (1)访问初始点v,接着访问v的所有未被访问过的邻接点v1,v2,…,vt。 (2)按照v1,v2,…,vt的次序,访问每一个顶点的所有未被访问过的邻接点。 (3)依次类推,直到图中所有和初始点v有路径相通的顶点都被访问过为止。 广度优先遍历体现先进先出的特点,用队列实现。..原创 2020-05-08 21:06:26 · 438 阅读 · 0 评论 -
图的遍历之深度优先遍历DFS
GraphBasicOperation.cpp文件链接:https://blog.csdn.net/qq_16261421/article/details/106005857深度优先搜索遍历的过程是: (1)从图中某个初始顶点v出发,首先访问初始顶点v。 (2)选择一个与顶点v相邻且没被访问过的顶点w为初始顶点,再从w出发进行深度优先搜索,直到图中与当前顶点v邻接的所有顶点都被访问过为止。 #include "GraphBasicOperation.cpp"void BFS(A原创 2020-05-08 20:57:37 · 451 阅读 · 0 评论 -
图的存储-邻接矩阵和邻接表之间的相互转化
邻接矩阵和邻接表之间的相互转化,输出邻接矩阵和邻接表算法实现。将文件保存为GraphBasicOperation.cpp文件,具体实现如下:#include <stdio.h>#include <malloc.h>#include "graph.h"void MatToList(MGraph g,ALGraph *&G)//将邻接矩阵g转换成邻接表G{ int i,j; ArcNode *p; G=(ALGraph *)malloc(sizeof(AL原创 2020-05-08 20:47:54 · 7417 阅读 · 3 评论 -
图数据结构的定义
用邻接矩阵和邻接表存储,将其命名为文件graph.h头文件,后续图的所有算法都依赖于该文件。1.邻接矩阵存储方法 邻接矩阵是表示顶点之间相邻关系的矩阵。设G=(V,E)是具有n(n>0)个顶点的图,顶点的顺序依次为0~n-1,则G的邻接矩阵A是n阶方阵,其定义如下: (1)如果G是无向图,则: A[i][j]=1:若(i,j)∈E(G) 0:其他 (2)如果G是有向图,则: A[i][j]=1:若<i,j>...原创 2020-05-08 20:39:07 · 993 阅读 · 1 评论 -
弗洛伊德(Floyd)算法
求每对顶点之间的最短路径弗洛伊德算法实现#include <stdio.h>#define MaxSize 100#define INF 32767 //INF表示∞#define MAXV 100 //最大顶点个数typedef int InfoType;typedef struct { int no; //顶点编号 InfoType info; //顶点其他信息} VertexType; //顶点类型typedef struct原创 2020-05-15 19:41:30 · 295 阅读 · 0 评论 -
狄克斯特拉(Dijkstra)算法
从一个顶点到其余各顶点的最短路径狄克斯特拉算法实现#include <stdio.h>#define MaxSize 100#define INF 32767 //INF表示∞#define MAXV 100 //最大顶点个数typedef int InfoType;typedef struct { int no; //顶点编号 InfoType info; //顶点其他信息} VertexType; //顶点类型typedef st原创 2020-05-15 21:12:31 · 824 阅读 · 0 评论 -
克鲁斯卡尔算法-改进版
#include <stdio.h>#define MaxSize 100#define INF 32767 //INF表示∞#define MAXV 100 //最大顶点个数typedef int InfoType;typedef struct { int no; //顶点编号 InfoType info; //顶点其他信息} VertexType; //顶点类型typedef struct //图的定义{ int ed.原创 2020-05-14 21:00:40 · 484 阅读 · 0 评论 -
最小生成树算法-克鲁斯卡尔(kruskal)算法
#include <stdio.h>#define MaxSize 100#define INF 32767 //INF表示∞#define MAXV 100 //最大顶点个数typedef int InfoType;typedef struct { int no; //顶点编号 InfoType info; //顶点其他信息} VertexType; //顶点类型typedef struct //图的定义{ int ed.原创 2020-05-14 08:20:06 · 309 阅读 · 0 评论 -
最小生成树算法-普里姆(prim)算法
prime算法实现#include <stdio.h>#define MAXV 20 //最多顶点数#define INF 32767 //INF表示∞typedef char InfoType;typedef struct { int no; //顶点编号 InfoType info; //顶点其他信息} VertexType; //顶点类型typedef struct //图的定义{ int edges原创 2020-05-13 14:16:06 · 462 阅读 · 0 评论 -
哈夫曼树和哈夫曼编码
哈夫曼树和哈夫曼编码#include <stdio.h>#include <string.h>#define N 50 //叶子结点数#define M 2*N-1 //树中结点总数typedef struct{ char data[5]; //结点值 double weight; //权重 int parent; //双亲结点 int lchild; //左孩子结点 int rchild; //右孩子结点} HTNode;typedef st原创 2020-05-09 22:06:04 · 240 阅读 · 0 评论 -
线索二叉树和非递归遍历(中序)
基于中序遍历的线索二叉树和非递归遍历#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct node { ElemType data; int ltag,rtag; //增加的线索标记 struct node *lchild; struct node *rchild;} TBTNode;void CreateTBTNo原创 2020-05-09 22:51:17 · 1307 阅读 · 1 评论 -
广义表的基本运算
广义表的基本运算和实现#include <stdio.h>#include <malloc.h>#include "glnode.h"int GLLength(GLNode *g) //求广义表g的长度{ int n=0; GLNode *g1; g1=g->val.sublist; //g指向广义表的第一个元素 while (g1!=NUL...原创 2020-05-09 20:55:18 · 1719 阅读 · 0 评论 -
稀疏矩阵十字链表表示法
稀疏矩阵十字链表表示法和基本运算#include <stdio.h>#include <malloc.h>#define M 3 //矩阵行#define N 3 //矩阵列#define Max ((M)>(N)?(M):(N)) //矩阵行列较大者typedef int ElemType;ty...原创 2020-05-09 21:51:50 · 1680 阅读 · 0 评论 -
稀疏矩阵三元组表示法
稀疏矩阵三元组表示法的定义及其基本运算#include <stdio.h>#define M 6#define N 7#define MaxSize 100 //矩阵中非零元素最多个数typedef int ElemType;typedef struct{ int r; //行号 int c; //列号 ElemType d; ...原创 2020-05-07 20:51:22 · 2270 阅读 · 1 评论 -
KMP算法的改进
KMP算法的改进#include "sqstring.cpp"void GetNextval(SqString t,int nextval[]) //由模式串t求出nextval值{ int j=0,k=-1; nextval[0]=-1; while (j<t.length) { if (k==-1 || t.data[j]==t.data[k]...原创 2020-05-07 08:01:22 · 252 阅读 · 0 评论 -
KMP算法
KMP算法-简单匹配算法的改进KMP算法的实现如下:#include "sqstring.cpp"void GetNext(SqString t,int next[]) /*由模式串t求出next值*/{ int j,k; j=0;k=-1;next[0]=-1; while (j<t.length-1) { if (k==-1 || t.data[j]==t...原创 2020-05-07 07:58:51 · 382 阅读 · 0 评论 -
BF算法(简单匹配算法)
字符串的简单匹配算法-BF算法的实现#include "sqstring.cpp"int index(SqString s,SqString t){ int i=0,j=0; while (i<s.length && j<t.length) { if (s.data[i]==t.data[j]) //继续匹配下一个字符 { i++;...原创 2020-05-07 07:55:38 · 7555 阅读 · 2 评论 -
链串
字符串的链式存储及其基本运算的实现#include <stdio.h>#include <malloc.h>typedef struct snode { char data; struct snode *next;} LiString;void StrAssign(LiString *&s,char cstr[]) //字符串常量cstr赋给串...原创 2020-05-07 07:49:56 · 255 阅读 · 0 评论