C专家编程在附录A2讨论了链表环检测的算法,实现一下,算法一通过设置标志,复杂度O(N),算法二就是所谓的追赶法,算法还是很好理解的,在链表尾部形成的环中,假设两个步伐不一致的的人在绕圈,总有一个交点,但是要证明该算法的正确性似乎还要费一些精力。先给出实现: 1: /* 2: Expert C Programming,Chinese Edition 3: How to detect loop in linked list, p274,solution1 4: */ 5: int check_loop1(pnode list) 6: { 7: p... Read More
posted @ 2012-07-05 22:54 Dance With Automation Views(667) Comments(0) Diggs(0)
given a list as: Header->1->2->3->4->5 reverse it, the idea should be like: Step1:Header->2->1->3->4->5 Step2:Header->3->2->1->4->5 Step3:Header->4->3->2->1->5 Step4:Header->5->4->3->2->1 running tim... Read More
posted @ 2012-07-02 22:23 Dance With Automation Views(282) Comments(0) Diggs(0)
Here I give two insertion sort, one use array, another use list. I also get a conclusion that nothing better in insertion_sort2 than insertion_sort, maybe the size is too large and too random which wi... Read More
posted @ 2012-06-29 09:06 Dance With Automation Views(174) Comments(0) Diggs(0)
1: #include <stdio.h> 2: #include <stdlib.h> 3: #define MAX_LENGTH 9999 4: void print_array(const int a[],int size) 5: { 6: int i=0; 7: for(i=0;i<size;i++) 8: { 9: printf("%d,",a[i]); 10: } 11: printf("\n"); 12: } 13: 14: /* 15: as... Read More
posted @ 2012-06-29 09:02 Dance With Automation Views(1707) Comments(0) Diggs(0)
最近在看最大公约数的一些性质,很有意思,有些性质simple,beautiful,乍看上很明显,但依旧需要思索一番才敢确认嘛。 如wikiepdia给出的这条性质: 现证明第二行: 不妨假设最大公约数为d,则a,b可以写成a=dx,b=dy的形式(乘积!非微分哦)。 于是a-b=d(x-y),于是命题证明变成要证明a-b=d(x-y)和b的最大公约数即是a与b的最大公约数。 而a-b=d(x-y... Read More
posted @ 2012-06-23 19:02 Dance With Automation Views(3353) Comments(3) Diggs(2)
首先给出wikipedia上Stein算法的理论依据: The algorithm reduces the problem of finding the GCD by repeatedly applying these identities: gcd(0, v) = v, because everything divides zero, and v is the largest number ... Read More
posted @ 2012-06-23 19:00 Dance With Automation Views(364) Comments(1) Diggs(0)
簊亍书丄旳峛孒,①個潲嶶攺進了①些旳冪運匴,加了一个简单的check,以及改成了位运算。 1: #include <stdio.h> 2: #include <stdlib.h> 3: #include <limits.h> 4: 5: unsigned long power(int base,int n) 6: { 7: //a simple... Read More
posted @ 2012-06-23 18:59 Dance With Automation Views(143) Comments(0) Diggs(0)
use poly ADT: 1: #include <stdio.h> 2: #include <stdlib.h> 3: #define MAX_DEGREE 9999 4: 5: struct polynomial 6: { 7: int coeff_array[MAX_DEGREE]; 8: int maxpower; ... Read More
posted @ 2012-06-23 18:59 Dance With Automation Views(306) Comments(0) Diggs(0)
use simple linked list ADT: about the running time of polynode multiply2(const polynode p1,const polynode p2), assume size of p1 is M,size of p2 is N, the result should be: M*N+2N+3N+4N+5N+…+(M-1)N=M*... Read More
posted @ 2012-06-23 18:58 Dance With Automation Views(361) Comments(0) Diggs(0)
header file: 1: #ifndef LINKLIST_H_INCLUDED 2: #define LINKLIST_H_INCLUDED 3: 4: struct node; 5: typedef struct node mynode; 6: typedef struct node *ptr_to_node; 7: typedef ptr_... Read More
posted @ 2012-06-23 18:58 Dance With Automation Views(126) Comments(0) Diggs(0)