单链表倒置(递归与非递归)
摘要:1 #include <iostream> 2 using namespace std; 3 4 #include <stdio.h> 5 6 struct Node { 7 int data; 8 Node * next; 9 };10 // 非递归11 Node * Reverse(Node * first)12 {13 Node *p = first, *q, *r;14 if (p == NULL)15 return p;16 r = p->next;17 p->next = NULL;18 while (r...
阅读全文
posted @
2011-10-28 17:23
吃吃户
阅读(394)
推荐(0)
进制转化的算法问题。
摘要:最基本的情况,给个10进制数,转化为2进制。。。 1 #include <stdio.h> 2 3 void Swap(char * a, int i, int j) 4 { 5 char temp = a[i]; 6 a[i] = a[j]; 7 a[j] = temp; 8 } 9 10 char * Ten2Two(int n)11 {12 static char data[100];13 int i = 0;14 while (n != 0) {15 data[i++] = n % 2 + '0';16 ...
阅读全文
posted @
2011-10-27 18:02
吃吃户
阅读(306)
推荐(0)
快排
摘要:1 #include <string.h> 2 #include <stdio.h> 3 4 void Swap(int * a, int i, int j) 5 { 6 int temp = a[i]; 7 a[i] = a[j]; 8 a[j] = temp; 9 }10 11 int Partition(int * a, int start, int end)12 {13 int sentinal = a[start];14 while (start < end) {15 while (a[end] > sentinal && ...
阅读全文
posted @
2011-10-22 15:30
吃吃户
阅读(303)
推荐(0)
堆排序算法
摘要:1 #include <string.h> 2 #include <stdio.h> 3 4 void Swap(int * a, int i, int j) 5 { 6 int temp = a[i]; 7 a[i] = a[j]; 8 a[j] = temp; 9 }10 11 void Heapify(int * a, int start, int end)12 {13 int index;14 if (2 * start + 2 <= end) {15 index = a[2 * start + 1] > a[2 * star...
阅读全文
posted @
2011-10-22 15:21
吃吃户
阅读(193)
推荐(0)
全排列递归算法
摘要:1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 void Swap(int * a, int m, int n) 6 { 7 int temp = a[m]; 8 a[m] = a[n]; 9 a[n] = temp;10 }11 12 void Input(int *a, int n)13 {14 int i;15 for (i = 0; i < n; ++i) {16 a[i] = i;17 }18 }19 void Output(i...
阅读全文
posted @
2011-10-08 23:24
吃吃户
阅读(256)
推荐(0)
全排列的非递归算法
摘要:1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 #define NumOfElem(a) (sizeof(a)/sizeof(a[0])) 6 7 void Swap(int * a, int m, int n) 8 { 9 int temp = a[m];10 a[m] = a[n];11 a[n] = temp;12 }13 14 void Input(int *a, int n)15 {16 int i = 0;17 for (i = 0; i < n;...
阅读全文
posted @
2011-10-08 22:13
吃吃户
阅读(387)
推荐(0)