js实现链表

//实现链表

function LinkedList() {

    // Node辅助类,表示要加入列表的项,element是即将添加到列表的值,next是指向列表中下一个节点项的指针
    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);
        var current;
        if (head === null) { // 列表中第一个节点
            head = node;
        } else {
            current = head;
            while (current.next) {
                current = current.next; // 找到最后一项,是null
            }
            current.next = node; // 给最后一项赋值
        }
        length++; // 更新列表的长度
    }

    // 从链表中移除指定位置元素
    this.removeAt = function (position) {
        if (position > -1 && position < length) { // 值没有越界
            var current = head;
            var previous, index = 0;
            if (position === 0) { //  移除第一项
                head = current.next;
            } else {
                while (index++ < position) {
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;// 将previous与current的下一项连接起来,跳过current,从而移除
            }
            length--; // 更新列表的长度
            return current.element;
        } else {
            return null;
        }
    }

    // 在链表任意位置插入一个元素
    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与current的下一项之间插入node
                previous.next = node;
            }
            length++;
            return true;
        } else {
            return false;
        }
    }

    // 把链表内的值转换成数组
    this.toArr = function () {
        var current = head;
        var arr = [];
        while (current) {
            arr.push(current.element);
            current = current.next;
        }
        return arr;
    }

  // 把链表内的值转换成一个字符串
    this.toString = function () {
        var current = head;
        var string = '';
        while (current) {
            string += current.element + ',';
            current = current.next;
        }
        return string.substring(0, string.length-1);
    }

    // 在链表中查找元素并返回索引值
    this.indexOf = function (element) {
        var current = head,
            index = 0;
        while (current) {
            if (element == current.element) {
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    }

    // 从链表中移除指定元素
    this.remove = function (element) {
        var index = this.indexOf(element);
        return this.removeAt(index);
    }

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

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

    this.getHead = function () {
        return head;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值