本文实例讲述了JavaScript 双向链表操作。分享给大家供大家参考,具体如下:一个 双向链表(doubly linked list) 是由一组称为节点的顺序链接记录组成的链接数据结构。每个节点包含两个字段,称为链接,它们是对节点序列中上一个节点和下一个节点的引用doubly linked listdoubly linked list开始节点和结束节点的上一个链接和下一个链接分别指向某种终止节点,通常是前哨节点或null,以方便遍历列表。如果只有一个前哨节点,则列表通过前哨节点循环链接。它可以被概念化为两个由相同数据项组成的单链表,但顺序相反。
class DNode {
constructor(val) {

this.val = val;

this.prev = null;

this.next = null;
}
}


class DNode {
constructor(val) {

this.val = val;

this.prev = null;

this.next = null;
}
}

增加节点增加节点
function add(el) {

var currNode = this.head;

while (currNode.next != null) {

currNode = currNode.next;

}

var newNode = new DNode(el);

newNode.next = currNode.next;

currNode.next = newNode;
}


function add(el) {

var currNode = this.head;

while (currNode.next != null) {

currNode = currNode.next;

}

var newNode = new DNode(el);

newNode.next = currNode.next;

currNode.next = newNode;
}

查找
function find(el) {

var currNode = this.head;

while (currNode && currNode.el != el) {

currNode = currNode.next;

}

return currNode;
}


function find(el) {

var currNode = this.head;

while (currNode && currNode.el != el) {

currNode = currNode.next;

}

return currNode;
}

插入
function (newEl, oldEl) {

var newNode = new DNode(newEl);

var currNode = this.find(oldEl);

if (currNode) {

newNode.next = currNode.next;

newNode.prev = currNode;

currNode.next = newNode;

} else {

throw new Error('未找到指定要插入节点位置对应的值!')

}
}


function (newEl, oldEl) {

var newNode = new DNode(newEl);

var currNode = this.find(oldEl);

if (currNode) {

newNode.next = currNode.next;

newNode.prev = currNode;

currNode.next = newNode;

} else {

throw new Error('未找到指定要插入节点位置对应的值!')

}
}

展示
// 顺序
function () {

var currNode = this.head.next;

while (currNode) {

console.log(currNode.el);

currNode = currNode.next;

}
}

// 逆序
function () {

var currNode = this.head;

currNode = this.findLast();

while (currNode.prev != null) {

console(currNode.el);

currNode = currNode.prev;

}
}


// 顺序
function () {

var currNode = this.head.next;

while (currNode) {

console.log(currNode.el);

currNode = currNode.next;

}
}

// 逆序
function () {

var currNode = this.head;

currNode = this.findLast();

while (currNode.prev != null) {

console(currNode.el);

currNode = currNode.prev;

}
}

删除
function (el) {

var currNode = this.find(el);

if (currNode && currNode.next != null) {

currNode.prev.next = currNode.next;

currNode.next.prev = currNode.prev;

currNode.next = null;

currNode.previous = null;

} else {

throw new Error('找不到要删除对应的节点');

}
}


function (el) {

var currNode = this.find(el);

if (currNode && currNode.next != null) {

currNode.prev.next = currNode.next;

currNode.next.prev = currNode.prev;

currNode.next = null;

currNode.previous = null;

} else {

throw new Error('找不到要删除对应的节点');

}
}

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数学运算用法总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript数组操作技巧总结》、《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》、《JavaScript查找算法技巧总结》及《JavaScript错误与调试技巧总结》JavaScript数学运算用法总结JavaScript数据结构与算法技巧总结JavaScript数组操作技巧总结JavaScript排序算法总结JavaScript遍历算法与技巧总结JavaScript查找算法技巧总结JavaScript错误与调试技巧总结希望本文所述对大家JavaScript程序设计有所帮助。