javascript普通链表及双向链表

简介: 写代码的真是心细啊,每一步操作的先后顺序都在卡准。 我其实只是理解了思想和大概的操作。 真正要用时,可能还是要复制,粘贴。。。:) function LinkedList(){ var Node = function(element){ this.

写代码的真是心细啊,每一步操作的先后顺序都在卡准。

我其实只是理解了思想和大概的操作。

真正要用时,可能还是要复制,粘贴。。。:)

function LinkedList(){
    var Node = function(element){
        this.element = element;
        this.next = null;
    };

    var length = 0;
    var head = null;

    this.append = function(element){
        var node = new Node(element),
            current;

        if (head == null){
            head = node;
        } else {
            current = head;
            while (current.next){
                current = current.next;
            }
            current.next = node;
        }
        length++;
    };

    this.insert = function(position, element){
        if (position >=0 && position <=length){
            var node = new Node(element),
                current = head,
                previous,
                index =0;
            if (position === 0){
                node.next = current;
                head = node;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;
            }
            length++;
            return true;
        } else {
            return false;
        }
    };

    this.removeAt = function(position){
        if (position > -1 && position < length){
            var current = head,
                previous,
                index = 0;
            if (position === 0){
                head = current.next;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    };

    this.remove = function(element){
        var index = this.indexOf(element);
        return this.removeAt(index);
    };

    this.indexOf = function(element){
        var current = head,
            index = -1;
        while (current){
            if (element == current.element){
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    };

    this.isEmpty = function(){
        return length === 0;
    };

    this.size = function () {
        return length;
    };

    this.toString = function(){
        var current = head,
            string = '';
        while (current){
            string += current.element;
            current = current.next;
        }
        return string;
    };

    this.getHead = function(){
        return head;
    }

    this.print = function(){

    };
}

var list = new LinkedList();
list.append(15);
list.append(10);
console.log(list.toString());

function DoublyLinkedList() {
    var Node = function(element){
        this.element = element;
        this.next = null;
        this.prev = null;
    };

    var length = 0;
    var head = null;
    var tail = null;

    this.insert = function(position, element){
        if(position >=0 && position <= length){
            var node = new Node(element),
                current = head,
                previous,
                index = 0;

            if (position === 0){
                if (!head){
                    head = node;
                    tail = node;
                } else {
                    node.next = current;
                    current.prev = node;
                    head = node;
                }
            } else if (position === length){
                current = tail;
                current.next = node;
                node.prev = current;
                tail = node;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;

                current.prev = node;
                node.prev = previous;
            }
            length++;
            return true;
        } else {
            return false;
        }
    }

    this.removeAt = function(position){
        if (position > -1 && position < length){
            var current = head,
                previous,
                index = 0;

            if (position === 0){
                head = current.next;
                if (length === 1){
                    tail = null;
                } else {
                    head.prev = null;
                }
            } else if(position == length-1){
                current = tail;
                tail = current.prev;
                tail.next = null;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
                current.next.prev = previous;
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    }
}

目录
相关文章
uni-app 126删除指定消息记录
uni-app 126删除指定消息记录
152 0
|
存储 边缘计算 容灾
阿里云资深技术专家李克畅谈边缘云计算趋势与实践
2021年5月15日,以“相信边缘的力量”为主题的全球边缘计算大会在深圳成功召开。 阿里云资深技术专家李克,分享阿里云在边缘云计算的探索和实践,如何为行业提供广覆盖、低成本、高可靠的边缘基础设施。
3240 1
阿里云资深技术专家李克畅谈边缘云计算趋势与实践
|
弹性计算 对象存储
钉钉案例
关于钉钉
|
运维 安全 大数据
网络运维团队如何应对最新的黑客威胁?
本文讲的是网络运维团队如何应对最新的黑客威胁?,企业网络上的数据存在极高的价值,因此成为恶意黑客觊觎的目标。重大安全事件几乎已经成为了日常新闻,而网络团队对现状负有的责任越来越重。
1133 0
|
4月前
|
人工智能 监控 安全
财务自动化办公:银企互联优质工具分享
财务常遇多银行账户管理难、银企直连开通成本高、虚拟户对接受限等问题。RPA财务机器人可自动登录各网银,实时查余额、下流水、取回单、生成台账,无需每家银行单独对接,免收银企直联费用,真正降本增效。(239字)
|
9天前
|
人工智能 JSON 安全
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
阿里云AI安全产品联动防御Fastjson攻击
2334 12
Fastjson远程代码执行漏洞,阿里云AI安全为您保驾护航
|
9天前
|
云安全 人工智能 安全
|
9天前
|
人工智能 自然语言处理 数据挖掘
Qwen3.8-Max-Preview深度全解析:2.4万亿参数旗舰MoE模型+Token Plan限时优惠完整落地指南
2026年7月,全新旗舰级混合专家大模型Qwen3.8-Max-Preview正式开放抢先体验,作为通义千问Qwen3系列规格最高、综合推理能力顶尖的新一代模型,该模型总参数量达到2.4万亿(2.4T),是当前线上可调用的原生多模态旗舰模型,综合推理水准对标海外顶级Fable 5模型,在复杂工程开发、长文档深度分析、多步骤智能体自治、跨境多语言创作、海量数据挖掘五大高难度业务场景实现跨越式性能提升。
1053 2