JS实现单向链表

88 阅读1分钟

链表(Linked list) 通过“指针”将一组零散的内存块串联起来使用,它并不需要一块连续的内存空间。

  • 优点:存储空间不必事先分配,在需要存储空间的时候可以临时申请,不会造成空间的浪费;链表最大的优点在于可以灵活的添加和删除元素,插入、移动、删除元素的时间效率远比数组高。
  • 缺点:不仅数据元素本身的数据信息要占用存储空间,指针也需要占用存储空间,链表结构比数组结构的空间开销大。
class Node{
    next = null;
    constructor(val){
        this.val = val;
    }
}
class SingleLinkedList{
    head = null;
    tail = null;
    size = 0;
    constructor(Node){
        this.init(Node);
    }
    init(Node){
        this.nodeInstance = new Node();
        this.generateNode = (data)=>{
            return new Node(data)
        };
    }
    append(data){
        const node = this.generateNode(data);
        if(this.head === null) {
            this.head = node;
            this.tail = node;
        }else {
            this.tail.next = node;
        }
        this.tail = node;
        this.size ++ ;
    }
    insert(position,data){
        if(position > this.size || position < 0) return false;
        let current = this.nodeInstance;
        this.nodeInstance.next = this.head;
        for(let i = 0; i <position; i++){
            current = current.next;
        }
        const node = this.generateNode(data);
        const next = current.next
        current.next = node;
        node.next = next;
        this.head = this.nodeInstance.next;
        this.size ++;
        return true;
    }
    getData(position){
        if(position > this.size || position < 0) return undefined;
        let current = this.head;
        for(let i = 0; i <position; i++){
            current = current.next;
        }
        return current.val
    }
    indexOf(data){
        let idx = 0;
        let current = this.head;
        while(current) {
            if(current.val === data) return idx;
            idx++;
            current = current.next;
        }
        return -1
    }
    update(position,data){
        if(position > this.size || position < 0) return false;
        let current = this.head;
        for(let i = 0; i <position; i++){
            current = current.next;
        }
        current.val = data;
        return true;
    }
    remove(data){
        this.nodeInstance.next = this.head;
        let current = this.nodeInstance;
        let preNode = this.nodeInstance;
        while(current){
            if(current.val === data){
                preNode.next = current.next;
                current = preNode;
                this.size--;
            }
            preNode = current;
            current = current.next;
        }
        this.head = this.nodeInstance.next
    }
    removeAt(position){
        if(position > this.size || position < 0) return false;
        let current = this.nodeInstance;
        this.nodeInstance.next = this.head;
        for(let i = 0; i <position; i++){
            current = current.next;
        }
        current.next = current.next.next;
        this.head = this.nodeInstance.next;
        this.size --;
    }
    toString(){
        const list = [];
        let current = this.head;
        while(current){
            list.push(current.val);
            current= current.next;
        }
        return list.join('');
    }
    count(){
        return this.size;
    }
    isEmpty(){
        return this.size === 0;
    }
}

欢迎指教