feature:增加append函数,打印查找操作忽略head指向的元素
This commit is contained in:
parent
aa1c0b86c9
commit
88b6d1bddd
@ -15,7 +15,7 @@ class LinkedList {
|
||||
}
|
||||
// 根据value查找节点
|
||||
findByValue (item) {
|
||||
let currentNode = this.head
|
||||
let currentNode = this.head.next
|
||||
while (currentNode !== null && currentNode.element !== item) {
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
@ -23,9 +23,9 @@ class LinkedList {
|
||||
return currentNode === null ? -1 : currentNode
|
||||
}
|
||||
|
||||
// 根据index查找节点
|
||||
// 根据index查找节点,下标从0开始
|
||||
findByIndex (index) {
|
||||
let currentNode = this.head
|
||||
let currentNode = this.head.next
|
||||
let pos = 0
|
||||
while (currentNode !== null && pos !== index) {
|
||||
currentNode = currentNode.next
|
||||
@ -33,7 +33,17 @@ class LinkedList {
|
||||
}
|
||||
console.log(currentNode)
|
||||
return currentNode === null ? -1 : currentNode
|
||||
}
|
||||
}
|
||||
|
||||
// 向链表末尾追加节点
|
||||
append(newElement) {
|
||||
const newNode = new Node(newElement)
|
||||
let currentNode = this.head
|
||||
while(currentNode.next) {
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
currentNode.next = newNode
|
||||
}
|
||||
|
||||
// 指定元素向后插入
|
||||
insert (newElement, element) {
|
||||
@ -71,7 +81,7 @@ class LinkedList {
|
||||
|
||||
// 遍历显示所有节点
|
||||
display () {
|
||||
let currentNode = this.head
|
||||
let currentNode = this.head.next
|
||||
while (currentNode !== null) {
|
||||
console.log(currentNode.element)
|
||||
currentNode = currentNode.next
|
||||
@ -80,10 +90,14 @@ class LinkedList {
|
||||
}
|
||||
// Test
|
||||
const LList = new LinkedList()
|
||||
LList.insert('chen', 'head')
|
||||
LList.insert('curry', 'chen')
|
||||
LList.insert('sang', 'head')
|
||||
LList.insert('zhao', 'head')
|
||||
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
|
||||
console.log('-------------remove item------------')
|
||||
LList.remove('curry')
|
||||
LList.display()
|
||||
|
Loading…
Reference in New Issue
Block a user