插入操作 insert 示意图
function DoublyLinkedList() { //内部类 function Node(data) { this.data = data this.prev = null this.next = null } //属性 this.head = null this.tail = null this.length = 0 //方法封装 往尾部添加节点 DoublyLinkedList.prototype.append = function(data) { var newNode = new Node(data) if(this.length == 0) { this.head = newNode this.tail = newNode }else { //链表中已经存在数据 原来的最后一个节点的next this.tail.next newNode.prev = this.tail this.tail.next = newNode this.tail = newNode } this.length += 1 } DoublyLinkedList.prototype.toString = function() { return this.backwardString() } DoublyLinkedList.prototype.forwardString = function() { var current = this.tail var resultString = '' while(current) { resultString += current.data + ' ' current = current.prev } return resultString } 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 var newNode = new Node(data) if (this.length == 0) { this.head = newNode this.tail = newNode } else { if (position == 0) {// 与 append方法中的else类似 原来的最开始一个节点的prev this.head.prev this.head.prev = newNode newNode.next = this.head this.head = newNode } else if (position == this.length) { //与 append一样 newNode.prev = this.tail this.tail.next = newNode 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 } DoublyLinkedList.prototype.get = function(position,data) { //越界判断 if (position < 0 || position >= this.length) return null // this.length / 2 > position 从前往后 // this.length / 2 < position 从后往前 var current = this.head var index = 0 while (index ++ < position) { current = current.next } 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 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 thia.tail = this.tail.prev }else { var index = 0 // var current = this.head while (index ++ < position) { current = current.next } current.prev.next = current.next current.next.prev = current.prev } } this.length -= 1 return current.data } DoublyLinkedList.prototype.remove = function(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 } } var list = new DoublyLinkedList() list.append('abc') list.append('cba') list.append('nba') // alert(list.backwardString()) // alert(list.forwardString()) list.insert(0,'aaa') list.insert(4,'bbb') list.insert(2,'ccc') alert(list) alert(list.get(0)) alert(list.get(2)) alert(list.get(5)) alert(list.indexOf('aaa')) alert(list.indexOf('ccc')) alert(list) list.update(0,'mmm') list.update(1,'nnn') alert(list.removeAt(1)) alert(list.removeAt(0)) alert(list) alert(list.remove('cba')) alert(list) alert(list.remove('nba')) alert(list) alert(list.isEmpty()) alert(list.size()) alert(list.getHead()) alert(list.getTail())