JavaScript实现单项链表

8 篇文章 0 订阅
2 篇文章 0 订阅
function LinkList() {

    function LinkNode(data) {
        this.data = data;
        this.next = null;
    }

    this.length = 0;

    this.head = new LinkNode();

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

    function clear() {
        this.traverse(function(elem, index) {
            delete elem;
        });
        this.head.next = null;
        this.length = 0;
    }

    function get(index) {
        if (index < 0) {
            index = this.length + index - 1;
        }
        if (index < 0 || index >= this.length) {
            return null;
        }
        var nowElem = this.head.next;
        var i = 0;
        while (i < index) {
            nowElem = nowElem.next;
            i++;
        }
        return nowElem.data;
    }

    function set(data, index) {
        if (index < 0) {
            index = this.length + index - 1;
        }
        if (index < 0 || index >= this.length) {
            return false;
        }
        var nowElem = this.head.next;
        var i = 0;
        while (i < index) {
            nowElem = nowElem.next;
            i++;
        }
        nowElem.data = data;
        return true;
    }

    function traverse(callback) {
        if (Object.prototype.toString.call(callback) !== "[object Function]") {
            throw new TypeError("callback is not a function");
        }

        if (!this.isEmpty()) {
            var nowElem = this.head,
            nextElem = nowElem.next;
            var index = 0;
            while (nextElem) {
                nowElem = nextElem;
                nextElem = nextElem.next;
                callback(nowElem, index++, this);
            }

        }
    }

    function indexOf(data) {
        var index = -1;
        if (this.length > 0) {
            var nowElem = this.head.next;
            var i = 0;
            while (i < this.length && nowElem.data !== data) {
                nowElem = nowElem.next;
                i++;
            }
            if (i < this.length) {
                index = i;
            }
        }
        return index;
    }

    function insert(data, index) {
        index = index || 0;
        if (this.length > 0) {
            var i = 0;
            var nowElem = this.head;
            if (index < 0) {
                index = this.length + index;
                if (index < 0) {
                    index = 0;
                }
            }
            while (i < this.length && i < index) {
                nowElem = nowElem.next;
                i++;
            }
            var nextElem = nowElem.next;
            nowElem.next = new LinkNode(data);
            nowElem.next.next = nextElem;
        } else {
            this.head.next = new LinkNode(data);
        }
        this.length++;
    }

    function append(data) {
        var nowElem = this.head;
        for (var i = 0; i < this.length; i++) {
            nowElem = nowElem.next;
        }
        nowElem.next = new LinkNode(data);
        this.length++;
    }

    function remove(index) {
        if (index < 0) {
            index = this.length + index - 1;
        }
        if (index > 0 && index < this.length) {
            var nowElem = this.head;
            for (var i = 0; i < this.length; i++) {
                if (i === index) {
                    var deleteElem = nowElem.next;
                    nowElem.next = deleteElem.next;
                    delete deleteElem;
                    this.length--;
                    return true;
                }
                nowElem = nowElem.next;
            }
        }
        return false;
    }

    this.clear = clear;
    this.get = get;
    this.set = set;
    this.traverse = traverse;
    this.indexOf = indexOf;
    this.append = append;
    this.insert = insert;
    this.remove = remove;
    this.isEmpty = isEmpty;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值