linked list in node js code example

Example 1: Javascript singly linked list

/**
 * @constructor SLL
 *
 * @param head The head of the list.
 * @param klass The reference to the SLLNode class
 * @param size the size or length of the list.
 * @arguments <klass:SLLNode>
 * @description The SLL Factory
 *
 */
export function SLL(klass = Node) {
    this.head = new Node();
    this.klass = Node;
    this.size = 0;
}

/**
 * @function Add|Method
 *
 * @returns SLLNode
 * @param node The node you will append
 * @arguments <key:string>
 * @description A method that will insert a new node at the beginning of the
 * list.
 */
SLL.prototype.add = function insert(key) {
    const node = new this.klass(key);
    this.head = this.head.add(node);
    this.size++;
    return this;
}

/**
 * @function Find|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to find.
 * @arguments <key:string>
 * @description A method that will retireve a node associated with the
 * corrosponding key.
 */
SLL.prototype.find = function find(key) {
    return this.head && this.head.find(key);
}

/**
 * @function Insert|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to append a new node.
 * @param key1 The key of the node you wish to insert
 * @arguments <key:string>, <key1:string>
 * @description A method that will insert a new node into the middle of the
 * list
 */
SLL.prototype.insert = function insert(key, key1) {
    const node = new this.klass(key1);
    return this.head && this.head.insert(key, node);
}

/**
 * @function Del|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to delete.
 * @arguments <key:string>
 * @description A method that will delete a node associated with the
 * corrosponding key.
 */
SLL.prototype.del = function del(key) {
    return this.head.del(key);
}

/**
 * @returns SLLNode
 * @param key The key of the node you wish to create.
 * @arguments <key:null?string>
 * @description The SLLNode Interface
 *
 * @interface ISLLNode
 */
function ISSLNode(key=null) {
    this.key = key;
    this.next = null;
}

/**
 * @constructor SLLNode
 *
 * @param key The key of the node you wish to create.
 * @arguments <key:null?string>
 * @description The SLLNode Factory
 * 
 * @implements ISLLNode
 */
export function SLLNode(key=null) {
    ISSLNode.call(this, key=null);
}

/**
 * @function Add|Method
 *
 * @returns SLLNode
 * @param node The node you will append
 * @arguments <node:SLLNode>
 * @description A method that will insert a new node at the beginning of the
 * list.
 */
SLLNode.prototype.add = function add(node) {
    if(!this.key && !this.next) {
        this.key = node.key;
        this.next = node.next;
        return node;
    }
    node.next = this;
    return node;
}

/**
 * @function Find|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to find.
 * @arguments <key:string>
 * @description A method that will retireve a node associated with the 
 * corrosponding key.
 */
SLLNode.prototype.find = function find(key) {
    if (this.key === key) {
        this.next = null;
        return this;
    }
    if (this.key !== key) {
        if(!this.next) {
            return 0;
        }
        return this.next.find(key);
    }
}

/**
 * @function Insert|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to append a new node.
 * @param node The node you will append
 * @arguments <key:string>, <node:SLLNode>
 * @description A method that will insert a new node into the middle of the
 * list
 */
SLLNode.prototype.insert = function insert(key, node) {
    if (this.key === key) {
        let tmp = this.next;
        this.next = node;
        node.next = tmp;
        return node;
    }
    if (this.key !== key) {
        if (!this.next) {
            return 0;
        }
        return this.next.insert(key, node);
    }
}

/**
 * @function Del|Method
 *
 * @returns SLLNode
 * @param key The key of the node you wish to delete.
 * @param pre 
 * @arguments <key:string>, <pre:null?SLLNode>
 * @description A method that will delete a node associated with the
 * corrosponding key.
 */
SLLNode.prototype.del = function del(key, pre=null) {
    if (this.key === key) {
       let tmp = this.next;
       pre.next = tmp;
        return this;
    }
    if (this.key !== key) {
        if (!this.next) {
            return 0;
        }
        return this.next.del(key, this);
    }
}

Example 2: linked list javascript

const list = {
    head: {
        value: 6
        next: {
            value: 10                                             
            next: {
                value: 12
                next: {
                    value: 3
                    next: null	
                    }
                }
            }
        }
    }
};