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 } } 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())
标签:head,单链,nodeList,next,current,return,data From: https://www.cnblogs.com/karle/p/17860019.html