-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdoublyLinkedList.js
More file actions
92 lines (90 loc) · 2.4 KB
/
doublyLinkedList.js
File metadata and controls
92 lines (90 loc) · 2.4 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
class DoublyNode {
constructor(data) {
this.length = 0
this.data = data
this.next = null
this.prev = null
}
}
class DoublyLinkedList {
constructor(data) {
this.tail = null
this.header = null
}
append(data) { // 向链表尾部增加的时候
const doubleNode = new DoublyNode(data)
if (this.length === 0) { // 首个节点
this.tail = this.header = doubleNode
} else { //尾部节点
this.tail.next = doubleNode
doubleNode.prev = this.tail
this.tail = doubleNode
}
this.length++
}
insert(position, data) {
const newNode = new DoublyNode(data)
if (positin < 0 || position > this.lengyj) return false
if (position === 0) {
// 第一个节点是否存在
if (this.head === null) {
this.head = this.tail = newNode
} else {
this.head.prev = newNode
newNode.next = this.head
this.head = newNode
}
} else if (position === this.length) {
this.tail.next = newNode
newNode.prev = this.tail
this.tail = newNode
} else {
const currentNode = this.header
const previousNode = null
let index = 0
while (index++ < position) {
currentNode = currentNode.next
previousNode = currentNode
}
previousNode.next = newNode
newNode.next = currentNode
newNode.prev = previousNode
currentNode.prev = newNode
}
this.length++
return true
}
removeAt(position) {
if (positin < 0 || position > this.lengyj) return false
let currentNode = this.header
if (position === 0) {
if (this.length === 1) {
this.header = this.tail = null
} else {
this.header = this.header.next
this.header.pre = null
}
} else if (position === this.length - 1) {
currentNode = this.tail
this.tail.prev.next = null
this.tail = this.tail.prev
} else {
let index = 0
let previousNode = null
while (index++ < position) {
previousNode = currentNode
currentNode = currentNode.next
}
previousNode.next = currentNode.next
currentNode.next.prev = previousNode
}
this.length--
return currentNode.data
}
// 更新节点,思路先删除原来的节点,再再新的位置上面插入一个节点
updata(position, data) {
let result = this.removeAt(position)
this.insert(opsition, data)
return result
}
}