本文共 4488 字,大约阅读时间需要 14 分钟。
在单向链表中,可以轻松的从头遍历到尾部,到达下一个节点,但是想回到前一个节点是很难的。
function DoublyLinkedList(){ // 内部类:节点类 function Node(data){ this.data = data this.prev = null this.next = null } // 属性 this.head = null this.tail = null this.height = 0}
DoublyLinkedList.prototype.append = function(data){ var newNode = new Node(data) if(this.length == 0){ this.head = newNode }else{ var current = this.head while(current.next){ current = current.next } current.next = newNode } this.length += 1}
//forwardString方法DoublyLinkedList.prototype.forwardString = function(){ var current = this.head var resultString = '' // 依次向前遍历,获取每一个节点 while(current){ resultString += current.data + '' current = current.prev } return resultString}//backwardString方法DoublyLinkedList.prototype.backwardString = function(){ var current = this.head var resultString = '' // 依次向后遍历,获取每一个节点 while(current){ resultString += current.data + '' current = current.next } return resultString}
DoublyLinkedList.prototype.insert = function(position,data){ // 越界判断 if(position < 0 || position > this.length) return false // 根据data创建新节点 var newNode = new Node(data) // 判断原来的链表是否为空 if(this.length == 0){ this.head = newNode this.tail = newNode }else{ // 1.判断position是否为0 if(position == 0){ 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 current = this.head var index = 0 while(index++ < position){ current = current.next } newNode.next = current newNode.prev = current.prev current.prev.next = newNode current.prev = newNode } this.length += 1 return true }}
// 方法1(普通方法)DoublyLinkedList.prototype.get = function(position){ // 越界判断 if(position < 0 || position >= this.length) return false // 获取元素 var current = this.head var index = 0 while(index++ < position){ current = current.next } return current.data}// 方法2DoublyLinkedList.prototype.get = function(position){ // 越界判断 // this.length / 2 > position: 从头向后遍历 // this.length / 2 < position: 从后向前遍历 // 从后向前遍历的操作: var current = this.tail var index = this.length - 1 // 链表的长度-1 while(index-- > position){ current = current.prev } return current.data}
DoublyLinkedList.prototype.indexOf = function(data){ // 定义变量 var current = this.head var index = 0 // 查找和data相同的节点 while(current){ if(current.data == data){ return index }else{ current = current.next index++ } } 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 return true}
DoublyLinkedList.prototype.removeAt = function(position){ //越界判断 if(position < 0 || position >= this.length) return null //判断是否只有一个节点 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 } length -= 1 return current.data}
DoublyLinkedList.prototype.remove = function(data){ // 根据data获取下标值 var index = this.indexOf(data) // 根据index删除对应位置的节点 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.data}
DoublyLinkedList.prototype.getTail = function(){ return this.tail.data}
转载地址:http://ohze.baihongyu.com/