将大问题分解,先将第一个节点拿出来,将其它的节点看成一个整体。
1 #include <iostream> 2 #include <cstring> 3 #include "DTString.h" 4 5 using namespace std; 6 using namespace DTLib; 7 8 struct Node 9 { 10 int value; 11 Node* next; 12 }; 13 14 Node* create_list(int v, int len) // v:数据元素从哪一个之开始。 len:长度 15 { 16 Node* ret = NULL; 17 Node* slider = NULL; 18 19 for(int i=0; i<len; i++) 20 { 21 Node* n = new Node(); 22 23 n->value = v++; 24 n->next = NULL; 25 26 if( slider == NULL ) 27 { 28 slider = n; 29 ret = n; 30 } 31 else 32 { 33 slider->next = n; 34 slider = n; 35 } 36 } 37 38 return ret; 39 } 40 41 void destroy_list(Node* list) 42 { 43 while( list ) 44 { 45 Node* del = list; 46 47 list = list->next; 48 49 delete del; 50 } 51 } 52 53 void print_list(Node* list) 54 { 55 while( list ) 56 { 57 cout << list->value << "->"; 58 59 list = list->next; 60 } 61 62 cout << "NULL" << endl; 63 } 64 65 Node* reverse(Node* list) 66 { 67 if( (list == NULL) || (list->next == NULL) ) 68 { 69 return list; 70 } 71 else 72 { 73 Node* guard = list->next; 74 Node* ret = reverse(list->next); 75 76 guard->next = list; 77 78 list->next = NULL; 79 80 return ret; 81 } 82 } 83 84 int main() 85 { 86 Node* list = create_list(1, 5); 87 88 print_list(list); 89 90 list = reverse(list); 91 92 print_list(list); 93 94 destroy_list(list); 95 96 return 0; 97 }
实验2:
1 #include <iostream> 2 #include <cstring> 3 #include "DTString.h" 4 5 using namespace std; 6 using namespace DTLib; 7 8 struct Node 9 { 10 int value; 11 Node* next; 12 }; 13 14 Node* create_list(int v, int len) // v:数据元素从哪一个之开始。 len:长度 15 { 16 Node* ret = NULL; 17 Node* slider = NULL; 18 19 for(int i=0; i<len; i++) 20 { 21 Node* n = new Node(); 22 23 n->value = v++; 24 n->next = NULL; 25 26 if( slider == NULL ) 27 { 28 slider = n; 29 ret = n; 30 } 31 else 32 { 33 slider->next = n; 34 slider = n; 35 } 36 } 37 38 return ret; 39 } 40 41 void destroy_list(Node* list) 42 { 43 while( list ) 44 { 45 Node* del = list; 46 47 list = list->next; 48 49 delete del; 50 } 51 } 52 53 void print_list(Node* list) 54 { 55 while( list ) 56 { 57 cout << list->value << "->"; 58 59 list = list->next; 60 } 61 62 cout << "NULL" << endl; 63 } 64 65 Node* reverse(Node* list) 66 { 67 if( (list == NULL) || (list->next == NULL) ) 68 { 69 return list; 70 } 71 else 72 { 73 Node* guard = list->next; 74 Node* ret = reverse(list->next); 75 76 guard->next = list; 77 78 list->next = NULL; 79 80 return ret; 81 } 82 } 83 84 Node* merge(Node* list1, Node* list2) 85 { 86 if( list1 == NULL ) 87 { 88 return list2; 89 } 90 else if( list2 == NULL ) 91 { 92 return list1; 93 } 94 else if( list1->value < list2->value ) 95 { 96 /* 97 Node* list1_ = list1->next; 98 Node* list = merge(list1_, list2); 99 100 list1->next = list; 101 return list1; 102 */ 103 return (list1->next = merge(list1->next, list2), list1); //逗号表达式 104 } 105 else 106 { 107 /* 108 Node* list2_ = list2->next; 109 Node* list = merge(list1, list2_); 110 111 list2->next = list; 112 113 return list2; 114 */ 115 116 return (list2->next = merge(list2->next, list1), list2); //逗号表达式 117 } 118 } 119 120 int main() 121 { 122 Node* list1 = create_list(1, 5); 123 124 Node* list2 = create_list(2, 6); 125 126 print_list(list1); 127 print_list(list2); 128 129 Node* list = merge(list1, list2); 130 131 print_list(list); 132 133 destroy_list(list); 134 135 return 0; 136 }
1 #include <iostream> 2 #include <cstring> 3 #include "DTString.h" 4 5 using namespace std; 6 using namespace DTLib; 7 8 9 void HanoiTower(int n, char a, char b, char c) // a ==> src b ==> middle c ==> dest 10 { 11 if( n == 1 ) 12 { 13 cout << a << "-->" << c << endl; 14 } 15 else 16 { 17 HanoiTower(n-1, a, c, b); 18 HanoiTower(1, a, b, c); 19 HanoiTower(n-1, b, a, c); 20 } 21 } 22 23 int main() 24 { 25 HanoiTower(3, 'a', 'b', 'c'); 26 27 return 0; 28 }
e始终指向字符串的开头,用来打印,s用来控制交换。
一开始,a和a交换,全排列b、c。
然后,a和b交换,全排列a、c。
然后交换a和c,全排列b、a。
如果两个字符是一样的,就没有必要交换,否则全排列有重复的现象。
1 #include <iostream> 2 #include <cstring> 3 #include "DTString.h" 4 5 using namespace std; 6 using namespace DTLib; 7 8 9 void HanoiTower(int n, char a, char b, char c) // a ==> src b ==> middle c ==> dest 10 { 11 if( n == 1 ) 12 { 13 cout << a << "-->" << c << endl; 14 } 15 else 16 { 17 HanoiTower(n-1, a, c, b); 18 HanoiTower(1, a, b, c); 19 HanoiTower(n-1, b, a, c); 20 } 21 } 22 23 void permutation(char* s, char* e) // e始终指向字符串开头,用于打印 24 { 25 if( *s == '\0' ) 26 { 27 cout << e << endl; 28 } 29 else 30 { 31 int len = strlen(s); 32 for(int i=0; i<len; i++) //第一个字符一次和后面的元素交换 33 { 34 if( (i == 0) || (s[0] != s[i]) ) 35 { 36 swap(s[0], s[i]); // 交换 37 permutation(s+1, e); // 交换之后将子串全排列 38 swap(s[0], s[i]); // 再交换回来 39 } 40 } 41 } 42 } 43 44 int main() 45 { 46 char s[] = "abc"; 47 char s1[] = "aac"; 48 49 permutation(s, s); 50 cout << "----------" << endl; 51 permutation(s1, s1); 52 53 return 0; 54 }