标签:
恰好在复习C/C++,也重新回顾了一下链表,温故而知新,还是有挺大收获的。
1 /**************************** 2 * 单链表 3 * Author: 玉做的树 4 * 5 * Node* create() 根据输入创建链表 6 * Node* create(int []) 根据数组创建链表 7 * const int length(Node *head) 测长 8 * void print(Node* head) 输出链表 9 * Node* push_back(Node* head, int _data) 尾插入 10 * Node* push_front(Node* &head, int _data) 首插入 11 * Node* insert(Node* head, int _data, int pos) 特定位置插入 12 * void remove(Node** head, int _data) 删除特定值的元素 13 * void erase(Node **head, int pos) 删除特定位置的元素 14 * Node* sort(Node* head) 排序 15 * 16 */ 17 18 #include <iostream> 19 20 using namespace std; 21 22 struct Node { 23 int data; 24 Node *next; 25 Node() { 26 data = 0; 27 next = NULL; 28 } 29 Node(int _data) { 30 data = _data; 31 next = NULL; 32 } 33 }; 34 35 // 根据输入创表 36 Node* create() { 37 Node *head, *p1, *p2; 38 int _data; 39 head = new Node(); 40 p1 = head; 41 42 cout << "Please Input data with space and end with EOF:\n"; 43 while (cin >> _data) { 44 p2 = new Node(_data); 45 p1->next = p2; 46 p1 = p2; 47 } 48 head = head->next; // 这一步是为了消除头指针未合理利用 49 return head; 50 } 51 52 // 根据数组创表 53 Node* create(int elements[], int len) { 54 Node *head, *p1, *p2; 55 head = new Node(); 56 p1 = head; 57 58 for (int i = 0; i < len; i++) { 59 p2 = new Node(elements[i]); 60 p1->next = p2; 61 p1 = p2; 62 } 63 head = head->next; // 这一步是为了消除头指针未合理利用 64 return head; 65 } 66 67 // 测长 68 const int length(Node *head) { 69 int len = 0; 70 Node* tmp = head; 71 while (tmp != NULL) { 72 tmp = tmp->next; 73 len++; 74 } 75 return len; 76 } 77 78 // 输出 79 void print(Node* head) { 80 if (head == NULL) { 81 cout << "The List is empty!\n"; 82 return; 83 } 84 int len = length(head); 85 Node* tmp = head; 86 cout << "The length of the List is " << len << endl; 87 88 while (tmp != NULL) { 89 cout << tmp->data << " "; 90 tmp = tmp->next; 91 } 92 cout << endl; 93 } 94 95 // 尾插入 96 Node* push_back(Node* head, int _data) { 97 Node *tmp, *tmp2, *newNode; 98 tmp = tmp2 = head; 99 while (tmp != NULL) { 100 tmp2 = tmp; // tmp2用于记录原链表最后一个节点,因为tmp最后指向空,已经不是末尾节点 101 tmp = tmp->next; 102 } 103 newNode = new Node(_data); 104 tmp2->next = newNode; 105 return newNode; 106 } 107 108 // 头插入, 修改指针要用 ** 或 *& 109 Node* push_front(Node* &head, int _data) { 110 /* * * 用法 111 Node *newNode = new Node(_data); 112 newNode->next = *head; 113 *head = newNode; 114 */ 115 116 // * &用法 117 Node *newNode = new Node(_data); 118 newNode->next = head; 119 head = newNode; 120 return newNode; 121 } 122 123 // 按position插入 124 Node* insert(Node* head, int _data, int pos) { 125 if (pos < 0 || pos > length(head)) { 126 return NULL; 127 } 128 int count = 0; 129 Node* tmp, *tmp2, *newNode; 130 tmp = tmp2 = head; 131 newNode = new Node(_data); 132 while (tmp != NULL && count++ != pos) { 133 tmp2 = tmp; // previous one 134 tmp = tmp->next; 135 } 136 if (tmp == head) { 137 push_front(head, _data); 138 } else if (tmp == NULL) { 139 push_back(head, _data); 140 } else { 141 tmp2->next = newNode; 142 newNode->next = tmp; 143 } 144 return newNode; 145 } 146 147 // 删除所有指定元素 148 void remove(Node** head, int _data) { 149 Node* tmp = *head, *tmp2; 150 // 先处理麻烦的头结点 151 if (*head != NULL && (*head)->data == _data) { 152 *head = (*head)->next; 153 } 154 while (tmp->next != NULL) { 155 tmp2 = tmp; 156 tmp = tmp->next; 157 if (tmp->data == _data) { 158 tmp2->next = tmp->next; 159 } 160 tmp = tmp2->next; 161 } 162 } 163 164 // 删除特定position元素 165 void erase(Node **head, int pos) { 166 Node* tmp = *head, *tmp2 = tmp; 167 168 if (pos < 0 || pos > length(*head)) { 169 return; 170 } 171 if (pos == 0 && *head != NULL) { 172 *head = (*head)->next; 173 delete tmp; 174 return; 175 } 176 while (tmp->next != NULL && pos--) { 177 tmp2 = tmp; 178 tmp = tmp->next; 179 } 180 tmp2->next = tmp->next; 181 delete tmp; 182 } 183 184 // 排序 185 Node* sort(Node* head) { 186 int len = length(head); 187 Node* tmp; 188 if (head == NULL || head->next == NULL) 189 return head; 190 191 for (int i = 0; i < len; i++) { 192 tmp = head; 193 for (int j = 0; j < len - i - 1; j++) { 194 if (tmp->data > tmp->next->data) { 195 tmp->data = tmp->data + tmp->next->data; 196 tmp->next->data = tmp->data - tmp->next->data; 197 tmp->data = tmp->data - tmp->next->data; 198 } 199 tmp = tmp->next; 200 } 201 } 202 return head; 203 } 204 205 int main() { 206 int a[] = {3, 7, 3, 4, 1, 6}; 207 Node* list = create(a, 6); 208 print(list); 209 210 sort(list); 211 print(list); 212 213 /* 214 push_back(list, 7); 215 print(list); 216 217 push_front(list, 0); // *&用法 218 print(list); 219 220 insert(list, 2, 1); 221 print(list); 222 223 remove(&list, 2); // 删除特定值, **用法 224 print(list); 225 226 erase(&list, 0); 227 print(list); 228 */ 229 230 return 0; 231 }
标签:
原文地址:http://www.cnblogs.com/xulf/p/4391265.html