随笔分类 -  数据结构与算法

摘要:问题:对可以分为大顶堆和小顶堆。大顶堆满足父节点大于左右孩子节点:父节点i,则i>2*i ;i>2*i+1;小顶堆满足父节点小于左右孩子节点:父节点i,则i<2*i ;i<2*i+1;指针问题,在交换两个变量时,确保交换,要用指针。下面代码是小顶堆排序。代码:#include <iostream>using namespace std;void buildHeap(int arr[],int length) //建立堆{ int i; int *min; int *temp; int t; int f; int k; int flag; int n=leng 阅读全文
posted @ 2013-05-30 20:59 xshang 阅读(337) 评论(0) 推荐(0) 编辑
摘要:问题:希尔排序又叫缩小增量排序。增量的选取为d=length/2;d=d/2...d=1;代码:#include <iostream>using namespace std;void shellSort(int arr[],int length){ int d; int temp; int j,k; static int count=1; for(d=length/2;d>=1;d=d/2) { for(j=d;j<length;j++) { for(k=j-d;k>=0;k=k-d) { if(arr[k]>arr[k+d]) { temp=... 阅读全文
posted @ 2013-05-27 20:08 xshang 阅读(204) 评论(0) 推荐(0) 编辑
摘要:问题:找临界点挺难的,是了好几次,终于OK了。代码:#include <iostream>using namespace std;void qSort(int arr[],int left,int right){ int i,j; int pos; static int count=1; if(left>=right) return; i=left; j=right; int key=arr[i]; pos=i; while(i<j) { while(i<j&&arr[j]>=key) { j--; } if(i<j&& 阅读全文
posted @ 2013-05-27 19:25 xshang 阅读(177) 评论(0) 推荐(0) 编辑
摘要:代码:#include <iostream>using namespace std;typedef struct ss{ int i; int key;}iSort;void display(iSort arr[],int n){ for(int i=0;i<n;i++) { cout<<arr[i].key<<" "; } cout<<endl;}int main(){ iSort arr[10]; int temp; int k,j; int m; for(int i=0;i<10;i++) { cin> 阅读全文
posted @ 2013-05-27 13:22 xshang 阅读(151) 评论(0) 推荐(0) 编辑
摘要:二叉排序树又叫二叉查找树。遵循三个原则:1、一棵树要么为空,要么2、左孩子节点小于根节点值,根节点值小于右孩子节点值3、子树也具备这个规则代码:#include <iostream>#include <cstdlib>using namespace std;static int flag=0;typedef struct binst{ int data; struct binst *lchild; struct binst *rchild;}*BST;void createBST(BST &bst,int key){ BST t,p,pre; t=(BST)ma 阅读全文
posted @ 2013-05-17 19:20 xshang 阅读(202) 评论(0) 推荐(0) 编辑
摘要:问题:明白思想,其他的没什么,记得结构体为指针时一定要动态分配内存。代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXL 20typedef struct seq{ int key[MAXL]; int len;}data;typedef struct table{ int start; int end; int d;}index[4];int block_search(index s,data *list,int key) //分块查找{ int i=0; int j; whi 阅读全文
posted @ 2013-05-16 10:48 xshang 阅读(469) 评论(0) 推荐(0) 编辑
摘要:比较简单,主要是明白算法思想。代码:#include <iostream>#include <cstdlib>using namespace std;int main(){ cout<<"/"<<"折半查找"<<"/"<<endl; cout<<"-------------------"<<endl; int arr[10]={1,2,3,4,5,6,7,8,9,10}; int low,high,mid; int ke 阅读全文
posted @ 2013-05-16 10:08 xshang 阅读(134) 评论(0) 推荐(0) 编辑
摘要:#include <iostream>#include <cstdlib>using namespace std;#define MAXL 20typedef struct data{ int d[MAXL]; int len;}arr;int searchKey(arr *a,int key){ int i; a->d[0]=key; for(i=a->len-1;i>0;i--) { if(a->d[i]==key) { return i; } } }int main(){ arr *a; a=(arr*)malloc(sizeof(stru 阅读全文
posted @ 2013-05-16 09:53 xshang 阅读(186) 评论(0) 推荐(0) 编辑
摘要:问题:关键还是定义数据结构问题,还有如何把两个集合合并成一个。代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXV 20#define INFINITY 65535typedef struct node{ int from; int to; int weight;}WGraph;typedef struct map{ WGraph arr[MAXV]; int vexs,edges;}*Map;void createGraph(Map &map) //创... 阅读全文
posted @ 2013-05-15 16:23 xshang 阅读(144) 评论(0) 推荐(0) 编辑
摘要:问题:经典的就是最简单的。用到两个数组:lowcost[] 和 closeset[],前者用来记录U集合和V集合的最小边,后者用来记录最小边的起始顶点。代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXV 20#define INFINITY 65535typedef struct map{ char vex[MAXV]; int arr[MAXV][MAXV]; int vexs,edges;}*mapNode;int locatePos(mapNode mn,char c){ 阅读全文
posted @ 2013-05-12 20:21 xshang 阅读(171) 评论(0) 推荐(0) 编辑
摘要:问题:还是指针的问题,在第一次遍历时,一定要用一个临时指针来指向图节点,不然等遍历玩指针为空,再用广度遍历算法就会失效。广度遍历类似于树层次遍历,只是有顺序,因此用到了队列。再次证明了STL的强大和方便。代码:#include <iostream>#include <cstdlib>#include <queue>using namespace std;#define MAXV 20typedef struct edgeNode{ int data; struct edgeNode *next;}edgeList;typedef struct headNod 阅读全文
posted @ 2013-05-11 09:43 xshang 阅读(535) 评论(0) 推荐(0) 编辑
摘要:问题:用标记来记录是否访问过,这个是个关键点。当然也可以用栈,思想是每访问一个顶点就入栈,接着访问这个顶点的下一个顶点,最后出栈,看出栈的顶点有没有下一个顶点,如果有就继续访问。如此重复,直到栈空。还有一点就是用到了递归思想。很像二叉树的前序遍历。代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXV 20typedef struct edgeNode{ int data; struct edgeNode *next;}edgeList;typedef struct headNod 阅读全文
posted @ 2013-05-10 20:46 xshang 阅读(497) 评论(0) 推荐(0) 编辑
摘要:问题:还是把邻接表的结构体定义搞明白,就没那么难了^^代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXV 20typedef struct edgeNode //边表节点{ int data; struct edgeNode *next;}edgeList;typedef struct headNode //头节点{ char vex; edgeList *firstedge;}headList;typedef struct adjNode //邻接表{ headLi... 阅读全文
posted @ 2013-05-09 19:48 xshang 阅读(685) 评论(0) 推荐(0) 编辑
摘要:问题:定义结构体要分配空间,这个不知为什么?有很长时间没写数据结构了,忙,也不知忙什么。。。下面创建的是无向图。主要是记得图的结构体的定义,我刚开始没想起来,查了资料才弄明白的。代码:#include <iostream>#include <cstdlib>using namespace std;#define MAXVEX 20typedef struct map{ char vex[MAXVEX]; int arrMap[MAXVEX][MAXVEX]; int numvexs,numedges;}mapNode;int getPos(mapNode *g,char 阅读全文
posted @ 2013-05-09 16:07 xshang 阅读(395) 评论(0) 推荐(0) 编辑
摘要:问题:事情总是这样,当你明白时,很简单,但当你不会时,又好像觉得自己怎么那么笨。。。huffman算法关键是选择两个最小的数时不要弄错了。刚开始看时,真的很吃力,都不敢相信自己居然把huffman译码也做出来了。代码:#include <iostream>#include <cstdlib>#include <cstring>using namespace std;int s1,s2;typedef struct huffmanNode{ int weight; int parent; int lchild; int rchild;}*HuffmanTree 阅读全文
posted @ 2013-05-01 17:01 xshang 阅读(807) 评论(2) 推荐(1) 编辑
摘要:问题:这两种操作都用到了递归,别人说递归好理解,我真的不觉得递归好理解,只是递归的代码看起来简单。下面代码是求高度的另一种方法。递归计算高度是从底向上计算的,因此叶子节点高度为0;方法真的很巧妙。。。比如getTreeHigh(a->lchild)返回的就是a的左子树的高度,getTreeHigh(a->lchild)返回的就是a的右子树的高度。int getTreeHigh(BinTree btree){ int depth; if(btree==NULL) depth=0; else { depthleft=getTreeHigh(btree->lchild); ... 阅读全文
posted @ 2013-04-30 16:16 xshang 阅读(2112) 评论(0) 推荐(0) 编辑
摘要:问题:再一次验证自己功底的薄弱,对那些大侠只有仰慕的份。。。简单说一下自己的感受,递归的实现是个栈,每次函数结束时,退回到上一层,函数结束默认为return。二叉树的各种非递归实现用到的数据结构是栈。这次用重温了STL中stack的用法。代码:#include <iostream>#include <cstdlib>#include <stack>using namespace std;typedef struct node{ char data; struct node *left; struct node *right;}*BinTree;void Cr 阅读全文
posted @ 2013-04-30 10:42 xshang 阅读(215) 评论(0) 推荐(0) 编辑
摘要:问题:比较简单^^代码:#include <iostream>using namespace std;#define MAXSIZE 1000typedef struct tMatrix{ int row; int col; int data;}TMatrix;typedef struct matrix{ TMatrix arr[MAXSIZE]; int r,c,num;}Matrix;void CreateTMatrix(Matrix &c){ int rs,cs,ns; int p,q,e; cout<<"please input the row 阅读全文
posted @ 2013-04-27 15:51 xshang 阅读(367) 评论(0) 推荐(0) 编辑
摘要:问题:很经典,也很难,理解了一个下午。。。终于把KMP算法思想弄明白了。改天继续看看。昨天写的,今天继续补充,KMP算法是由三个人发明的,KMP就是三个人名字的首字母。下面开始讲这个经典的算法:1、刚开始比较时,如果相等,下移;2、继续比较,遇到不相等的,假设位置为j,然后找j之前最大长度相等的前缀和后缀。这个怎么找不好理解,但理解了就很简单。用子串(现在作为主串)的第一个字符和j位置之前的第一个字符比较,如果相等,继续比较,第二个字符和j之前的第二个字符往后比较,。。。记录最大长度相等的前缀和后缀。3、如果没有,则主串和子串都增1。next数组的求法:首先,初始化。next[0]=-1;ne 阅读全文
posted @ 2013-04-26 22:36 xshang 阅读(243) 评论(0) 推荐(0) 编辑
摘要:代码:#include <iostream>using namespace std;int main(){ char *s="helolophp"; char *s1="lop"; char *c=s1; int len=0; while(c[len]!='\0') { len++; } int i=0,j=0; for(;s[i]!='\0';i++) { if(s1[j]==s[i]) { j++; if(j==len) { cout<<"匹配成功的起始位置是:"<&l 阅读全文
posted @ 2013-04-26 15:56 xshang 阅读(171) 评论(0) 推荐(0) 编辑