<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>双向链表</title>
</head>
<body>
<script>
function DoublyLinkedList(){
function Node(data){
this.data = data;
this.next = null;
this.prev = null // 新添加的
}
this.head = null;
this.tail = null;
this.length = 0;
//拼接
DoublyLinkedList.prototype.append = function (data) {
var newNode = new Node(data);
if(this.head == null){
this.head = newNode;
this.tail = newNode;
}else {
var currentNode = this.head;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = newNode;
newNode.prev = currentNode;
this.tail = newNode;
}
this.length +=1;
}
//插入
DoublyLinkedList.prototype.insert = function (postion,data) {
if (postion<0 || postion> this.length) return false;
var newNode = new Node(data);
var currentNode = this.head;
//1判断原来列表是否为空
if (this.length == 0){
this.head = newNode;
this.tail = newNode;
}else {
//插入到首位
if(postion == 0){
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
}else if(postion == this.length)//插入到尾部
{
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}else {//中间插入
var currentNode = this.head;
var index = 0;
while (index++ <postion){
currentNode = currentNode.next;
}
newNode.prev = currentNode.prev;
newNode.next = currentNode;
currentNode.prev.next = newNode;
currentNode.prev = newNode;
}
this.length +=1;
return true;
}
}
//获取根据位置
DoublyLinkedList.prototype.get = function (position) {
if (position < 0 || position >= this.length) return null;
var currentNode = position *2 < this.length ? this.head : this.tail;
var index = 0;
if (position *2 < this.length){//正向查询
// currentNode = this.head;
while (position> index++){
currentNode = currentNode.next;
}
}else {//反向查询
// currentNode = this.tail;
index = this.length -1;
while (position < index--){
currentNode = currentNode.prev;
}
}
return currentNode.data;
}
//根据位置获取元素
DoublyLinkedList.prototype.indexOf = function (data) {
var currentNode = this.head;
var index = 0;
while (currentNode){
if(currentNode.data == data) return index;
else {
currentNode = currentNode.next;
index+=1;
}
}
return -1;
}
//更新数据根据位置和数据
DoublyLinkedList.prototype.update = function (position,data) {
if (position < 0 || position >= this.length) return false;
var currentNode = position *2 < this.length ? this.head : this.tail;
var index = 0;
if (position *2 < this.length){//正向查询
while (position> index++){
currentNode = currentNode.next;
}
}else {//反向查询
index = this.length -1;
while (position < index--){
currentNode = currentNode.prev;
}
}
currentNode.data = data;
return true;
}
//根据位置移除
DoublyLinkedList.prototype.removeAt = function (position) {
if (position < 0 || position >= this.length) return false;
if(this.length == 1){
this.head = null;
this.tail = null;
}else {
if (position == 0){
this.head = this.head.next;
this.head.prefix = null;
}else if(position == this.length -1){
this.tail = this.tail.prev;
this.tail.next = null;
}else {
var currentNode = position *2 < this.length ? this.head : this.tail;
var index = 0;
if (position *2 < this.length){//正向查询
while (position> index++){
currentNode = currentNode.next;
}
}else {//反向查询
index = this.length -1;
while (position < index--){
currentNode = currentNode.prev;
}
}
currentNode.prev.next = currentNode.next;
currentNode.next.prev = currentNode.prev;
}
}
return true;
}
//移除某元素
DoublyLinkedList.prototype.remove = function (data) {
var index = this.indexOf(data);
if(index == -1) return false;
return this.removeAt(index);
}
//是否为空
DoublyLinkedList.prototype.isEmpty = function () {
return this.length == 0;
}
//有多少元素
DoublyLinkedList.prototype.size = function () {
return this.length;
}
//元素内容
DoublyLinkedList.prototype.toString = function () {
return this.backwardString();
}
//
DoublyLinkedList.prototype.fowardString = 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;
}
}
var dLink = new DoublyLinkedList();
dLink.append('a');
dLink.append('b');
dLink.append('c');
dLink.append('d');
dLink.append('e');
// dLink.insert(0,'0');
// alert(dLink);
// dLink.insert(6,'F');
// alert(dLink);
// dLink.insert(5,'5');
// alert(dLink);
// alert(dLink.backwardString());
// alert(dLink.fowardString());
// alert(dLink.get(3));
// alert(dLink.indxOf('f'))
// dLink.update(4,'D');
// dLink.removeAt(4);
dLink.remove('m');
alert(dLink);
</script>
</body>
</html>