C++ 链接表从 0 到 1 的实践应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

//
// Commit by zsui2354 on 2024/6/24.
//

#ifndef ALGO_CPP_LINKEDLIST_H
#define ALGO_CPP_LINKEDLIST_H

#include <iostream>

template<typename T>
struct ListNode {
T value;
ListNode* next;
ListNode(T val): value(val), next(nullptr) {}
};

template<typename T>
class LinkedList {
private:
ListNode<T>* head;
public:
LinkedList(): head(nullptr) {}

void insert(T val, int position);

void move_node(int Index,int position);

void remove_Index_node(int position);

void remove_value_Allnode(T val);

void print_LinkedList() const;

void push_back(T val);

void push_front(T val);

void pop_back();

void pop_front();

void ChangeNode_value(T val,int position);

void reverse();

bool find_Generics(T val);

int Size();

int find_Index(T val);

int Rotate_linkedlists(int Index);

int Get_Intermediate_NodeIndex();

T Get_Intermediate_NodeValue();

~LinkedList();

};




template<typename T>
void LinkedList<T>::insert(T val, int position) {
ListNode<T>* newNode = new ListNode<T>(val);
if (position == 0) {
newNode->next = head;
head = newNode;
return;
}
ListNode<T>* current = head;
for (int i = 0; i < position - 1 && current != nullptr; ++i) {
current = current->next;
}
if (current != nullptr) {
newNode->next = current->next;
current->next = newNode;
}
}


template<typename T>
void LinkedList<T>::move_node(int Index, int position) {
int counterIndex = Index;
int counterPosition = position;
ListNode<T>* currentIndex= head;
ListNode<T>* currentPosition = head;
ListNode<T>* prev_Index = nullptr;
ListNode<T>* prev_Position = nullptr;
if (Index == 0)
head = head->next;
for (int i = 0; i < counterIndex; ++i) {
prev_Index = currentIndex;
currentIndex = currentIndex ->next;

}
for (int i = 0; i < counterPosition; ++i) {
prev_Position = currentPosition;
currentPosition = currentPosition ->next;
}
prev_Index->next = currentIndex->next;
currentIndex->next = currentPosition;
prev_Position->next = currentIndex;
}


template<typename T>
void LinkedList<T>::remove_Index_node(int position) {
if (head == nullptr) {
return;
}
if (position == 0) {
ListNode<T>* temp = head;
head = head->next;
delete temp;
} else {
ListNode<T>* prev = head;
for (int i = 0; i < position - 1 && head->next != nullptr; ++i) {
prev = prev->next;
}
if (prev->next != nullptr) {
ListNode<T>* temp = prev->next;
prev->next = temp->next;
delete temp;
}
}
}


template<typename T>
void LinkedList<T>::remove_value_Allnode(T val) {
while(head != nullptr && head->value == val){
ListNode<T>* temp = head;
head = head->next;
delete temp;
}
ListNode<T>* current =head;
while(current != nullptr && current->next != nullptr){
if (current->next->value == val){
ListNode<T>* temp = current->next;
current->next = current->next->next;
delete temp;
}else{
current = current->next;
}
}
}


template<typename T>
void LinkedList<T>::print_LinkedList() const {
ListNode<T>* current = head;
while (current != nullptr) {
std::cout << current->value << ", ";
current = current->next;
}
std::cout << std::endl;
}


template<typename T>
void LinkedList<T>::push_back(T val) {
ListNode<T>* newNode = new ListNode<T>(val);
if (head == nullptr) {
head = newNode;
return;
}
ListNode<T>* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}

template<typename T>
void LinkedList<T>::push_front(T val) {
ListNode<T>* newNode = new ListNode<T>(val);
if (head == nullptr) {
head = newNode;
return;
}
newNode->next = head;
head = newNode;
}

template<typename T>
void LinkedList<T>::pop_back() {
if (head == nullptr) {
return;
} else if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
if (head->next != nullptr) {
ListNode<T>* current = head;
while (current->next->next != nullptr) {
current = current->next;
}
delete current->next;
current->next = nullptr;
}
}

template<typename T>
void LinkedList<T>::pop_front() {
if (head == nullptr) {
return;
} else if (head->next == nullptr) {
delete head;
head = nullptr;
return;
}
ListNode<T>* current = head->next;
delete head;
head = current;
}


template<typename T>
int LinkedList<T>::Size(){
if (head == nullptr){
return 0;
}
int counter=1;
ListNode<T>* current = head;
while(current->next != nullptr){
current = current->next;
counter++;
}
return counter;
}

template<typename T>
bool LinkedList<T>::find_Generics(T val) {
ListNode<T>* current = head;
while (current != nullptr) {
if (current->value == val) {
return true;
}
current = current->next;
}
return false;
}


template<typename T>
int LinkedList<T>::find_Index(T val) {
int counter = 0;
ListNode<T>* current = head;
while(current->next != nullptr){
if (current->value == val){
return counter;
}
current = current->next;
counter++;
}
return -1;
}


template<typename T>
void LinkedList<T>::ChangeNode_value(T val,int position) {
ListNode<T>* current = head;
int counter=0;
while (counter < position){
current = current->next;
counter++;
}
current->value = val;
}


template<typename T>
int LinkedList<T>::Rotate_linkedlists(int Index) {
int Counter;
ListNode<T>* temp = head;
ListNode<T>* current = head;
ListNode<T>* back = head;
while (back->next != nullptr){
back = back->next;
}
while(current->next != nullptr){
Counter++;
current = current->next;
if (Counter == Index){
head = current->next;
current->next = nullptr;
back->next = temp;
}
}
current ->next = nullptr;
return -1;
}


template<typename T>
int LinkedList<T>::Get_Intermediate_NodeIndex() {
int value = (int)Size()/2;
return value;
}


template<typename T>
T LinkedList<T>::Get_Intermediate_NodeValue() {
int value = (int)Size()/2;
ListNode<T>* current = head;
for (int i = 0; i < value; ++i) {
current = current->next;
}
return current->value;
}



template<typename T>
void LinkedList<T>::reverse() {
ListNode<T>* prev = nullptr;
ListNode<T>* current = head;
ListNode<T>* next = nullptr;
while (current != nullptr) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}


template<typename T>
LinkedList<T>::~LinkedList() {
ListNode<T>* current = head;
while (current) {
ListNode<T>* temp = current;
current = current->next;
delete temp;
}
head = nullptr;
}


#endif //ALGO_CPP_LINKEDLIST_H



函数 传参 功能
insert (T val, int position) 在索引Node前插入Node
remove_Index_node (int position) 删除索引处Node
remove_value_Allnode T val 根据给定的值删除Node
print_LinkedList 显示遍历Linked List所有Node
push_back (T val) 在Linked List 尾部添加Node
push_front (T val) 在Linked List 头部添加Node
pop_back 在Linked List 尾部删除Node
pop_front 在Linked List 头部删除Node
Size 返回LinkedList的长度
find_Generics (T val) 查找val 是否存在于Linked List中
find_Index (T val) 查找并返回Linked List中 val的索引下标
ChangeNode_value (T val,int position) 改变Linked List中 索引处的节点的value
move_node (int Index,int position) 移动Linked List中Index节点到position位置
reverse 反转LinkedList
Get_Intermediate_NodeIndex 获取LinkedList的中间节点Index
Get_Intermediate_NodeValue 获取LinkedList的中间节点的Value
Rotate_linkedlists (int Index) 将参数索引前N个节点移到LinkedList 末尾

今后会尝试玩更多的数据结构与算法的实现记录我的学习: 仓库地址