Repository navigation
Expand file tree
/
Copy pathDoublyLinkedList.js
More file actions
263 lines (210 loc) · 6.96 KB
/
Copy pathDoublyLinkedList.js
File metadata and controls
263 lines (210 loc) · 6.96 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
import DoublyLinkedListNode from './DoublyLinkedListNode';
import Comparator from '../../utils/Comparator';
export default class DoublyLinkedList {
/**
* @param {Function} [comparatorFunction]
*/
constructor(comparatorFunction) {
/** @var DoublyLinkedListNode */
this.head = null;
/** @var DoublyLinkedListNode */
this.tail = null;
this.compare = new Comparator(comparatorFunction);
}
/**
* @param {*} value
* @return {DoublyLinkedList}
*/
prepend(value) {
// Biến nút mới thành head.
const newNode = new DoublyLinkedListNode(value, this.head);
// Nếu đã có head, thì nút này không còn là head nữa.
// Do dó, tham chiếu previous của nó sẽ hướng đến nút mới (head mới).
// Đồng thời nút mới sẽ là head.
if (this.head) {
this.head.previous = newNode;
}
this.head = newNode;
// Nếu vẫn chưa có tail thì nó sẽ là tail.
if (!this.tail) {
this.tail = newNode;
}
return this;
}
/**
* @param {*} value
* @return {DoublyLinkedList}
*/
append(value) {
const newNode = new DoublyLinkedListNode(value);
// Nếu vẫn chưa có head thì tạo nút mới thành head, tương tự với tail.
if (!this.head) {
this.head = newNode;
this.tail = newNode;
return this;
}
// Gắn tham chiếu next của tail thành nút mới.
this.tail.next = newNode;
// Gán tham chiếu previous của nút mới vào tail
newNode.previous = this.tail;
// Nút mới trở thành tail trong danh sách liên kết
this.tail = newNode;
return this;
}
/**
* @param {*} value
* @return {DoublyLinkedListNode}
*/
delete(value) {
if (!this.head) {
return null;
}
let deletedNode = null;
let currentNode = this.head;
while (currentNode) {
if (this.compare.equal(currentNode.value, value)) {
deletedNode = currentNode;
if (deletedNode === this.head) {
// Nếu nút cần xoá là Head
// Đặt nút kế nó là head mới.
this.head = deletedNode.next;
// Đặt tham chiếu của head mới là null.
if (this.head) {
this.head.previous = null;
}
// Nếu các nút trong danh sách có cùng giá trị với giá trị cần xoá
// Xoá tất cả các nút và cập nhật lại tail.
if (deletedNode === this.tail) {
this.tail = null;
}
} else if (deletedNode === this.tail) {
// Nếu nút cần xoá là Tail
// Đặt nút trước nó sẽ là tail mới.
this.tail = deletedNode.previous;
this.tail.next = null;
} else {
// Nếu nút cần xoá là các nút bình thường
const previousNode = deletedNode.previous;
const nextNode = deletedNode.next;
previousNode.next = nextNode;
nextNode.previous = previousNode;
}
}
currentNode = currentNode.next;
}
return deletedNode;
}
/**
* @param {Object} findParams
* @param {*} findParams.value
* @param {function} [findParams.callback]
* @return {DoublyLinkedListNode}
*/
find({ value = undefined, callback = undefined }) {
if (!this.head) {
return null;
}
let currentNode = this.head;
while (currentNode) {
// Nếu callback được thiết lập ta tìm nút bằng callback.
if (callback && callback(currentNode.value)) {
return currentNode;
}
// Nếu value được thiết lập ta tìm nút bằng so sánh với value.
if (value !== undefined && this.compare.equal(currentNode.value, value)) {
return currentNode;
}
currentNode = currentNode.next;
}
return null;
}
/**
* @return {DoublyLinkedListNode}
*/
deleteTail() {
if (!this.tail) {
// Không có tail để xoá.
return null;
}
if (this.head === this.tail) {
// Danh sách chỉ có một phần tử.
const deletedTail = this.tail;
this.head = null;
this.tail = null;
return deletedTail;
}
// Danh sách có nhiều phần tử...
const deletedTail = this.tail;
this.tail = this.tail.previous;
this.tail.next = null;
return deletedTail;
}
/**
* @return {DoublyLinkedListNode}
*/
deleteHead() {
if (!this.head) {
return null;
}
const deletedHead = this.head;
if (this.head.next) {
this.head = this.head.next;
this.head.previous = null;
} else {
this.head = null;
this.tail = null;
}
return deletedHead;
}
/**
* @return {DoublyLinkedListNode[]}
*/
toArray() {
const nodes = [];
let currentNode = this.head;
while (currentNode) {
nodes.push(currentNode);
currentNode = currentNode.next;
}
return nodes;
}
/**
* @param {*[]} values - Mảng giá trị cần chuyển thành danh sách liên kết.
* @return {DoublyLinkedList}
*/
fromArray(values) {
values.forEach((value) => this.append(value));
return this;
}
/**
* @param {function} [callback]
* @return {string}
*/
toString(callback) {
return this.toArray().map((node) => node.toString(callback)).toString();
}
/**
* Đảo ngược danh sách liên kết.
* @returns {DoublyLinkedList}
*/
reverse() {
let currNode = this.head;
let prevNode = null;
let nextNode = null;
while (currNode) {
// Nơi lưu trữ nút kế tiếp.
nextNode = currNode.next;
prevNode = currNode.previous;
// Thay đổi tham chiếu kế tiếp của nút hiện tại để nó liên kết với nút trước đó.
currNode.next = prevNode;
currNode.previous = nextNode;
// Dịch chuyển nút prevNode và currNode về trước một bước.
prevNode = currNode;
currNode = nextNode;
}
// Cập nhật lại head và tail.
this.tail = this.head;
this.head = prevNode;
return this;
}
}