-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.js
More file actions
162 lines (130 loc) · 3.31 KB
/
LinkedList.js
File metadata and controls
162 lines (130 loc) · 3.31 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
function LinkedList () {
var Node = function (element) {
this.element = element
// 保存指向下个元素的引用,默认为null
this.next = null
}
// 链表长度
var length = 0
// head保存指向第一个元素的引用
var head = null
this.append = function (element) {
var node = new Node(element),
current
if (head === null) { // 当链表为空时
// 将head指向新增的元素
head = node
} else { // 链表不为空
// 使用一个current变量从head开始迭代链表
current = head
// 迭代链表,直到找到最后一项
while (current.next) {
current = current.next
}
// 找到最后一项,将其next赋为node,建立链接
current.next = node
}
// 更新列表长度
length++
}
this.removeAt = function (position) {
// 判断位置是否越界
if (position > -1 && position < length) {
var current = head,
previous,
index = 0
// 如果删除了第一个元素,把head指向下一个元素就行了
if (position === 0) {
head = current.next
} else {
// 根据输入的位置查找要删除的元素
while (index++ < position) {
previous = current
current = current.next
}
// 将上一个元素的next指向current的下一项,跳过current,实现移除current
previous.next = current.next
}
// 更新列表长度
length--
// 返回删除的元素
return current.element
} else {
return null
}
}
this.insert = function (position, element) {
// 检查位置是否越界
if (position >= 0 && position <= length) {
var node = new Node(element),
index = 0,
previous,
current = head
// 在第一个位置添加
if (position === 0) {
node.next = current
head = node
} else {
while (index++ < position) {
previous = current
current = current.next
}
node.next = current
previous.next = node
}
// 更新列表长度
length++
return true
} else {
return false
}
}
// 返回所有元素的值转成字符串
this.toString = function () {
var current = head,
string = ''
while (current) {
string += current.element
current = current.next
}
return string
}
// 查找元素在链表中的位置
this.indexOf = function (element) {
var current = head,
index = 0
while (current) {
if (element === current.element) {
return index
}
index++
current = current.next
}
return -1
}
// 移除特定元素
this.remove = function (element) {
var index = this.indexOf(element)
return this.removeAt(index)
}
// 判断链表是否为空
this.isEmpty = function () {
return length === 0
}
// 返回链表长度
this.size = function () {
return length
}
// 返回第一个元素
this.getHead = function () {
return head
}
}
// 一些操作
// var list = new LinkedList()
// console.log(list.indexOf(10))
// list.insert(0, 20)
// list.append(33)
// console.log(list.getHead())
// console.log(list.toString())
module.exports = LinkedList