function DoublyLinkedList() {
this.head = null
this.tail = null
this.length = 0
function Node(data) {
this.data = data
this.next = null
this.prev = null
}
DoublyLinkedList.prototype.append = function(el) {
let newNode = new Node(el)
if(this.length === 0) {
this.head = newNode
this.tail = newNode
} else {
this.tail.next = newNode
newNode.prev = this.tail
this.tail = newNode
}
this.length += 1
}
DoublyLinkedList.prototype.insert = function(position, el) {
if(position<0 || position > this.length) return false
let newNode = new Node(el)
if(position === 0) {
let current = this.head
this.head = newNode
current.prev = newNode
newNode.next = current
} else {
let index = 0
let current = this.head
while(index++ < position - 1) {
current = current.next
}
if(!current.next) {
newNode.prev = current
current.next = newNode
this.tail = newNode
} else {
newNode.next = current.next
newNode.prev = current
current.next.prev = newNode
current.next = newNode
}
}
this.length += 1
}
DoublyLinkedList.prototype.get = function(position) {
if(position < 0 || position >= this.length) return false
let index = 0
let current = this.head
while(index++ < position) {
current = current.next
}
return current.data
}
DoublyLinkedList.prototype.indexOf = function(el) {
let current = this.head
let index = 0
while(current) {
if(current.data === el) {
return index
}
current = current.next
index += 1
}
if(current.data == el) return this.length - 1
return -1
}
DoublyLinkedList.prototype.update = function(position, el) {
if(position < 0 || position >= this.length) return false
let index = 0
let current = this.head
while(index++ < position) {
current = current.next
}
current.data = el
}
DoublyLinkedList.prototype.removeAt = function(position) {
if(position < 0 || position >= this.length) return false
let current = this.head
if(position === 0) {
this.head = current.next
current.next.prev = null
}else{
let index = 0
while(index++ < position) {
current = current.next
}
if(!current.next) {
this.tail = current.prev
current.prev.next = null
} else{
current.prev.next = current.next
current.next.prev = current.prev
}
}
return current
}
DoublyLinkedList.prototype.remove = function(element) {
let index = this.indexOf(element)
if(index < 0) {
return undefined
}
return this.removeAt(index)
}
DoublyLinkedList.prototype.isEmpty = function() {
return this.length === 0
}
DoublyLinkedList.prototype.size = function() {
return this.length
}
DoublyLinkedList.prototype.toString = function() {
let current = this.head
let resultString = ''
while(current) {
resultString += current.data + ' '
current = current.next
}
return resultString
}
// 向前遍历
DoublyLinkedList.prototype.forwardString = function() {
let current = this.tail
let resultString = ''
while(current) {
resultString += current.data + ' '
current = current.prev
}
return resultString
}
// 向后遍历
DoublyLinkedList.prototype.backwardString = function() {
let current = this.head
let resultString = ''
while(current) {
resultString += current.data + ' '
current = current.next
}
return resultString
}
}
let dLinkedList = new DoublyLinkedList()
dLinkedList.append('a')
dLinkedList.append('b')
dLinkedList.append('c')
dLinkedList.append('d')
dLinkedList.append('e')
dLinkedList.append('f')
dLinkedList.insert(2,'2')
console.log(dLinkedList.toString());
console.log(dLinkedList.forwardString());
console.log(dLinkedList.get(3));
console.log(dLinkedList.indexOf('a'));
dLinkedList.update(3,'3')
console.log(dLinkedList.toString());
dLinkedList.removeAt(6)
console.log(dLinkedList.toString());
dLinkedList.remove('e')
console.log(dLinkedList.toString());
javascript实现双向链表
最新推荐文章于 2022-03-23 22:07:55 发布