数据结构与算法(1)——链表的javascript实现

linkedList.svg

链表结构中主要包含头结点,尾结点和中间结点 一个结点包含数据以及指向下个结点的指针

LinkedListNode

1
2
3
4
5
6
7
8
9
10
export default class LinkedListNode {
constructor(value, next = null) {
this.value = value; //存放值
this.next = next; // 指向下一个结点
}

toString(callback) {
return callback ? callback(this.value) : `${this.value}`;
}
}

LinkedList

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


export default class LinkedList {
/**
* @param {Function} [comparatorFunction]
*/
constructor(comparatorFunction) {
// 初始化 头结点 尾结点 引入值对比函数
this.head = null;
this.tail = null;
this.compare = new Comparator(comparatorFunction);
}

/**
* @param {*} value
* @return {LinkedList}
* 前置结点添加
*/
prepend(value) {
// 新结点成为一个头结点
const newNode = new LinkedListNode(value, this.head);
this.head = newNode;

// 如果是第一个结点 那么既是头也是尾
if (!this.tail) {
this.tail = newNode;
}

return this;
}

/**
* @param {*} value
* @return {LinkedList}
* 后置结点添加
*/
append(value) {
const newNode = new LinkedListNode(value);

// If there is no head yet let's make new node a head.
if (!this.head) {
this.head = newNode;
this.tail = newNode;

return this;
}

// 上一个尾部结点指向新结点 新结点成为尾部结点
this.tail.next = newNode;
this.tail = newNode;

return this;
}

/**
* @param {*} value
* @return {LinkedListNode}
*/
delete(value) {
// 没有头结点 还遍历个p
if (!this.head) {
return null;
}

let deletedNode = null;

// If the head must be deleted then make next node that is differ
// from the head to be a new head.
while (this.head && this.compare.equal(this.head.value, value)) {
deletedNode = this.head;
this.head = this.head.next;
}

let currentNode = this.head;

if (currentNode !== null) {

while (currentNode.next) {
if (this.compare.equal(currentNode.next.value, value)) {
// 满足删除条件 把当前结点指针指向下结点的下个结点
deletedNode = currentNode.next;
currentNode.next = currentNode.next.next;
} else {
// 否则 继续遍历
currentNode = currentNode.next;
}
}
}

// 如果是尾结点满足删除条件 上面已经处理了 currentNode是尾结点上个结点
// 这里重新指下尾结点
if (this.compare.equal(this.tail.value, value)) {
this.tail = currentNode;
}

return deletedNode;
}

/**
* @param {Object} findParams
* @param {*} findParams.value
* @param {function} [findParams.callback]
* @return {LinkedListNode}
*/
find({ value = undefined, callback = undefined }) {
if (!this.head) {
return null;
}

let currentNode = this.head;

while (currentNode) {
// 若自定义查找方法callback 使用其查找
if (callback && callback(currentNode.value)) {
return currentNode;
}

// If value is specified then try to compare by value..
if (value !== undefined && this.compare.equal(currentNode.value, value)) {
return currentNode;
}

currentNode = currentNode.next;
}

return null;
}

/**
* @return {LinkedListNode}
*/
deleteTail() {
const deletedTail = this.tail;

if (this.head === this.tail) {
// There is only one node in linked list.
this.head = null;
this.tail = null;

return deletedTail;
}

// If there are many nodes in linked list...

// Rewind to the last node and delete "next" link for the node before the last one.
let currentNode = this.head;
while (currentNode.next) {
if (!currentNode.next.next) {
currentNode.next = null;
} else {
currentNode = currentNode.next;
}
}

this.tail = currentNode;

return deletedTail;
}

/**
* @return {LinkedListNode}
*/
deleteHead() {
if (!this.head) {
return null;
}

const deletedHead = this.head;

if (this.head.next) {
this.head = this.head.next;
} else {
this.head = null;
this.tail = null;
}

return deletedHead;
}

/**
* @param {*[]} values - Array of values that need to be converted to linked list.
* @return {LinkedList}
* 数组转链表
*/
fromArray(values) {
values.forEach(value => this.append(value));

return this;
}

/**
* @return {LinkedListNode[]}
* 链表转数组
*/
toArray() {
const nodes = [];

let currentNode = this.head;
while (currentNode) {
nodes.push(currentNode);
currentNode = currentNode.next;
}

return nodes;
}

/**
* @param {function} [callback]
* @return {string}
*/
toString(callback) {
return this.toArray().map(node => node.toString(callback)).toString();
}

/**
* Reverse a linked list.
* @returns {LinkedList}
* 链表倒序
*/
reverse() {
let currNode = this.head;
let prevNode = null;
let nextNode = null;

while (currNode) {
// 存储下一个结点
nextNode = currNode.next;

// 当前指针指向前一个
currNode.next = prevNode;

// 为下一个循环准备
prevNode = currNode;
currNode = nextNode;
}

// 重置头尾指向
this.tail = this.head;
// prevNode是最后一个节点
this.head = prevNode;

return this;
}
}

