functionLinkedList() { functionNode(data) { this.data = data this.next = null
} this.head = null// 表头 this.length = 0 // 插入链表 LinkedList.prototype.append = function (data) { // 判断是否是添加的第一个节点 let newNode = newNode(data) if (this.length == 0) { this.head = newNode } else { let current = this.head while (current.next) { // 如果next存在, // 则当前节点不是链表最后一个 // 所以继续向后查找 current = current.next } // 如果next不存在 // 则当前节点是链表最后一个 // 所以让next指向新节点即可 current.next = newNode } this.length++ } // toString方法 LinkedList.prototype.toString = function () { let current = this.head let listString = '' while (current) { listString += current.data + ' ' current = current.next } return listString } // insert 方法 LinkedList.prototype.insert = function (position, data) { if (position < 0 || position > this.length) returnfalse let newNode = newNode(data) if (position == 0) { newNode.next = this.head this.head = newNode } else { let index = 0 let current = this.head let prev = null while (index++ < position) { prev = current current = current.next } newNode.next = current prev.next = newNode } this.length++ returntrue } // get方法 LinkedList.prototype.get = function (position) { if (position < 0 || position >= this.length) returnnull let index = 0 let current = this.head while (index++ < position){ current = current.next } return current.data } LinkedList.prototype.indexOf = function (data) { let index = 0 let current = this.head while (current) { if (current.data == data) { return index } else { current = current.next index++ } }
return -1 } LinkedList.prototype.update = function (position, data) { if (position < 0 || position >= this.length) returnfalse let index = 0 let current = this.head while (index++ < position) { current = current.next } current.data = data returntrue } LinkedList.prototype.removeAt = function (position) { if (position < 0 || position >= this.length) returnnull if (position == 0) { this.head = this.head.next } else { let index = 0 let current = this.head let prev = null while (index++ < position) { prev = current current = current.next } prev.next = current.next } this.length-- returntrue
} LinkedList.prototype.remove = function (data) { let postions = this.indexOf(data)
while (current) { str += current.data + '' current = current.prev }
return str } doublyLinkedList.prototype.backwardString = function () { let current = this.head let str = ''
while (current) { str += current.data + '' current = current.next }
return str }
doublyLinkedList.prototype.insert = function (position, data) { if (position < 0 || position > this.length) returnfalse let newNode = newNode(data) if (this.length === 0) { this.head = newNode this.tail = newNode } else { if (position == 0) { this.head.prev = newNode newNode.next = this.head this.head = newNode } elseif (position == this.length) { newNode.prev = this.tail this.tail.next = newNode this.tail = newNode } else { let current = this.head let index = 0 while( index++ < position){ current = current.next } newNode.next = current newNode.prev = current.prev current.prev.next = newNode current.prev = newNode
}
}
this.length++ returntrue } doublyLinkedList.prototype.get = function (position) { if (position < 0 || position >= this.length) returnnull let current = this.head let index = 0 while (index++) { current = current.next }
return current.data } doublyLinkedList.prototype.indexOf = function (data) { let current = this.head let index = 0 while (current) { if (current.data === data) { return index } current = current.next index++ } return -1 } doublyLinkedList.prototype.update = function (position, newData) { if (position < 0 || position >= this.length) returnfalse let current = this.head let index = 0 while(index++ < position){ current = current.next } current.data = newData returntrue } doublyLinkedList.prototype.removeAt = function (position) { if (position < 0 || position >= this.length) returnnull let current = this.head if (this.length === 1) { this.head = null this.tail = null } else { if (position === 0) { // 删除第一个节点 this.head.next.prev = null this.head = this.head.next } elseif (position === this.length - 1) { // 删除最后一个节点 this.tail.prev.next = null this.tail = this.tail.prev } else { let index = 0 while (index++ < position) { current = current.next } current.prev.next = current.next current.next.prev = current.prev } } this.length-- return current.data } doublyLinkedList.prototype.remove = function (data) { let index = this.indexOf(data) returnthis.removeAt(index) } }