数据结构与算法
LikeMoe
这个作者很懒,什么都没留下…
展开
-
二叉树——求路径
// 求到某一顶点的路径,倒序 // An highlighted block #include<stdio.h> #include<stdlib.h> struct Node{ int weight; Node *left; Node *right; }; /* 创建树 */ Node *CreateTree(){ Node *root = NULL; int ch; scanf("%d",&ch); if(ch!=原创 2020-12-08 21:45:41 · 323 阅读 · 1 评论 -
二叉树——求WPL
// 求WPL // An highlighted block #include<stdio.h> #include<stdlib.h> struct Node{ int weight; Node *left; Node *right; }; /* 创建树 */ Node *CreateTree(){ Node *root = NULL; int ch; scanf("%d",&ch); if(ch!=-1){原创 2020-12-01 22:21:40 · 1182 阅读 · 0 评论 -
背包问题——贪心算法
// 分别以不同起点开始进行以当前最优的策略选择,直到依次遍历所有的开始点 // An highlighted block #include<stdio.h> #include<stdlib.h> #define MAXSIZE 60 bool HasExist(int Exist[],int len,int flag){ for (int i = 0; i < len; i++) { if(Exist[i]==flag)原创 2020-09-03 20:16:16 · 391 阅读 · 0 评论 -
无向图——基本操作
1. 无向图的基本操作 // An highlighted block /* 无向图--邻接矩阵表示 */ #include<stdio.h> #include<stdlib.h> #define MAXSIZE 60 #define datatype char struct MGraph { datatype Vex[MAXSIZE]; int Edge[MAXSIZE][MAXSIZE]; int vexnum,arcnum; /* dat.原创 2020-09-02 19:12:24 · 952 阅读 · 0 评论 -
数组——求值 [递归]
// A code block 1. 求最大值 2. 求平均值 3. 求和 // An highlighted block #include<stdio.h> #include<stdlib.h> float max(float n,float m){ return n>m?n:m; } //递归求最大值 float GetMAX(float data[],int i){ return i>=0?max(data[i],GetMAX(data,原创 2020-09-02 19:09:25 · 168 阅读 · 0 评论 -
查找——二分查找法
// A code block 1. 二分查找法 2. 偶数前奇数后 // An highlighted block #include<stdio.h> #include<stdlib.h> ///在时间复杂度为O(n)的情况下,把数组中的数分为左为偶数,右为奇数 void OddAndDou(int data[],int length){ for (int i = 0,j=length-1; i < j;) { if(data[i]%原创 2020-09-02 19:03:13 · 193 阅读 · 0 评论 -
二叉树——基本操作
// A code block 二叉树的一些基本操作 1. 层序遍历 2. 先序遍历 3. 后序遍历 4. 中序遍历 5. 倒叙遍历 6. 求树高 7. 求叶子节点数 // An highlighted block #include<stdio.h> #include<stdlib.h> #include<iostream> #define MAXSIZE 60 #define datatype char struct Tree原创 2020-08-24 19:10:22 · 152 阅读 · 0 评论 -
栈的应用——括号匹配
// A code block 1. 遇到左括号入栈 2. 遇到右括号,出栈,如果与当前的右括号匹配则当前匹配成功,否则失败 3. 遍历目标要匹配字符串,并重复1,2步骤 // An highlighted block /* 栈的应用------括号匹配 */ #include<stdio.h> #include<stdlib.h> #define MAXSIZE 60 struct Stack { char data[MAXSIZE]; int top;原创 2020-08-24 19:02:01 · 231 阅读 · 0 评论 -
栈的应用——后缀表达式求值
// A code block 1. 若当前字符为数字时直接入栈 2. 若当前字符为运算符时,将两个字符出栈(当为 ‘-’或‘/’ 时用栈较用下面的数字减去上面的数字),并把结果压入栈中 3. 当所有的表达式都遍历完成之后,栈顶元素即为表达式运算结果 /* 栈在表达式中的应用--------求值 */ #include<stdio.h> #include<stdlib.h> #define MAXSIZE 60 struct Stack { char data[M原创 2020-08-24 18:54:18 · 727 阅读 · 0 评论 -
栈的应用——中缀转后缀表达式
下面展示一些 内联代码片。 1. 当遇到数字时直接输出 2. 当遇到运算符时,依次输出栈中比当前运算级别更高的运算符,直到遇到栈中的 ‘(’或栈底,并把当前运算符压入栈 3. 当遇到 ‘)’ 直接输出栈中的元素,直到遇到 '(' 结束 4. 当表达式字符串都遍历完成之后,把栈中元素依次出栈 // An highlighted block /* 栈在表达式中的应用 中缀转后缀 实现方式,栈+链表 */ #include<stdio.h> #include<stdlib.h原创 2020-08-24 18:42:42 · 162 阅读 · 0 评论