js 实现单向链表常用方法

89 阅读1分钟
    //单向链表的常用操作

    function LinkedList() {

        // 内部节点类
        function Node(element) {
            this.node = element;
            this.next = null;
        }

        //属性
        this.head = null;
        this.length = 0;

        // append() 向链表尾部添加一项
        LinkedList.prototype.append = function (element) {

            let node = new Node(element);

            if (this.length === 0) {
                this.head = node;
            } else {
                let current = this.head;

                while (current.next) {
                    current = current.next;
                }

                current.next = node;
            }

            this.length += 1;

        }
        // insert(position,element) 插入一项
        LinkedList.prototype.insert = function (position, element) {

            if (position < 0 || position > this.length) return false;

            let node = new Node(element);

            if (position == 0) {
                node.next = this.head;

                this.head = node;

            } else {

                let current = this.head,
                    index = 0,
                    prev;
                while (index++ < position) {
                    prev = current
                    current = current.next
                }

                prev.next = node;
                node.next = current;
            }


            this.length += 1;
        }
        // removeAt() 根据下标从列表移除
        LinkedList.prototype.removeAt = function (position) {
            if (position < 0 || position > this.length) return false;

            if (position == 0) {
                this.head = this.head.next;
            } else {

                let current = this.head,
                    index = 0,
                    prev;
                while (index++ < position) {
                    prev = current;
                    current = current.next;
                }

                prev.next = current.next;
            }

            this.length -= 1;

        }
        // remove 根据元素从列表移除
        LinkedList.prototype.remove = function (element) {
           
           let index = this.indexOf(element);
           this.removeAt(index)
        }
        // get 获取元素
        LinkedList.prototype.get = function (position) {

            if (position < 0 || position > this.length) return null;

            let current = this.head,
                index = 0;

            while (index++ < position) {

                current = current.next;
            }

            return current.node;

        }
        // indexOf 返回元素索引
        LinkedList.prototype.indexOf = function (element) {
            let current = this.head,
                index = 0;

            while (current) {
                if (current.node == element) {
                    return index;
                }
                index++;
                current = current.next;
            }

            return -1;
        }
        // update() 修改元素
        LinkedList.prototype.update = function (position, element) {

            if (position < 0 || position > this.length) return null;

            let current = this.head,
                index = 0;

            while (index++ < position) {

                current = current.next
            }

            current.node = element;

        }
        // isEmpty() 是否为空链
        LinkedList.prototype.isEmpty = function () {
            return !Boolean(this.length);
        }
        // size()  链表长度
        LinkedList.prototype.size = function () {
            return this.length;
        }
        // toString() 链表的 转 字符串
        LinkedList.prototype.toString = function () {

            let current = this.head,
                result = '',
                comma;

            while (current) {
                comma = current.next ? ',' : ''
                result += current.node + comma;
                current = current.next;
            }

            return result;


        }

    }

    let linked = new LinkedList();