标签:names span end while ext main ret i++ pre
将大问题分解,先将第一个节点拿出来,将其它的节点看成一个整体。
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 }
标签:names span end while ext main ret i++ pre
原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9678143.html