JS数据结构和算法
认识双向链表



双向链表封装
- 属性和内部类
<script>
function DoublyLinkedList() {
function Node(data) {
this.data = data;
this.prev = null;
this.next = null;
}
this.head = null;
this.tail = null;
this.length = 0;
}
</script>
- 双向链表方法

- 双向链表方法实现
<!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(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 {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
this.length += 1;
}
DoublyLinkedList.prototype.forwardString = function () {
var current = this.tail;
var forwardStr = '';
while (current) {
forwardStr += ' ' + current.data;
current = current.prev;
}
return forwardStr.trim();
}
DoublyLinkedList.prototype.backString = function () {
var current = this.head;
var backStr = '';
while (current) {
backStr += ' ' + current.data;
current = current.next;
}
return backStr.trim();
}
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) {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
} else if (position === this.length) {
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
} else if(position <= this.length/2) {
console.log("中上位置")
var index = 0;
var current = this.head;
while (index < position) {
current = current.next;
index += 1;
}
newNode.prev = current.prev;
current.prev.next = newNode;
newNode.next = current;
current.prev = newNode;
} else {
console.log("中下位置")
var index = this.length - 1;
var current = this.tail;
while (index > position) {
current = current.prev;
index -= 1;
}
newNode.prev = current.prev;
current.prev.next = newNode;
newNode.next = current;
current.prev = newNode;
}
}
this.length += 1;
return true;
}
DoublyLinkedList.prototype.get = function (position) {
if (position < 0 || position >= this.length || typeof position !== 'number') {
return false;
}
var current = this.head;
var index = 0;
while (index < position) {
current = current.next;
index += 1;
}
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, data) {
if (position < 0 || position >= this.length || typeof position !== 'number') {
return false;
}
var current = this.head;
var index = 0;
while (index < position) {
current = current.next;
index += 1;
}
current.data = data;
return true;
}
DoublyLinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length || typeof position !== 'number') {
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;
index += 1;
}
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
this.length -= 1;
return current.data;
}
DoublyLinkedList.prototype.remove = function (data) {
var position = this.indexOf(data);
return this.removeAt(position);
}
DoublyLinkedList.prototype.isEmpty = function () {
return this.length === 0;
}
DoublyLinkedList.prototype.size = function () {
return this.length;
}
}
var dlist = new DoublyLinkedList();
dlist.append(123);
dlist.append(456);
dlist.append(789);
console.log(dlist);
console.log(dlist.backString());
console.log(dlist.forwardString());
dlist.insert(0, "first");
console.log(dlist.backString())
dlist.insert(4, "last");
console.log(dlist.backString())
dlist.insert(2, "insert-Index-2");
console.log(dlist.backString())
dlist.insert(5, "insert-Last-2");
console.log(dlist.backString())
console.log(dlist.removeAt(0));
console.log(dlist.removeAt(12));
</script>
</body>
</html>