class DoublyNode { constructor(data) { this.length = 0 this.data = data this.next = null this.prev = null } } class DoublyLinkedList { constructor(data) { this.tail = null this.header = null } append(data) { // åé¾è¡¨å°¾é¨å¢å çæ¶å const doubleNode = new DoublyNode(data) if (this.length === 0) { // é¦ä¸ªèç¹ this.tail = this.header = doubleNode } else { //å°¾é¨èç¹ this.tail.next = doubleNode doubleNode.prev = this.tail this.tail = doubleNode } this.length++ } insert(position, data) { const newNode = new DoublyNode(data) if (positin < 0 || position > this.lengyj) return false if (position === 0) { // 第ä¸ä¸ªèç¹æ¯å¦åå¨ if (this.head === null) { this.head = this.tail = newNode } else { this.head.prev = newNode newNode.next = this.head this.head = newNode } } else if (position === this.length) { this.tail.next = newNode newNode.prev = this.tail this.tail = newNode } else { const currentNode = this.header const previousNode = null let index = 0 while (index++ < position) { currentNode = currentNode.next previousNode = currentNode } previousNode.next = newNode newNode.next = currentNode newNode.prev = previousNode currentNode.prev = newNode } this.length++ return true } removeAt(position) { if (positin < 0 || position > this.lengyj) return false let currentNode = this.header if (position === 0) { if (this.length === 1) { this.header = this.tail = null } else { this.header = this.header.next this.header.pre = null } } else if (position === this.length - 1) { currentNode = this.tail this.tail.prev.next = null this.tail = this.tail.prev } else { let index = 0 let previousNode = null while (index++ < position) { previousNode = currentNode currentNode = currentNode.next } previousNode.next = currentNode.next currentNode.next.prev = previousNode } this.length-- return currentNode.data } // æ´æ°èç¹ï¼æè·¯å å é¤åæ¥çèç¹ï¼ååæ°çä½ç½®ä¸é¢æå ¥ä¸ä¸ªèç¹ updata(position, data) { let result = this.removeAt(position) this.insert(opsition, data) return result } }