javascript封装数据结构中的双向链表,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>双向链表</title>
</head>
<body>
<script>
function DoublyLinkedList() {
function Node(element) {
this.element = element
this.next = null
this.prev = null
}
this.head = null
this.tail = null
this.length = 0
DoublyLinkedList.prototype.append = function (element) {
var newNode = new Node(element)
if (this.head == null) {
this.head = newNode
this.tail = newNode
} else {
newNode.prev = this.tail
this.tail.next = newNode
this.tail = newNode
}
this.length += 1
}
DoublyLinkedList.prototype.toString = function () {
return this.forwardString()
}
DoublyLinkedList.prototype.forwardString = function () {
var current = this.head
var listString = ''
while (current) {
listString += current.element + ','
current = current.next
}
return listString
}
DoublyLinkedList.prototype.reverseString = function () {
var current = this.tail
var listString = ''
while (current) {
listString += current.element + ','
current = current.prev
}
return listString
}
DoublyLinkedList.prototype.insert = function (position, element) {
var newNode = new Node(element)
if (position < 0 || position > this.length) return false
if (this.head == null) {
this.head = newNode
this.tail = newNode
} else {
if (position == 0) {
this.head.prev = newNode
newNode.next = this.head
this.head = newNode
} else if (position == this.length) {
newNode.prev = this.tail
this.tail.next = newNode
this.tail = newNode
} else {
var index = 0
var current = this.head
var prev = null
while (index++ < position) {
prev = current
current = current.next
}
newNode.prev = prev
newNode.next = current
current.prev = newNode
prev.next = newNode
}
}
this.length += 1
return true
}
DoublyLinkedList.prototype.removeAt = function (position) {
if (position < 0 || position > this.length) return false
var current = this.head
var prev = null
var index = 0
/*
第一次写的代码
while (index++ < position) {
prev = current
current = current.next
}
if (position == this.length - 1) {
this.tail = prev
prev.next = null
} else if(position == 0){
this.head = current.next
}else{
prev.next = current.next
current.next.prev = prev
} */
//第二次代码重构
if (position == 0) {
if (this.length == 1) {
current.prev = null
this.head = null
this.tail = null
} else {
this.head = current.next
current.prev = null
}
} else if (position == this.length - 1) {
this.tail = this.tail.prev
this.tail.prev.next = null
} else {
while (index++ < position) {
prev = current
current = current.next
}
prev.next = current.next
current.next.prev = prev
}
this.length -= 1
return current.element
}
DoublyLinkedList.prototype.indexOf = function (element) {
var current = this.head
var index = 0
while (current) {
if (current.element == element) {
return index
}
index++
current = current.next
}
return -1
}
DoublyLinkedList.prototype.remove = function (element) {
var index = this.indexOf(element)
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('abc')
list.append('cba')
list.append('nba')
list.append('mba')
list.insert(0, '100')
list.insert(2, '200')
list.insert(6, '300')
console.log(list.toString());
console.log(list.removeAt(0));
console.log(list.removeAt(1));
console.log(list.removeAt(4));
console.log(list.toString());
console.log(list.indexOf('abc'));
console.log(list.indexOf('cba'));
console.log(list.indexOf('nba'));
list.remove('abc')
console.log(list.toString());
</script>
</body>
</html>
这次过程相对于之前的单向链表更复杂,图画了好几遍,才理清逻辑,在此期间遇到的
主要问题是while循环没用对,有时需要while循环的时候,使用了if循环,另外一个就是
函数里面没有return值,导致打印的时候为undefined,另外一个是写removeAt 方法的
时候,考虑条件不全,写出来会有报错,通过调试,打断点发现问题,解决问题。