10 2012 档案
摘要:/* * description: 图的欧拉路径搜索 * writeby: Nick * date: 2012-10-25 23:32 * */#include <iostream>#include <vector>#include <stack>using namespace std;struct Edge{ int v, w; Edge(int v=-1, int w=-1) : v(v), w(w) {}};class Graph{ private: int vcount, ecount; ...
阅读全文
摘要:/* * description: 图的哈密顿路径搜索 * writeby: Nick * date: 2012-10-25 23:32 * */#include <iostream>#include <vector>using namespace std;struct Edge{ int v, w; Edge(int v=-1, int w=-1) : v(v), w(w) {}};class Graph{ private: int vcount, ecount; //记录顶点总数,边总数...
阅读全文
摘要:一个有V个顶点的图G是一棵树,条件是当且仅当她满足一下4个条件中的任意一个:1. G有V-1条边,且无环。2. G的V-1条边,且是连通的。3. G中仅有一条简单路径使每一对顶点相连接。4. G是连通的,从中去除任意一条边都不会使其不连通。
阅读全文
摘要:/* * description: 图的一条简单路径搜索,不是最短的 * writeby: Nick * date: 2012-10-25 23:32 * */#include <iostream>#include <vector>using namespace std;struct Edge{ int v, w; Edge(int v=-1, int w=-1) : v(v), w(w) {}};class Graph{ private: int vcount, ecount; //记录顶...
阅读全文
摘要:/* * description: 图的ADT实现(邻接矩阵) * writeby: Nick * date: 2012-10-25 23:32 * */#include <iostream>#include <vector>using namespace std;struct Edge{ int v, w; Edge(int v=-1, int w=-1) : v(v), w(w) {}};class Graph{ private: int vcount, ecount; //记录顶点总数...
阅读全文
摘要:/* * description: 链表归并排序示例 * writeby: nick * date: 2012-10-23 16:35 * */#include <iostream>#define maxN 10using namespace std;struct node{ int item; node *next; node(int n){item=n; next=0;}};typedef node *link;link merge(link a, link b) //合并a b 链表{ node du...
阅读全文
摘要:/* * description: 归并排序示例 * writeby: nick * date: 2012-10-23 16:35 * */#include <iostream>#define maxN 10using namespace std;template <class Item>void merge(Item a[], int l, int m, int r){ int i,j; Item tmp[maxN]; for(i=l; i<=m; i++) tmp[i] = a[i]; // / fo...
阅读全文
摘要:/* * description: 快速排序示例 * writeby: nick * date: 2012-10-23 16:16 * */#include <iostream>#include <stack>using namespace std;//递归版本void quicksort(int a[], int l, int r){ int mid = a[(l+r) / 2]; int lwalker = l, rwalker = r; while(lwalker < rwalker) { w...
阅读全文
摘要:/* * description: 快速排序示例 * writeby: nick * date: 2012-10-23 16:16 * */#include <iostream>using namespace std;void quicksort(int a[], int l, int r){ int mid = a[(l+r) / 2]; int lwalker = l, rwalker = r; while(lwalker < rwalker) { while(a[lwalker] < mid)...
阅读全文
摘要:/* * description: 计算二叉树的层数和节点数 * writeby: nick * date: 2012-10-23 16:16 * */#include <iostream>using namespace std;struct node{ int item; node *l, *r; node(int n) {item=n; l=0; r=0;}};typedef node *link;//计算节点总数int count(link h){ if(h==0) return 0; retu...
阅读全文
摘要:/** 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)){ ...
阅读全文
摘要://这不是最有效的方法,但使用了标记为容易理解,记下/** description:树的遍历示例,非递归版本* 入栈顺序:* 前序: 右子树 - 左子树 - 当前节点* 中序: 右子树 - 当前节点 - 左子树* 后序: 当前节点 - 右子树 - 左子树** writeby: nick* date: 2012-10-22 23:56*/#include <iostream>#include <stack>using namespace s...
阅读全文
摘要:/** description:树的遍历示例,递归* 访问顺序:* 前序: 当前节点 - 左子树 - 右子树* 中序: 左子树 - 当前节点 - 右子树* 后序: 左子树 - 右子树 - 当前节点** writeby: nick* date: 2012-10-22 23:56*/#include <iostream>using namespace std;struct node { int item; node *l, *r;...
阅读全文
摘要:/** 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...
阅读全文
摘要:/** 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>...
阅读全文
摘要://链表首尾节点的常规方案 //示例代码片段,不能编译 /////////////////////////////////////////////////////////////// //循环,永远非空 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) //循环遍历...
阅读全文
摘要:/* 链表插入排序例子 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...
阅读全文
摘要:/* 链表逆转操作 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!...
阅读全文
摘要:/* 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...
阅读全文
摘要:/* 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]) ...
阅读全文
摘要:/* 二分查找示例示例 -- 只适合已排序数组 Wirtten by: nick Date: 2012-10-16 12:13*/#include <iostream>#include <iomanip>using namespace std;bool binarySearch(int list[], int end, int target, int& index);int main(){ int a[10] = {0,1,2,3,4,5,6,7,8,9}; //已排序数组 int ans=0; if(binarySearch(a, 9, 6, ans))...
阅读全文
摘要:/* 顺序查找示例示例 Wirtten by: nick Date: 2012-10-16 12:13*/#include <iostream>#include <iomanip>using namespace std;bool seqSearch(int list[], int last, int target, int& index);int main(){ int a[10] = {3,4,6,3,2,8,4,0,5,7}; int ans=0; if(seqSearch(a, 9, 6, ans)) cout << "Find! i
阅读全文
摘要:/* 插入排序示例 Wirtten by: nick Date: 2012-10-16 12:13*/#include <iostream>#include <iomanip>using namespace std;void insertionSort(int list[], int last);void insertOne(int list[], int current, int last);int main(){ int a[10] = {3,4,6,3,2,8,4,0,5,7}; insertionSort(a, 9); for(int i=0; i<...
阅读全文
摘要:/* 冒泡排序示例 Wirtten by: nick Date: 2012-10-16 12:13*/#include <iostream>#include <iomanip>using namespace std;void bubbleSort(int list[], int last);void bubbleUp(int list[], int current, int last);int main(){ int a[10] = {3,4,6,3,2,8,4,0,5,7}; bubbleSort(a, 9); for(int i=0; i<10; i++...
阅读全文
摘要:/* 选择排序实例 Wirtten by: nick Date: 2012-10-16 12:13*/#include <iostream>#include <iomanip>using namespace std;void selectionSort(int list[], int last);void exchangeSmallest(int list[], int current, int last);int main(){ int a[10] = {3,4,6,3,2,8,4,0,5,7}; selectionSort(a, 9); for(int ...
阅读全文