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
|
// 封装链表类
function LinkedList() {
// 内部的类:节点类
function Node(data, next) {
this.data = data
this.next = null
}
// 属性
this.head = null
this.length = 0
// 1.追加方法
LinkedList.prototype.append = function(data) {
// 1.创建新节点
var newNode = new Node(data)
// 2.判断是否添加的是不是第一个节点
if (this.length == 0) { //是第一个节点
this.head = newNode
} else { // 不是第一个节点
var current = this.head
while (current.next) {
current = current.next
}
// 最后节点的next指向新的节点
current.next = newNode
}
// 3.length加1
this.length += 1
}
// 2.toString()方法
LinkedList.prototype.toString = function() {
// 1.定义变量
var current = this.head
var listString = ''
// 2.循环获取一个个节点
while (current) {
listString += current.data + " "
current = current.next
}
return listString
}
// 3.insert方法
LinkedList.prototype.insert = function(position, data) {
// 1.对position进行越界判断
if (position < 0 || position > this.length) return false
// 2.根据data创建newNode
var newNode = new Node(data)
// 3.判断插入的位置是否是第一个
if (position == 0) {
newNode.next = this.head
this.head = newNode
} else {
var index = 0
var current = this.head
var previous = null
while (index++ < position) {
previous = current
current = current.next
}
newNode.next = current
previous.next = newNode
}
// 4.length+1
this.length += 1
return true
}
// 4.get方法
LinkedList.prototype.get = function(position) {
// 1.对position进行越界判断
if (position < 0 || position >= this.length) return null
// 2.获取对应的data
var current = this.head
var index = 0
while (index++ < position) {
current = current.next
}
return current.data
}
// 5.indexOf(element)方法
LinkedList.prototype.indexOf = function(element) {
// 1.定义变量
var current = this.head
var index = 0
// 2.开始查找
while (current) {
if (current.data === element) {
return index
}
current = current.next
index += 1
}
// 3.找到最后没找到,返回-1
return -1
}
// 6.update方法
LinkedList.prototype.update = function(position, newData) {
// 1.越界判断
if (position < 1 || position >= this.length) return false
// 2.查找正确的节点
var current = this.head
var index = 0
while (index++ < position) {
current = current.next
}
// 3.将position位置的node的data修改成newData
current.data = newData
return true
}
// 7.removeAt方法
LinkedList.prototype.removeAt = function(position) {
// 1.越界判断
var current = this.head
if (position < 0 || position >= this.length) return null
// 2.判断是否删除的是第一个节点
if (position == 0) {
this.head = this.head.next
} else {
var index = 0
var previous = null
while (index++ < position) {
previous = current
current = current.next
}
// 前一个节点的next指向current.next即可
previous.next = current.next
}
// 3.length-1
this.length -= 1
return current.data
}
// 8.remove方法
LinkedList.prototype.remove = function(data) {
// 1.获取data在列表中的位置
var position = this.indexOf(data)
// 2.根据位置信息,删除节点
return this.removeAt(position)
}
// 9.isEmpty方法
LinkedList.prototype.isEmpty = function() {
return this.length === 0
}
// 10.size方法
LinkedList.prototype.size = function() {
return this.length
}
}
// 测试代码
// 1.创建LinkedList
var list = new LinkedList()
// 2.测试append方法
list.append('abc')
list.append('cba')
list.append('mba')
console.log(list.toString())
// 3.测试insert
list.insert(0, 'aaa')
list.insert(3, 'bbb')
list.insert(5, 'ccc')
// 4.测试toString
console.log(list.toString());
// 5.测试get
console.log(list.get(3));
console.log(list.get(5));
console.log(list.get(6));
console.log(list.indexOf('ccc'));
// 6.测试update
console.log(list.toString());
list.update(2, 'ooo')
console.log(list.toString());
// 7.测试removeAt方法
console.log(list.removeAt(0));
list.removeAt(3)
console.log(list.toString());
// 8.测试remove方法
list.remove('ooo')
console.log(list.toString());
// 9.测试size方法和isEmpty
console.log(list.isEmpty());
console.log(list.size());
|