使用js封装单向链表

一、单向链表的优点

进行数据的删除、插入简单;

二、单向链表的缺点

进行数据查找时比较慢;

三、使用类进行封装

class Node {
    constructor(element) {
        this.element = element;
        this.next = null;
    }
}


class LinkList {

    constructor() {
        this.count = 0;
        this.head = null;
    }

    push(data) {

        const node = new Node(data);
        if (this.head === null) {
            this.head = node;
        } else {
            let current = this.head;
            while (current.next !== null) {
                current = current.next;
            }
            current.next = node;
        }
        this.count++;
    }

    removeAt(index) {
        if (index >= 0 && index < this.count) {
            let current = this.head;
            if (index == 0) {
                this.head = this.head.next;
            } else {
                let previous;

                for (let i = 0; i < index; i++) {
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
            }
            this.count--;
            return current.element;
        }
        return false
    }

    removeAt2(index) {
        if (index >= 0 && index < this.count) {
            let current = this.head;
            if (index == 0) {
                this.head = this.head.next;
            } else {
                let previous = this.getNodeAt(index - 1);
                current = previous.next;
                previous.next = current.next;
            }
            this.count--;
            return current.element;
        }
    }

    equal(a, b) {
        // return a=== b;
        return JSON.stringify(a) === JSON.stringify(b);
        // imumutable 判断数据相等的插件库
    }

    indexOf(element) {
        let current = this.head;
        for (let i = 0; i < this.count; i++) {
            if (current.element == element) {
                return i;
            }
            current = current.next;

        }
    }

    remove(element) {
        const index = this.indexOf(element);
        this.removeAt(index);
    }

    getNodeAt(index) {
        if (index >= 0 && index < this.count) {
            let node = this.head;
            for (let i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        }
        return false
    }

    insert(element, index) {
        if (0 <= index || index <= this.count) {
            let node = new Node(element);
            if (index == 0) {
                let current = this.head;
                node.next = current;
                this.head = node;
            } else {
                let previous = this.getNodeAt(index - 1);
                let next = previous.next;
                node.next = next;
                previous.next = node;
            }
            this.count++;
            return true;
        }
        return false;
    }

    size() {
        return this.count;
    }

    isEmpty() {
        return this.size() === 0;
    }

    getHead() {
        return this.head;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值