-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path双向链表.js
111 lines (108 loc) · 3.02 KB
/
双向链表.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
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
function Node(data) {
this.data = data
this.prev = null
this.next = null
}
class Dlist {
constructor () {
this.length = 0
this.data = null
}
find (elem) { //查找
let current = this.data
while (current !== null && current.data != elem) {
current = current.next
}
return current
}
insert (data,elem) { //插入
this.length++
let current
if (elem) { //先处理插入位置问题。若目标位置存在,则插在它后面,否则插在最后
current = this.find(elem)
} else {
current = null
}
let newNode = new Node(data)
if (this.data == null) { //链表为空情况,直接插
this.data = newNode
return
}
if (current) { //目标节点存在
if (current.next) {
let temp = current.next
temp.prev = newNode
}
newNode.next = current.next
newNode.prev = current
current.next = newNode
} else { //目标节点不存在
let temp = this.data
while (temp != null && temp.next != null) {
temp = temp.next
}
temp.next = newNode
newNode.prev = temp
}
}
remove (pos) { //移除
if (pos < 0 || pos >= this.length) {
console.log('不存在,无法删除!!!')
return
}
let curNode = this.data //首节点无误
if (pos == 0) { //首位置
let temp = this.data.data
this.data = this.data.next
this.data.prev = null
this.length --
return temp
} else if (pos == this.length - 1) { //末位置
for (let i = 0; i < pos; i++) {
curNode = curNode.next
}
let temp = curNode.data
curNode.prev.next = null
this.length --
return temp
} else {
for (let i = 0; i < pos; i++) {
curNode = curNode.next
}
let temp = curNode.data
curNode.prev.next = curNode.next;
curNode.next.prev = curNode.prev;
this.length--
}
}
display () { //输出
let current = this.data
while(current != null) {
console.log(current.data)
current = current.next
}
}
findLast () { //查找第末个
let current = this.data
while(current.next != null) {
current = current.next
}
return current
}
disReverse() { //逆向输出
let last = this.findLast()
while (last != null) {
console.log(last.data)
last = last.prev
}
}
leng () { //链表长度
return this.length
}
clear () {
for(var key in this.data){ //遍历删光所有属性即可
delete this.data[key];
}
this.length = 0
}
}