双向链表
1、双向链表的特点:
1、可以使用一个head和一个tail分别指向头部和尾部的节点 2、每个节点都由三部分组成: 前一个节点的指针prev 保存的元素item 后一个节点的指针next 3、双向链表的第一个节点的prev是null 4、双向链表的最后节点的next是null
2、双向链表常见操作:
1、append(element): 向列表尾部添加一个新的项 2、insert(position, element): 向链表的特定位置插入一个新的项 3、get(position): 获取对应位置的元素 4、indexOf(element): 返回元素在列表中的索引,如果列表中没有该元素则返回-1 5、update(position, element): 修改某个位置的元素 6、removeAt(position): 从列表的特定位置移除一项 7、remove(element): 从列表中移除一项 8、isEmpty(): 如果链表中不包含任何元素,返回true,如果链表长度大于0则返回false 9、size(): 返回链表包含的元素个数,与数组的length属性类似 10、toString(): 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值。 11、backwordString(): 返回从前向后遍历的节点字符串形式 12、forwardString(): 返回从后向前遍历的节点字符串形式 13、getHead(): 获取链表的第一个元素 14、getTail(): 获取链表的最后一个元素
3、封装双向链表
function DoublyLinkedList() {
function Node(element) {
this.element = element
this.next = null
this.prev = null
}
this.length = 0
this.head = null
this.tail = null
DoublyLinkedList.prototype.append = function (element) {
var newNode = new Node(element)
if (this.head == null) {
this.head = newNode
this.tail = newNode
} else {
this.tail.next = newNode
newNode.prev = this.tail
this.tail = newNode
}
this.length++
}
DoublyLinkedList.prototype.toString = function () {
return this.backwardString()
}
DoublyLinkedList.prototype.forwardString = function () {
var current = this.tail
var forwardStr = ""
while (current) {
forwardStr += current.element + " "
current = current.prev
}
return forwardStr
}
DoublyLinkedList.prototype.backwardString = function(){
var current = this.head
var backwardStr = ""
while (current) {
backwardStr += current.element + ' '
current = current.next
}
return backwardStr
}
DoublyLinkedList.prototype.insert = function (position, element) {
if (position < 0 || position > this.length) return false
var newNode = new Node(element)
if (position === 0) {
if (this.head == null) {
this.head = newNode
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 {
var index = 0
var current = this.head
while (index++ < position) {
current = current.next
}
newNode.next = current
newNode.prev = current.prev
current.prev.next = newNode
current.prev = newNode
}
this.length++
return true
}
DoublyLinkedList.prototype.get = function(position){
if(position < 0 || position >= this.length ) return null
if(this.length / 2 >= position){
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
}
if(this.length / 2 < position){
var current = this.tail
var index = length - 1
while(index-- > position){
current = current.prev
}
}
return current.element
}
DoublyLinkedList.prototype.indexOf = function (element) {
var current = this.head
var index = 0
while (current) {
if (current.element === element) {
return index
}
current = current.next
index += 1
}
return -1
}
DoublyLinkedList.prototype.update = function(position, newElement){
if(position < 0 || position >= this.length ) return false
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
current.element = newElement
return true
}
DoublyLinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length) return null
var current = this.head
if (position === 0) {
if (this.length == 1) {
this.head = null
this.tail = null
} else {
this.head = this.head.next
this.head.prev = null
}
} else if (position === this.length -1) {
current = this.tail
this.tail = this.tail.prev
this.tail.next = null
} else {
var index = 0
var previous = null
while (index++ < position) {
previous = current
current = current.next
}
previous.next = current.next
current.next.prev = previous
}
this.length--
return current.element
}
DoublyLinkedList.prototype.remove = function (element) {
var index = this.indexOf(element)
return this.removeAt(index)
}
DoublyLinkedList.prototype.isEmpty = function () {
return this.length === 0
}
DoublyLinkedList.prototype.size = function () {
return this.length
}
DoublyLinkedList.prototype.getHead = function(){
return this.head.element
}
DoublyLinkedList.prototype.getTail = function(){
return this.tail.element
}
}
var list = new DoublyLinkedList()
list.append(10)
list.append(5)
list.append(20)
list.append(25)
alert(list)
alert(list.backwardString())
alert(list.forwardString())
list.insert(0, 28)
list.insert(2, 40)
list.insert(6, 50)
alert(list)
alert(list.get(0))
alert(list.get(6))
alert(list.indexOf(28))
alert(list.update(0, 30))
alert(list)
alert(list.removeAt(0))
alert(list.removeAt(2))
alert(list.removeAt(4))
alert(list)
alert(list.remove(10))
alert(list)
alert(list.isEmpty())
alert(list.size())
alert(list.getHead())
alert(list.getTail())
|