双向链表,
双向链表需要增加一个prevIoUs属性
<span style="color: #0000ff;">this.head = <span style="color: #0000ff;">new Node("head"<span style="color: #000000;">);
<span style="color: #0000ff;">this.find =<span style="color: #000000;"> find;
<span style="color: #0000ff;">this.insert =<span style="color: #000000;"> insert;
<span style="color: #0000ff;">this.display =<span style="color: #000000;"> display;
<span style="color: #0000ff;">this.remove =<span style="color: #000000;"> remove;
<span style="color: #0000ff;">this.findLast =<span style="color: #000000;"> findLast;
<span style="color: #0000ff;">this.dispReverse = dispReverse;<span style="color: #008000;">//<span style="color: #008000;">将链表反转
<span style="color: #000000;">}
<span style="color: #0000ff;">function<span style="color: #000000;"> dispReverse() {
<span style="color: #0000ff;">var currNode = <span style="color: #0000ff;">this<span style="color: #000000;">.head;
currNode = <span style="color: #0000ff;">this<span style="color: #000000;">.findLast();
<span style="color: #0000ff;">var nodestr = ""<span style="color: #000000;">;
<span style="color: #0000ff;">while (!(currNode.prevIoUs == <span style="color: #0000ff;">null<span style="color: #000000;">)) {
nodestr += " "+<span style="color: #000000;">currNode.element;
currNode =<span style="color: #000000;"> currNode.prevIoUs;
}
console.log("将链表反转后: "+<span style="color: #000000;">nodestr);
}
<span style="color: #0000ff;">function<span style="color: #000000;"> findLast() {
<span style="color: #0000ff;">var currNode = <span style="color: #0000ff;">this<span style="color: #000000;">.head;
<span style="color: #0000ff;">while (!(currNode.next == <span style="color: #0000ff;">null<span style="color: #000000;">)) {
currNode =<span style="color: #000000;"> currNode.next;
}
<span style="color: #0000ff;">return<span style="color: #000000;"> currNode;
}
<span style="color: #0000ff;">function<span style="color: #000000;"> remove(item) {
<span style="color: #0000ff;">var currNode = <span style="color: #0000ff;">this<span style="color: #000000;">.find(item);
<span style="color: #0000ff;">if (!(currNode.next == <span style="color: #0000ff;">null<span style="color: #000000;">)) {
currNode.prevIoUs.next =<span style="color: #000000;"> currNode.next;
currNode.next.prevIoUs =<span style="color: #000000;"> currNode.prevIoUs;
currNode.next = <span style="color: #0000ff;">null<span style="color: #000000;">;
currNode.prevIoUs = <span style="color: #0000ff;">null<span style="color: #000000;">;
}
}
<span style="color: #008000;">//<span style="color: #008000;"> findPrevIoUs is no longer needed<span style="color: #008000;">
/<span style="color: #008000;">function findPrevIoUs(item) {
var currNode = this.head;
while (!(currNode.next == null) &&
(currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}<span style="color: #008000;">/
<span style="color: #0000ff;">function<span style="color: #000000;"> display() {
<span style="color: #0000ff;">var currNode = <span style="color: #0000ff;">this<span style="color: #000000;">.head;
<span style="color: #0000ff;">var nodestr = ""<span style="color: #000000;">;
<span style="color: #0000ff;">while (!(currNode.next == <span style="color: #0000ff;">null<span style="color: #000000;">)) {
nodestr += " "+<span style="color: #000000;">currNode.next.element;
currNode =<span style="color: #000000;"> currNode.next;
}
console.log(nodestr);
}
<span style="color: #0000ff;">function<span style="color: #000000;"> find(item) {
<span style="color: #0000ff;">var currNode = <span style="color: #0000ff;">this<span style="color: #000000;">.head;
<span style="color: #0000ff;">while (currNode.element !=<span style="color: #000000;"> item) {
currNode =<span style="color: #000000;"> currNode.next;
}
<span style="color: #0000ff;">return<span style="color: #000000;"> currNode;
}
<span style="color: #0000ff;">function<span style="color: #000000;"> insert(newElement,item) {
<span style="color: #0000ff;">var newNode = <span style="color: #0000ff;">new<span style="color: #000000;"> Node(newElement);
<span style="color: #0000ff;">var current = <span style="color: #0000ff;">this<span style="color: #000000;">.find(item);
newNode.next =<span style="color: #000000;"> current.next;
newNode.prevIoUs = current;<span style="color: #008000;">//<span style="color: #008000;">双向链表在这里需要设置新节点prevIoUs属性
current.next =<span style="color: #000000;"> newNode;
}
<span style="color: #0000ff;">var cities = <span style="color: #0000ff;">new<span style="color: #000000;"> LList();
cities.insert("Conway","head"<span style="color: #000000;">);
cities.insert("Russellville","Conway"<span style="color: #000000;">);
cities.insert("Carlisle","Russellville"<span style="color: #000000;">);
cities.insert("Alma","Carlisle"<span style="color: #000000;">);
cities.display();<span style="color: #008000;">//<span style="color: #008000;">Conway Russellville Carlisle Alma
cities.remove("Carlisle"<span style="color: #000000;">);
cities.display();<span style="color: #008000;">//<span style="color: #008000;">Conway Russellville Alma
cities.dispReverse();<span style="color: #008000;">//<span style="color: #008000;"> Alma Russellville Conway
原文链接:https://www.f2er.com/js/403379.html