测试

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
   const linkedList = new LinkedList();

// 测试空链表
linkedList.toString()==='' // true
linkedList.head===null // true
linkedList.tail===null // true

// append

linkedList.append(1);
linkedList.append(2);
linkedList.toString()==='1,2' //true
linkedList.tail.next===null //true

// prepend
const linkedList1 = new LinkedList();
linkedList1.prepend(2);
linkedList1.head.toString()==='2' //true
linkedList1.tail.toString()==='2' //true
linkedList1.append(1);
linkedList1.prepend(3);
linkedList1.toString()==='3,2,1' //true

// delete

const linkedList2 = new LinkedList();
linkedList2.delete(5)===null //true
linkedList2.append(1);
linkedList2.append(1);
linkedList2.append(2);
linkedList2.append(3);
linkedList2.append(3);
linkedList2.append(3);
linkedList2.append(4);
linkedList2.append(5);
const deletedNode= linkedList2.delete(3)
deletedNode.value===3 //true
linkedList2.toString()==='1,1,2,4,5' //true

// delete tail
const linkedList3 = new LinkedList();
linkedList3.append(1)
linkedList3.append(2)
linkedList3.append(3)
const tailNode= linkedList3.deleteTail()
tailNode.value===3 //true
linkedList3.toString()==='1,2' //true

// delete head
linkedList3.deleteHead()
linkedList3.toString()==='2'

// objects value
const linkedList4= new LinkedList();
const nodeValue1 = { value: 1, key: 'key1' };
const nodeValue2 = { value: 2, key: 'key2' };
linkedList4.append(nodeValue1).prepend(nodeValue2)
const nodeStringfier=value=>`key:${value.key} value:${value.value}`
linkedList4.toString(nodeStringfier)==='key:key1 value:1,key:key2 value:2'

// find
const linkedList5=new LinkedList();
linkedList5.append(1).append(2.append(3).append(4)
const node= linkedList5.find({value:3})
node.value===3 //true

// find callback
const cbNode= linkedList4.find({callback:value=>value.key==='key1'})
cbNode.value.value===1 //true

// custom compare Function
const customFunc=(a,b)=>{
if(_.isEqual(a,b)){
return 0
}else{
return -1
}
}
const linkedList6=new LinkedList(customFunc)
linkedList6.append( { value: 1, key: 'key1' }).append( { value: 2, key: 'key2' })
const node = linkedList6.find({value: { value: 1, key: 'key1' }})
node.value.value===1 //true
node.value.key==='key1'

// reverse
const linkedList7 = new LinkedList();
linkedList7
.append(1)
.append(2)
.append(3);
linkedList7.toString()==='1,2,3'
linkedList7.reverse()
linkedList7.toString()==='3,2,1'
查看评论