5JavaScript数据结构与算法--链表

版权声明:本文为作者的原创文章,未经允许不得转载。 https://blog.csdn.net/lin5165352/article/details/81519174

链表用来存储有序元素的集合。

function LinkedList() {
 
    let Node = function(element){
 
        this.element = element;
        this.next = null;
    };
 
    let length = 0;
    let head = null;
 
    this.append = function(element){
 
        let node = new Node(element),
            current;
 
        if (head === null){ //first node on list
            head = node;
        } else {
 
            current = head;
 
            //loop the list until find last item
            while(current.next){
                current = current.next;
            }
 
            //get last item and assign next to added item to make the link
            current.next = node;
        }
 
        length++; //update size of list
    };
 
    this.insert = function(position, element){
 
        //check for out-of-bounds values
        if (position >= 0 && position <= length){
 
            let node = new Node(element),
                current = head,
                previous,
                index = 0;
 
            if (position === 0){ //add on first position
 
                node.next = current;
                head = node;
 
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;
            }
            length++; //update size of list
            return true;
        } else {
            return false;
        }
    };
    this.removeAt = function(position){
        //check for out-of-bounds values
        if (position > -1 && position < length){
            let current = head,
                previous,
                index = 0;
            //removing first item
            if (position === 0){
                head = current.next;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                //link previous with current's next - skip it to remove
                previous.next = current.next;
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    };
    this.remove = function(element){
        let index = this.indexOf(element);
        return this.removeAt(index);
    };
    this.indexOf = function(element){
        let current = head,
            index = 0;
        while (current) {
            if (element === current.element) {
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    };
    this.isEmpty = function() {
        return length === 0;
    };
    this.size = function() {
        return length;
    };
    this.getHead = function(){
        return head;
    };
    this.toString = function(){
        let current = head,
            string = '';
        while (current) {
            string += current.element + (current.next ? ', ' : '');
            current = current.next;
        }
        return string;
    };
    this.print = function(){
        console.log(this.toString());
    };
}

链表es6闭包的形式。

let LinkedList2 = (function () {
 
    class Node {
        constructor(element){
            this.element = element;
            this.next = null;
        }
    }
 
    const length = new WeakMap();
    const head = new WeakMap();
 
    class LinkedList2 {
 
        constructor () {
            length.set(this, 0);
            head.set(this, null);
        }
 
        append(element) {
 
            let node = new Node(element),
                current;
 
            if (this.getHead() === null) { //first node on list
                head.set(this, node);
            } else {
 
                current = this.getHead();
 
                //loop the list until find last item
                while (current.next) {
                    current = current.next;
                }
 
                //get last item and assign next to added item to make the link
                current.next = node;
            }
 
            //update size of list
            let l = this.size();
            l++;
            length.set(this, l);
        }
 
        insert(position, element) {
 
            //check for out-of-bounds values
            if (position >= 0 && position <= this.size()) {
 
                let node = new Node(element),
                    current = this.getHead(),
                    previous,
                    index = 0;
 
                if (position === 0) { //add on first position
 
                    node.next = current;
                    head.set(this, node);
 
                } else {
                    while (index++ < position) {
                        previous = current;
                        current = current.next;
                    }
                    node.next = current;
                    previous.next = node;
                }
                //update size of list
                let l = this.size();
                l++;
                length.set(this, l);
                return true;
            } else {
                return false;
            }
        }
        removeAt(position) {
            //check for out-of-bounds values
            if (position > -1 && position < this.size()) {
                let current = this.getHead(),
                    previous,
                    index = 0;
                //removing first item
                if (position === 0) {
                    head.set(this, current.next);
                } else {
                    while (index++ < position) {
                        previous = current;
                        current = current.next;
                    }
                    //link previous with current's next - skip it to remove
                    previous.next = current.next;
                }
                let l = this.size();
                l--;
                length.set(this, l);
                return current.element;
            } else {
                return null;
            }
        }
        remove(element) {
            let index = this.indexOf(element);
            return this.removeAt(index);
        }
        indexOf(element) {
            let current = this.getHead(),
                index = 0;
            while (current) {
                if (element === current.element) {
                    return index;
                }
                index++;
                current = current.next;
            }
            return -1;
        }
        isEmpty() {
            return this.size() === 0;
        }
        size() {
            return length.get(this);
        }
        getHead() {
            return head.get(this);
        }
        toString() {
            let current = this.getHead(),
                string = '';
            while (current) {
                string += current.element + (current.next ? ', ' : '');
                current = current.next;
            }
            return string;
        }
        print() {
            console.log(this.toString());
        }
    }
    return LinkedList2;
})();

猜你喜欢

转载自blog.csdn.net/lin5165352/article/details/81519174