• js实现双向链表, 双向链表需要增加一个previous属性


    双向链表,
    双向链表需要增加一个previous属性
    /*双向链表
    * */
    function Node(element) {
        this.element = element;
        this.next = null;
        this.previous = null;//双向链表在这里需要增加一个previous属性
    }
    
    function LList() {
        this.head = new Node("head");
        this.find = find;
        this.insert = insert;
        this.display = display;
        this.remove = remove;
        this.findLast = findLast;
        this.dispReverse = dispReverse;//将链表反转
    }
    
    function dispReverse() {
        var currNode = this.head;
        currNode = this.findLast();
        var nodestr = "";
        while (!(currNode.previous == null)) {
            nodestr += " "+currNode.element;
            currNode = currNode.previous;
        }
        console.log("将链表反转后: "+nodestr);
    }
    
    function findLast() {
        var currNode = this.head;
        while (!(currNode.next == null)) {
            currNode = currNode.next;
        }
        return currNode;
    }
    
    function remove(item) {
        var currNode = this.find(item);
        if (!(currNode.next == null)) {
            currNode.previous.next = currNode.next;
            currNode.next.previous = currNode.previous;
            currNode.next = null;
            currNode.previous = null;
        }
    }
    
    // findPrevious is no longer needed
    /*function findPrevious(item) {
     var currNode = this.head;
     while (!(currNode.next == null) &&
     (currNode.next.element != item)) {
     currNode = currNode.next;
     }
     return currNode;
     }*/
    
    function display() {
        var currNode = this.head;
        var nodestr = "";
        while (!(currNode.next == null)) {
            nodestr += " "+currNode.next.element;
            currNode = currNode.next;
        }
        console.log(nodestr);
    }
    
    function find(item) {
        var currNode = this.head;
        while (currNode.element != item) {
            currNode = currNode.next;
        }
        return currNode;
    }
    
    function insert(newElement, item) {
        var newNode = new Node(newElement);
        var current = this.find(item);
        newNode.next = current.next;
        newNode.previous = current;//双向链表在这里需要设置新节点previous属性
        current.next = newNode;
    }
    
    
    var cities = new LList();
    cities.insert("Conway", "head");
    cities.insert("Russellville", "Conway");
    cities.insert("Carlisle", "Russellville");
    cities.insert("Alma", "Carlisle");
    cities.display();//Conway Russellville Carlisle Alma
    cities.remove("Carlisle");
    cities.display();//Conway Russellville Alma
    cities.dispReverse();// Alma Russellville Conway
  • 相关阅读:
    [bbk2908]第4集 Chapter 03 介绍RAC的体系结构
    [bbk3011]第8集 Chapter 05 介绍RAC安装过程概述
    [bbk3100]第7集 Chapter 04 介绍RAC中CVU工具的使用
    [bbk2907]第3集 Chapter 02 RAC的安装过程中需要注意的要点
    [bbk2905]第1集 Chapter 01 介绍RAC概述
    [bbk2906]第2集 Chapter 02 介绍RAC概述
    RAC之CRS架构简介
    NOIP普及组2017比赛总结
    struct和typedef
    KMP详解(转)
  • 原文地址:https://www.cnblogs.com/baiyangyuanzi/p/6689531.html
Copyright © 2020-2023  润新知