Repository navigation
Expand file tree
/
Copy pathunidirectional-linked-list.js
More file actions
183 lines (163 loc) · 3.52 KB
/
Copy pathunidirectional-linked-list.js
File metadata and controls
183 lines (163 loc) · 3.52 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
class Node {
constructor(el) {
this.el = el
this.next = null
}
}
class LinkedList {
static newNode(el = {}) {
return new Node(el)
}
constructor(nodeStr) {
const _nodeStr = nodeStr || ''
const _strList = _nodeStr.split('||').filter(it => it)
this.head = null
this.lastNode = null
this.count = 0
this.__init__(_strList)
}
__init__(list = []) {
for (let i = 0; i < list.length; i++) {
const _item = list[i]
try {
const _el = JSON.parse(_item)
this.push(_el)
} catch (error) {
console.error(`${_item} is error node el`)
}
}
}
/*
* 获取链表头
*/
getRoot() {
return this.head
}
/*
* 获取列表长度
*/
getLength() {
return this.count
}
/*
* 获取链表尾
*/
getLastNode() {
return this.lastNode
}
/*
* 给链表插入节点
*/
push(el = {}) {
const _node = new Node(el)
if (this.head) {
this.lastNode.next = _node
} else {
this.head = _node
}
this.count++
this.lastNode = _node
return this.getLastNode()
}
/*
* 查询索引位置的节点
*/
getElementAt(index = 0) {
if (index >= 0 && this.getLength() >= index) {
let _currentEl = this.getRoot()
for (let i = 1; i <= index; i++) {
_currentEl = _currentEl.next
if (!_currentEl) return null
}
return _currentEl
}
return null
}
/*
* 删除节点
*/
removeAt(index = 0) {
if (index < 0 || index > this.getLength()) return null
if (index === 0) return this.removeRoot()
const _preEl = this.getElementAt(index - 1)
const _currentEl = _preEl ? _preEl.next : null
if (!_preEl || !_currentEl) return null
_preEl.next = _currentEl ? _currentEl.next : null
if ((index + 1) === this.getLength()) this.lastNode = _preEl.next || _preEl
--this.count
_currentEl.next = null
return _currentEl
}
/*
* 删除root节点
*/
removeRoot() {
const _currentEl = this.head
if (!_currentEl) return null
this.head = _currentEl.next || null
--this.count
delete _currentEl.next
return _currentEl
}
/*
* 向指定长度插入节点
*/
insert(index = 0, el = {}) {
if (index < 0 || index > this.getLength()) return null
if (index === 0) return this.insertRoot(el)
const _el = new Node(el)
const _preEl = this.getElementAt(index - 1)
_el.next = _preEl.next
_preEl.next = _el
++this.count
return _el
}
/*
* 向头部插入节点
*/
insertRoot(el = {}) {
const _el = new Node(el)
const _currentEl = this.getRoot()
_el.next = _currentEl
this.head = _el
++this.count
return _el
}
/*
* 序列化
*/
toString() {
let _currentEl = this.head
let _objStr = `${this.getElementString(_currentEl)}`
for (let i = 1; i < this.count; i++) {
_currentEl = _currentEl.next
if (_currentEl) {
_objStr += `||${this.getElementString(_currentEl)}`
} else {
return _objStr
}
}
return _objStr
}
/*
*
*/
getElementString(node = {}) {
return JSON.stringify(node.el)
}
}
module.exports = LinkedList
// 测试代码
// const linkList = new LinkedList()
// linkList.push({ name: 'root', customDate: 1100 })
// linkList.push({ name: '1', customProps: 1021 })
// linkList.push({ name: '2', customProps: 9999 })
// linkList.push({ name: '3', customDate: 890 })
// linkList.insert(1, { name: 'test', customDate: 32190 })
// console.info(linkList.getElementAt(0))
// console.info(linkList.getElementAt(1))
// console.info(linkList.getElementAt(2))
// console.info(linkList.getElementAt(3))
// console.info(linkList.getElementAt(4))
// console.info(linkList.toString())
// console.info(new LinkedList(linkList.toString()))