小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
前面讲过链表,今天来了解双向链表
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表
创建双向链表
function DoublyLinkedList() {
function Node(data) {
this.data = data
this.prev = null //指向的是上一个节点
this.next = null
}
//属性
this.head = null
this.tail = null //指向的是末尾的节点
this.length = 0
}
添加节点append方法
DoublyLinkedList.prototype.append = function (data) {
var newNode = new Node(data)
if (this.head == null) {
this.head = newNode
this.tail = newNode
} else {
this.tail.next = newNode
newNode.prev = this.tail
this.tail = newNode
}
this.length += 1
}
重写toString方法
DoublyLinkedList.prototype.toString = function () {
return this.forWardString()
}
遍历链表中的每一项元素(正遍历)
DoublyLinkedList.prototype.forWardString = function () {
var current = this.head
var forWordStr = ""
while (current) {
forWordStr += "," + current.data
current = current.next
}
return forWordStr.slice(1)
}
遍历链表中的每一个元素(反遍历)
DoublyLinkedList.prototype.reverseString = function () {
var current = this.tail
var reverseStr = ""
while (current) {
reverseStr += "," + current.data
current = current.prev
}
return reverseStr.slice(1)
}
链表中任意位置插入元素
DoublyLinkedList.prototype.insert = function (position, data) {
if (position < 0 || position > this.length) return false
var newNode = new Node(data)
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
var previous = null
// 查找正确位置
while (index++ < position) {
previous = current
current = current.next
}
//交换节点指向顺序
newNode.next = current
newNode.prev = previous
current.prev = newNode
previous.next = newNode
}
this.length += 1
return true
}
获取指定位置的节点
DoublyLinkedList.prototype.get = function (position) {
if (position < 0 || position >= this.length) return false
if (this.length / 2 > position) {
var current = this.head
var index = 0
while (index++ < position) {
current = current.next
}
return current.data
}else{
var current = this.tail
var index = this.length-1
while(index-- > position){
current = current.prev
}
return current.data
}
}
获取节点长度
DoublyLinkedList.prototype.indexOf = function(data){
var current = this.head
var index = 0
while(current){
if(current.data == data){
return index
}
current = current.next
index += 1
}
return -1
}
更新指定位置节点的内容
DoublyLinkedList.prototype.update = function(position , newData){
if(position < 0 || position >= this.length) return false
var current = this.head
var index = 0
while(index++ < position){
current = current.next
}
current.data = newData
// console.log(list.forWardString());
return true
}
删除指定位置的节点,并返回删除信息
DoublyLinkedList.prototype.removeAt = function(position , data){
var newNode = new Node(data)
if(position < 0 || position >= this.length) return false
var 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
}else if(position == this.length-1){
current = this.tail
this.tail.prev.next = null
this.tail = this.tail.prev
}else{
var index = 0
while(index++ < position){
current = current.next
}
current.prev.next = current.next
current.next,prev = current.prev
}
}
this.length -= 1
return current
}