-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.js
74 lines (64 loc) · 1.52 KB
/
DoublyLinkedList.js
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
const { Node, LinkedList } = require('./LinkedList.js')
class DoublyNode extends Node {
constructor(value) {
super(value)
this.prev = null
}
}
class DoublyLinkedList extends LinkedList {
constructor() {
super()
this.tail = null
}
insert(value, index) {
if (index < 0 || index > this.count)
return false
let node = new DoublyNode(value)
let current = this.head
if (index === 0) { // 在第 0 个节点插入
if (!current) { // 如果链表为空
this.head = node
this.tail = node
} else { // 如果链表不为空
this.head = node
node.next = current
current.prev = node
}
} else if (index === this.count) { // 在最后一个节点插入
current = this.tail
current.next = node
node.prev = current
this.tail = node
} else { // 在中间位置插入节点
let previous = this.getNodeAt(index - 1)
current = previous.next
previous.next = node
node.prev = previous
node.next = current
current.prev = node
}
this.count++
return true
}
}
const dblist = new DoublyLinkedList()
dblist.insert(15, 0)
dblist.insert(10, 1)
dblist.insert(13, 2)
dblist.insert(11, 3)
dblist.insert(12, 4)
// console.log(dblist)
let result = []
let current = dblist.tail
while (current) {
result.push(current.value)
current = current.prev
}
console.log(result)
result = []
current = dblist.head
while (current) {
result.push(current.value)
current = current.next
}
console.log(result)