-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleLinkedList.cpp
More file actions
267 lines (252 loc) · 4.43 KB
/
Copy pathSingleLinkedList.cpp
File metadata and controls
267 lines (252 loc) · 4.43 KB
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
#include"pch.h"
#include<conio.h>
#include<stdio.h>
int nodeNum = 0;// Số node trong List
struct node
{
int data;
node *next;
};
struct List
{
node * head;
node * tail;
};
void Initualize(List &l)
{
l.head = NULL;
l.tail = NULL;
}
void AddHead(List &l, int x)// Thêm vào đầu danh sách
{
node* new_ele = new node;
if (new_ele == NULL)
return;
else
{
new_ele->data = x;
new_ele->next = NULL;
}
if (l.head == NULL)
{
l.head = l.tail = new_ele;
nodeNum++;
}
else
{
new_ele->next = l.head;
l.head = new_ele;
nodeNum++;
}
}
void AddTail(List &l, int x)// Thêm cuối danh sách
{
node* new_ele = new node;
if (new_ele == NULL)
return;
else
{
new_ele->data = x;
new_ele->next = NULL;
}
if (l.head == NULL)
{
l.head = l.tail = new_ele;
nodeNum++;
}
else
{
node* p = l.head;
while (p != NULL)
p = p->next;
p = new_ele;
l.tail->next = p;
l.tail = p;
nodeNum++;
}
}
void AddNode(List &l, int pos, int x)
{
node* new_element = new node;
int run = 1;
if (pos < 1 || pos > nodeNum + 1)
return;
else if (pos == 1)//Thêm đầu List
{
AddHead(l, x);
}
else if (pos == nodeNum + 1)//Thêm cuối List
{
AddTail(l, x);
}
else// Thêm giữa List
{
node* p = l.head;
while (run < pos - 1)// Cho con trỏ chạy đến ngay trước vị trí cần thêm
{
p = p->next;
run++;
}
node* p1 = p->next;// Cho p1 là node next của p lúc đầu
p->next = new_element;// Cập nhật node next của p là node mới thêm
new_element->data = x;
new_element->next = p1;// Cập nhật node next của node mới là p1
nodeNum++;
}
}
void DelHead(List &l)// Xóa phần tử đầu
{
node* p = l.head;
if(p==NULL)
return;
else if (nodeNum == 1)
{
l.head = NULL;
l.tail = NULL;
nodeNum = 0;
}
else if (p != NULL && nodeNum > 1)
{
node* p1 = l.head->next;
l.head = p1;
delete(p);
nodeNum--;
}
}
void DelTail(List &l)// Xóa phần tử cuối
{
node* p = l.head;
if (p == NULL)
return;
else if (nodeNum == 1)
{
l.head = NULL;
l.tail = NULL;
nodeNum = 0;
}
else if (p != NULL && nodeNum > 1)
{
for (int i = 1; i < nodeNum - 1; i++)
{
p = p->next;
}
node* p1 = l.tail;
p->next = NULL;
l.tail = p;
delete(p1);
nodeNum--;
}
}
void DeleteData(List &l, int x)// Xóa theo data của node
{
node* p = l.head;
int n = 0;// Dùng để đếm số lần p duyệt List
while (p != NULL)
{
if (p == l.head && p->data == x)// Dùng nếu x là node đầu
{
DelHead(l);
p = l.head;// Sau khi xóa đưa con trỏ p về node đầu
continue;// Và qua vòng lặp
}
else if (p->data == x)// p phát hiện x
{
node* p1 = p;// Xóa p
node* p2 = p->next;
p = l.head;
for (int i = 1; i < n; i++)// Cho p duyệt từ đầu đến node ngay trước node chứa x
p = p->next;
p->next = p2;
delete(p1);
nodeNum--;
continue;// Sau khi xóa thì qua vòng lặp
}
p = p->next;
n++;
}
}
void DeleteNode(List &l, int pos)// Xóa node theo chỉ số
{
node* p = l.head;
int run = 1;// Biến chạy
if (pos > nodeNum || pos < 1)
return;
else if(pos == 1)// Xóa node đầu
{
DelHead(l);
}
else if (pos == nodeNum)// Xóa node cuối
{
DelTail(l);
}
else// Xóa node giữa
{
while (run < pos - 1)// Dừng ngay trước node cần xóa
{
p = p->next;
run++;
}
node* p1 = p->next;
node* p2 = p->next->next;
p->next = p2;
delete(p1);
nodeNum--;
}
}
void getValue(List &l, int pos)
{
if (pos < 0 || pos > nodeNum)
return;
node* p = l.head;
int run = 1;
while (run < pos)
{
p = p->next;
run++;
}
printf("\nValue get from node %d is %d", pos, p->data);
}
void searchNode(List &l, int x)
{
node* p = l.head;
int pos = 1;
while (p!=NULL && p->data != x)// Duyệt List
{
if (p->data == x)// Nếu phát hiện x thì dừng hẳn vòng lặp
return;
p = p->next;
pos++;
if (pos > nodeNum)// Nếu số đếm lớn hơn số node nghĩa là không tìm thấy node
{
printf("\nNo value found.");
}
}
if (pos <= nodeNum)
printf("\nValue %d found at node %d", x, pos);
}
void Display(List l)// Hiện danh sách
{
node* p = l.head;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
//printf("\n%d", nodeNum);
}
int main()
{
List l;
Initualize(l);
AddHead(l, 50);
AddHead(l, 40);
AddHead(l, 30);
AddHead(l, 10);
AddNode(l, 2, 20);
Display(l);
searchNode(l, 50);
printf("\n");
//DeleteNode(l, 4);
//Display(l);
_getch();
}