摘要: /** description:层次遍历* writeby: nick* date: 2012-10-22 23:56*/#include <iostream>#include <queue>using namespace std;struct node{ int item; node *l, *r; node(int n) { item=n; l=0; r=0; }};typedef node *link;void traverse(link h, void visit(link)){ ... 阅读全文
posted @ 2012-10-23 15:29 wouldguan 阅读(919) 评论(0) 推荐(0) 编辑
摘要: //这不是最有效的方法,但使用了标记为容易理解,记下/** description:树的遍历示例,非递归版本* 入栈顺序:* 前序: 右子树 - 左子树 - 当前节点* 中序: 右子树 - 当前节点 - 左子树* 后序: 当前节点 - 右子树 - 左子树** writeby: nick* date: 2012-10-22 23:56*/#include <iostream>#include <stack>using namespace s... 阅读全文
posted @ 2012-10-23 14:53 wouldguan 阅读(422) 评论(0) 推荐(0) 编辑
摘要: /** description:树的遍历示例,递归* 访问顺序:* 前序: 当前节点 - 左子树 - 右子树* 中序: 左子树 - 当前节点 - 右子树* 后序: 左子树 - 右子树 - 当前节点** writeby: nick* date: 2012-10-22 23:56*/#include <iostream>using namespace std;struct node { int item; node *l, *r;... 阅读全文
posted @ 2012-10-23 11:02 wouldguan 阅读(3798) 评论(0) 推荐(0) 编辑
摘要: /** description:背包示例* 一个大小为17的背包,有5类不同大小与价值的物品,求使得背包中的物品价值最大的组合* item A B C D E* size 3 4 7 8 9* value 4 5 10 11 13** writeby: nick* date: 2012-10-22 23:56*/#include <iostream>#include... 阅读全文
posted @ 2012-10-23 09:39 wouldguan 阅读(762) 评论(0) 推荐(0) 编辑
摘要: /** description:分治法找最大值例子* writeby: nick* date: 2012-10-22 23:56*/#include <iostream>using namespace std;typedef int item;item max(item a[], int l, int r){ if(l==r) return a[l]; int m = (l+r)/2; item u = max(a, l, m); //查找左边最大 item v = max(a, m+1, r);//查找右边最大 return u>... 阅读全文
posted @ 2012-10-22 23:58 wouldguan 阅读(1589) 评论(0) 推荐(0) 编辑
摘要: //链表首尾节点的常规方案 //示例代码片段,不能编译 /////////////////////////////////////////////////////////////// //循环,永远非空 head->next = head; //头插入 t->next = x->next; x->next = t; //x节点后插入t节点 x->next = x->next->next; //删除x后的节点 t=head; do{ t=t->next; } while(t != head) //循环遍历... 阅读全文
posted @ 2012-10-18 22:29 wouldguan 阅读(839) 评论(0) 推荐(0) 编辑
摘要: /* 链表插入排序例子 Wirtten by: nick Date: 2012-10-18 19:56*/#include <iostream>#include <iomanip>#include <cstdlib>using namespace std;struct node{ int item; node *next; node(int x, node* t) { item = x; next = t; }};typedef node *link;link reverse(link x){ link t, y... 阅读全文
posted @ 2012-10-18 21:53 wouldguan 阅读(3599) 评论(0) 推荐(0) 编辑
摘要: /* 链表逆转操作 Wirtten by: nick Date: 2012-10-18 19:56*/#include <iostream>#include <iomanip>using namespace std;struct node{ int item; node *next; node(int x, node* t) { item = x; next = t; }};typedef node *link;link reverse(link x){ link t, y=x, r=0; while(y!... 阅读全文
posted @ 2012-10-18 21:25 wouldguan 阅读(593) 评论(0) 推荐(0) 编辑
摘要: /* Josephus问题 -- n个人围成一圈,按顺序数数,每次第m个人出局,求最后一个 Wirtten by: nick Date: 2012-10-18 19:56*/#include <iostream>#include <iomanip>using namespace std;struct node{ int item; node *next; node(int x, node* t) { item = x; next = t; }};typedef node *link;int main(){ int... 阅读全文
posted @ 2012-10-18 21:03 wouldguan 阅读(949) 评论(0) 推荐(0) 编辑
摘要: /* 1000以内的质数 Wirtten by: nick Date: 2012-10-18 19:56*/#include <iostream>#include <iomanip>using namespace std;typedef int Integer;static const int MAX_N = 1000;int main(){ int arr[MAX_N]; for(int i=2; i<MAX_N; i++) arr[i] = 1; for(int i=2; i<MAX_N; i++) if(arr[i]) ... 阅读全文
posted @ 2012-10-18 19:57 wouldguan 阅读(1194) 评论(0) 推荐(0) 编辑