LinkedList

//链表
        //内存中不是连续的空间,元素本身节点和指向下一个元素的引用
        //单向链表
            function LinkedList() {
                function Node(data) {
                    this.data = data;
                    this.next = null;
                }
                this.head = null;
                this.length = 0;

                LinkedList.prototype.append = function(data) {
                    if(this.length == 0) {
                        var newNode = new Node(data);
                        this.head = newNode;
                    }else{
                        var newNode = new Node(data);
                        var current = this.head;
                        while(current.next) {
                            current = current.next;
                        }
                        current.next = newNode;
                    }
                    this.length += 1;
                }

                LinkedList.prototype.toString = function() {
                    var current = this.head;
                    var listString = "";
                    while(current) {
                        listString += current.data + "";
                        current = current.next;
                    }
                    return listString;
                }

                LinkedList.prototype.insert = function(position, data) {
                    if(position < 0 || position > this.length) return false;
                        var newNode = new Node(data);
                        if(position == 0) {
                            newNode.next = this.head;
                            this.head = newNode;
                        }else{
                            var index = 0;
                            var current = this.head;
                            var previous = null;
                            while(index++ < position) {
                                previous = current;
                                current = current.next;
                            }
                            newNode.next = current;
                            previous.next = newNode;
                        }
                        this.length += 1;
                        return true;
                }

                LinkedList.prototype.get = function(position) {
                    if(position < 0 || position >= this.length) return null;
                    var current = this.head;
                    var index = 0;
                    while(index++ < position) {
                        current = current.next;
                    }
                    return current.data;
                }

                LinkedList.prototype.indexOf = function(data) {
                    var current = this.head;
                    var index = 0;
                    while(current) {
                        if(current.data == data) {
                            return index;
                        }
                        current = current.next;
                        index += 1;
                    }
                    return -1;
                }

                LinkedList.prototype.update = function(position, newData) {
                    if(position < 0 || position >= this.length) return false;
                    var current = this.head;
                    var index = 0;
                    while(index++ < position) {
                        current = current.next;
                    }
                    current.data = newData;
                    return true;
                }

                LinkedList.prototype.removeAt = function(position) {
                    if(position < 0 || position >= this.length) return false;
                    if(position == 0) {
                        this.head = this.head.next;
                    }else{
                        var index = 0;
                        var current = this.head;
                        var previous = null;
                        while(index++ < position) {
                            previous = current;
                            current = current.next;
                        }
                        previous.next = current.next;
                    }
                    this.length -= 1;
                    return true;
                }

                LinkedList.prototype.remove = function(data) {
                    var position = this.indexOf(data);
                    return this.removeAt(position);
                }
            }      

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值