以下图片来自:http://www.360doc.com/content/17/0427/12/30427643_649056362.shtml


// 双链表
/**
* get(index) :获取链表中第 index 个节点的值。如果索引无效,则返回 - 1。\
* addAtHead(val) :在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
* addAtTail(val) :将值为 val 的节点追加到链表的最后一个元素。
* addAtIndex(index, val) :在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
* deleteAtIndex(index) :如果索引 index 有效,则删除链表中的第 index 个节点。
*/
function doubleLinkedlList () {
function Node (val) {
this.val = val;
this.prev = null;
this.next = null;
}
this.head = null;
this.length = 0;
doubleLinkedlList.prototype.get = function (index) {
if (index < 0 || index > this.length - 1) return -1;
let i = 0, p = this.head;
while (p && i < index) {
p = p.next;
i++;
}
return p.val;
}
doubleLinkedlList.prototype.tostring = function () {
let p = this.head, result = "";
while (p) {
result += p.val + ' ';
p = p.next;
}
return result;
}
doubleLinkedlList.prototype.addAtHead = function (val) {
let newNode = new Node(val);
if (this.length === 0) {
this.head = newNode;
} else {
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
}
this.length++;
}
doubleLinkedlList.prototype.addAtTail = function (val) {
let newNode = new Node(val);
if (this.length === 0) {
this.head = newNode;
} else {
let p = this.head;
while (p.next) {
p = p.next;
}
p.next = newNode;
newNode.prev = p;
}
this.length++;
}
doubleLinkedlList.prototype.addAtIndex = function (index, val) {
if (index <= 0) {
this.addAtHead(val)
} else if (index > this.length) {
return false;
} else if (index === this.length) {
this.addAtTail(val);
} else {
let i = 0, p = this.head;
let newNode = new Node(val);
// 其实这里并不需要加p.next,因为这种情况是一定在首位之间的
while (i < index && p.next) {
p = p.next;
i++;
}
// 插入到p之前
newNode.prev = p.prev;
p.prev.next = newNode;
p.prev = newNode;
newNode.next = p;
this.length++;
}
}
doubleLinkedlList.prototype.deleteAtIndex = function (index) {
if (index < 0 || index > this.length - 1) return false;
let i = 0, p = this.head;
/**
* 1.删除头结点 this.head = this.head.next;
* 2.删除尾结点 p.prev.next = p.next;
* 3.删除非头尾节点 p.prev.next = p.next; p.next.prev = p.prev;
*/
if (index === 0) {
this.head = this.head.next;
} else {
while (i < index) {
p = p.next;
i++;
}
p.prev.next = p.next;
//如果不是末尾,需要后一个节点的前指针
if (index !== this.length - 1) {
p.next.prev = p.prev;
}
}
this.length--;
}
}
//测试
let list = new doubleLinkedlList();
list.addAtHead(1);
console.log(list.tostring());
list.addAtHead(2);
list.addAtHead(3);
console.log(list.tostring());
list.addAtTail(4);
list.addAtTail(5);
console.log(list.tostring());
list.addAtIndex(0, 6);
console.log(list.tostring());
list.addAtIndex(1, 8);
console.log(list.tostring());
list.addAtIndex(7, 9);
console.log(list.tostring());
list.addAtIndex(4, 10);
console.log(list.tostring());
console.log("删除");
list.deleteAtIndex(0);
console.log(list.tostring());
list.deleteAtIndex(1);
console.log(list.tostring());
list.deleteAtIndex(6);
console.log(list.tostring());
list.deleteAtIndex(7);
console.log(list.tostring());
双链表操作实现
本文介绍了一种使用JavaScript实现双链表的方法,并详细说明了如何进行节点的获取、添加及删除等基本操作。
535

被折叠的 条评论
为什么被折叠?



