• 双向链表的实现


        function Node(element) {
            this.element = element;
            this.next = null;
            this.previous = null;
        }
        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 find(item) {
            var currNode = this.head;
            while (currNode.element != item) {
                currNode = currNode.next;
            }
            return currNode;
        }
        function insert(newElement, item) {//在item后面插入新节点
            var newNode = new Node(newElement);
            var current = this.find(item);
            newNode.next = current.next;
            newNode.previous = current;
            current.next = newNode;
        }
        function display() {
            var currNode = this.head;
            while (!(currNode.next == null)) {
                document.write(currNode.next.element + " ");
                currNode = currNode.next;
            }
        }
        function remove(item) {
            var currNode = this.find(item);
            if ((currNode.element != null) && (currNode.next == null)) {
                currNode.previous.next = null;
                currNode.previous = null;
            } else if (currNode.element != null) {
                currNode.previous.next = currNode.next;//当前节点的前一个节点的链接指向当前节点的后一个节点            
                currNode.next.previous = currNode.previous;//当前节点的后一个节点的链接指向当前节点的前一个节点
                currNode.next = null;//将当前节点的后向指针置空
                currNode.previous = null;//将当前节点的前向指针置空 
            }
        }
        
        function findLast() {
            var currNode = this.head;
            while (currNode.next != null) {//针对于只有一个空节点的情况
                currNode = currNode.next;
            }
            return currNode;
        }
        //反序显示双向链表中的元素
        function dispReverse() {
            var currNode = this.head;
            currNode = this.findLast();
            while (currNode.previous != null) {//head节点的前向指针指向null
                document.write(currNode.element + " ");
                currNode = currNode.previous;
            }
        }
        var cities = new LList();
        cities.insert("Conway", "head");
        cities.insert("Russellville", "Conway");
        cities.insert("Carlisle", "Russellville");
        cities.insert("Alma", "Carlisle");
        cities.display();
        document.write("<br />");
        cities.remove("Alma");
        cities.display();
        document.write("<br />");
        cities.dispReverse();
        document.write("<br />");
        cities.remove("Conway");
        cities.display();
  • 相关阅读:
    【UML】基本介绍与类图(依赖、泛化、实现、关联、聚合、组合关系)
    【Java】集合综合案例
    【Java】集合框架(List Set Map)
    【Java】面向对象
    【Java】单例模式(Singleton)
    正则表达式(括号)、[中括号]、{大括号}的区别
    js正则表单验证汇总,邮箱验证,日期验证,电话号码验证,url验证,信用卡验证,qq验证
    网页倒计时,动态显示"××年还剩××天××时××分××秒"
    判断鼠标是否在指定区域代码
    鼠标左中右键返回值的兼容性问题
  • 原文地址:https://www.cnblogs.com/feile/p/5384029.html
Copyright © 2020-2023  润新知