链表
单链表
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class NodeList {
constructor() {
this.head = null
this.length = 0
}
appendNode(data) {
const newNode = new Node(data)
let currentNode = this.head
if (this.head === null) {
this.head = newNode
} else {
while (currentNode.next) {
currentNode = currentNode.next
}
currentNode.next = newNode
newNode.next = null
}
}
insert(data, target) {
const newNode = new Node(data)
let current = this.head
while (current) {
if (current.data === target) {
newNode.next = current.next
current.next = newNode
return true
}
current = current.next
}
return false
}
update(data, target) {
let current = this.head
while (current) {
if (current.data === target) {
current.data = data
return true
}
current = current.next
}
return false
}
remove(target) {
let current = this.head
let previous = null
while (current) {
if (current.data === target) {
previous.next = current.next
return true
}
previous = current
current = current.next
}
return false
}
find(target) {
let current = this.head
while (current) {
if (current.data === target) {
return current
}
current = current.next
}
return null
}
sort() {
let current = this.head
while(current) {
}
}
format() {
let current = this.head
let res = []
while (current) {
res.push(current.data)
current = current.next
}
return res
}
}
const nodeList = new NodeList()
nodeList.appendNode(1)
nodeList.appendNode(2)
nodeList.appendNode(3)
nodeList.appendNode(4)
nodeList.appendNode(5)
nodeList.appendNode(6)
nodeList.appendNode(7)
nodeList.appendNode(8)
nodeList.insert(10, 5)
nodeList.update(101, 1)
nodeList.remove(4)
console.log(nodeList.find(5))
console.log(nodeList.format())
双向链表
class Node {
constructor(data) {
this.data = data
this.next = null
this.previous = null
}
}
class NodeList {
constructor() {
this.head = null
this.tail = null
}
appendNode(data) {
const newNode = new Node(data)
if (this.head === null) {
this.head = newNode
this.tail = newNode
} else {
newNode.previous = this.tail
this.tail.next = newNode
this.tail = newNode
}
}
insert(data, target) {
const newNode = new Node(data)
if (this.head === null) {
this.head = newNode
this.tail = newNode
} else {
let current = this.head
while (current) {
if (current.data === target) {
newNode.next = current.next
current.next.previous = newNode
current.next = newNode
newNode.previous = current
return true
}
current = current.next
}
}
return false
}
update(data, target) {
let current = this.head
while (current) {
if (current.data === target) {
current.data = data
return true
}
current = current.next
}
return false
}
remove(target) {
let current = this.head
while (current) {
if (current.data === target) {
current.previous.next = current.next
current.next.previous = current.previous
return true
}
current = current.next
}
return false
}
find(target) {
let current = this.head
while (current) {
if (current.data === target) return current
current = current.next
}
return null
}
format() {
let current = this.head
let res = []
while (current) {
res.push(current.data)
current = current.next
}
return res
}
}
const nodeList = new NodeList()
nodeList.appendNode(1)
nodeList.appendNode(10)
nodeList.appendNode(15)
nodeList.appendNode(53)
nodeList.remove(10)
console.log(nodeList)
console.log(nodeList.format())