2018-10-05 18:58:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* 1)单链表的插入、删除、查找操作;
|
|
|
|
|
* 2)链表中存储的是int类型的数据;
|
|
|
|
|
*/
|
|
|
|
|
class Node {
|
|
|
|
|
constructor (element) {
|
|
|
|
|
this.element = element
|
|
|
|
|
this.next = null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class LinkedList {
|
|
|
|
|
constructor () {
|
|
|
|
|
this.head = new Node('head')
|
|
|
|
|
}
|
|
|
|
|
// 根据value查找节点
|
|
|
|
|
findByValue (item) {
|
2019-05-09 16:38:59 +08:00
|
|
|
|
let currentNode = this.head.next
|
2018-10-05 18:58:20 +08:00
|
|
|
|
while (currentNode !== null && currentNode.element !== item) {
|
|
|
|
|
currentNode = currentNode.next
|
|
|
|
|
}
|
|
|
|
|
console.log(currentNode)
|
|
|
|
|
return currentNode === null ? -1 : currentNode
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 16:38:59 +08:00
|
|
|
|
// 根据index查找节点,下标从0开始
|
2018-10-05 18:58:20 +08:00
|
|
|
|
findByIndex (index) {
|
2019-05-09 16:38:59 +08:00
|
|
|
|
let currentNode = this.head.next
|
2018-10-05 18:58:20 +08:00
|
|
|
|
let pos = 0
|
|
|
|
|
while (currentNode !== null && pos !== index) {
|
|
|
|
|
currentNode = currentNode.next
|
|
|
|
|
pos++
|
|
|
|
|
}
|
|
|
|
|
console.log(currentNode)
|
|
|
|
|
return currentNode === null ? -1 : currentNode
|
2019-05-09 16:38:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 向链表末尾追加节点
|
|
|
|
|
append(newElement) {
|
|
|
|
|
const newNode = new Node(newElement)
|
|
|
|
|
let currentNode = this.head
|
|
|
|
|
while(currentNode.next) {
|
|
|
|
|
currentNode = currentNode.next
|
|
|
|
|
}
|
|
|
|
|
currentNode.next = newNode
|
|
|
|
|
}
|
2018-10-05 18:58:20 +08:00
|
|
|
|
|
|
|
|
|
// 指定元素向后插入
|
|
|
|
|
insert (newElement, element) {
|
|
|
|
|
const currentNode = this.findByValue(element)
|
|
|
|
|
if (currentNode === -1) {
|
|
|
|
|
console.log('未找到插入位置')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const newNode = new Node(newElement)
|
|
|
|
|
newNode.next = currentNode.next
|
|
|
|
|
currentNode.next = newNode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找前一个
|
|
|
|
|
findPrev (item) {
|
|
|
|
|
let currentNode = this.head
|
|
|
|
|
while (currentNode.next !== null && currentNode.next.element !== item) {
|
|
|
|
|
currentNode = currentNode.next
|
|
|
|
|
}
|
|
|
|
|
if (currentNode.next === null) {
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
return currentNode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据值删除
|
|
|
|
|
remove (item) {
|
2019-05-09 16:18:27 +08:00
|
|
|
|
const prevNode = this.findPrev(item)
|
|
|
|
|
if (prevNode === -1) {
|
2018-10-05 18:58:20 +08:00
|
|
|
|
console.log('未找到元素')
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-05-09 16:18:27 +08:00
|
|
|
|
prevNode.next = prevNode.next.next
|
|
|
|
|
}
|
2018-10-05 18:58:20 +08:00
|
|
|
|
|
|
|
|
|
// 遍历显示所有节点
|
|
|
|
|
display () {
|
2019-05-09 16:57:41 +08:00
|
|
|
|
let currentNode = this.head.next // 忽略头指针的值
|
2018-10-05 18:58:20 +08:00
|
|
|
|
while (currentNode !== null) {
|
|
|
|
|
console.log(currentNode.element)
|
|
|
|
|
currentNode = currentNode.next
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Test
|
|
|
|
|
const LList = new LinkedList()
|
2019-05-09 16:38:59 +08:00
|
|
|
|
LList.append('chen')
|
|
|
|
|
LList.append('curry')
|
|
|
|
|
LList.append('sang')
|
|
|
|
|
LList.append('zhao') // chen -> curry -> sang -> zhao
|
|
|
|
|
console.log('-------------insert item------------')
|
|
|
|
|
LList.insert('qian', 'chen') // 首元素后插入
|
|
|
|
|
LList.insert('zhou', 'zhao') // 尾元素后插入
|
|
|
|
|
LList.display() // chen -> qian -> curry -> sang -> zhao -> zhou
|
2018-10-05 18:58:20 +08:00
|
|
|
|
console.log('-------------remove item------------')
|
2019-03-18 17:44:01 +08:00
|
|
|
|
LList.remove('curry')
|
2019-05-09 16:57:41 +08:00
|
|
|
|
LList.display() // chen -> qian -> sang -> zhao -> zhou
|
2018-10-05 18:58:20 +08:00
|
|
|
|
console.log('-------------find by item------------')
|
|
|
|
|
LList.findByValue('chen')
|
|
|
|
|
console.log('-------------find by index------------')
|
|
|
|
|
LList.findByIndex(2)
|
2019-05-09 16:47:21 +08:00
|
|
|
|
console.log('-------------与头结点同值元素测试------------')
|
|
|
|
|
LList.insert('head', 'sang')
|
|
|
|
|
LList.display() // chen -> qian -> sang -> head -> zhao -> zhou
|
|
|
|
|
LList.findPrev('head') // sang
|
|
|
|
|
LList.remove('head')
|
|
|
|
|
LList.display() // chen -> qian -> sang -> zhao -> zhou
|