Below is the source code for the Linked List Data Structure written entirely in JavaScript!


/*******************************************************
 *  LinkedList.js
 *  Created by Stephen Hall on 10/12/18.
 *  Copyright (c) 2018 Stephen Hall. All rights reserved.
 *  A singly linked list implementation in JavaScript
 ********************************************************/

/**
 * Node Object declaration
 **/
class Node {
    /**
     * Node Constructor
     * Creates a new node
     * @param {*} data
     *
     * @return none
     * @throws none
     **/
    constructor(data){
        this.data = data;
        this.next = null;
    }
}

/**
 * List Object declaration
 */
class List {
    /**
     * Linked List Constructor
     * Creates a new list
     *
     * @return none
     * @throws none
     **/
    constructor(){
        this.length = 0;
        this.head = null;
        this.tail = null;
    }

    /**
     * Add method
     * Adds a new node to the list
     * @param {*} data
     * @return {Node} created
     * @throws none
     **/
    add(data) {
        let node = new Node(data);

        if (isEmpty()) {
            // If there is an empty list
            this.head = this.tail = node;
            this.length++;
            return node;
        }
        else {
            // if List is not empty
            this.tail.next = node;
            this.tail = node;
            this.length++;
            return node;
        }
    }

    /**
     * Add method
     * Adds a new node to the list
     * @param {int} index to be added at
     * @param {*} data to be added to the list
     * @return {Node} created
     * @throws {RangeError}
     **/
    addAt(index, data) {
        if(isNaN(index) || index < 0 || index > this.length)
            throw new RangeError("Invalid Index");

        let node = new Node(data);
        let tmp = this.head;

        for (let i = 0; tmo && i < (index - 1); i++) {
            tmp = tmp.next;
        }

        if (isEmpty()) {
            // If there is an empty list
            this.head = this.tail = node;
            this.length++;
            return node;
        }
        else {
            // Index is in the list
            node.next = tmp.next;
            if(tmp.next == null)
                this.tail = node;
            tmp.next = node;
            this.length++;
            return node;
        }
    }

    /**
     * AddToHead method
     * Adds a new node to the beginning of the list
     * @param {*} data
     * @return {Node} created
     * @throws none
     **/
    addToHead(data) {
        return this.addAt(0, data);
    }

    /**
     * SearchNodeAt method
     * Search the list for the node at the given index
     *
     * @param {int} index to find in the list
     *
     * @return {Node} at index or null
     * @throws none
     **/
    searchNodeAt(index) {
        if(isNaN(index) || index < 0 || index >= this.length)
            return null;

        let tmp = this.head;
        let count = 0;

        while(tmp != null){
            if(count === index)
                return tmp;
            tmp = tmp.next;
            count++;
        }

        return null;
    }

    /**
     * searchListFor method
     * Searches the list for the first node containg
     *   the given data
     *
     * @param {*} data
     *
     * @return {int} index of matching node or -1
     * @throws none
     **/
    searchListFor(data) {
        let tmp = this.head;
        let index = 0;

        while(tmp != null){
            if(tmp.data === data)
                return index;
            tmp = tmp.next;
            index++;
        }
        return -1;
    }

    /**
     * RemoveByIndex method
     * Removes the node from the list at the given index
     *
     * @param {int} index to be removed
     *
     * @return {boolean} success, fail
     * @throws none
     **/
    removeByIndex(index) {
        if(isNaN(index) || index < 0 || index >= this.length)
            return false;

        if(!(index >= this.length)){
            let tmp = this.head;
            let count = 0;

            if(index === 0) {
                tmp = this.head.next;
                this.head = null;
                this.head = tmp;
                this.length--;
                return true;
            }
            else {
                let tmp2 = tmp.next;

                while(tmp2 != null){
                    if(count === index - 1){
                        tmp.next = tmp2.next;
                        tmp2 = null;
                        this.length--;
                        return true;
                    }

                    tmp = tmp.next;
                    tmp2 = tmp2.next;
                    count++;
                }
            }
        }
        return false;
    }

    /**
     * RemoveHead method
     * Removes the first Node in the list
     *
     * @return {boolean} success, fail
     * @throws none
     **/
    removeHead() {
        return this.removeByIndex(0);
    }

    /**
     * RemoveTail method
     * Removes the last Node in the list
     *
     * @return {boolean} success, fail
     * @throws none
     **/
    removeTail(index) {
        return this.removeByIndex(length);
    }

    /**
     * RemoveByData method
     * Removes the first Node from the list containing the given data
     *
     * @param  {*} data to be removed
     *
     * @return {boolean} success, fail
     * @throws none
     **/
    removeByData(data) {
        return this.removeByIndex(this.searchListFor(data));
    }

    /**
     * Clear method
     * Clears all the data from the list
     *
     * @return {boolean} success, fail
     * @throws none
     **/
    clear(){
        let tmp = this.head;
        while(tmp){
            tmp = tmp.next;
            this.head = null;
            this.head = tmp;
        }
        this.head = this.tail = null;
        this.length = 0;
    }

    /**
     * Print method
     * Prints the list to the console
     *
     * @return none
     * @throws none
     **/
    print(){
        let tmp = this.head;

        while(tmp != null) {
            console.log(tmp.data);
            tmp = tmp.next;
        }
    }

    /**
     * IsEmpty method
     * Determines if the list is empty
     *
     * @returns {boolean}
     * @throws none
     **/
    isEmpty(){
        return (this.size()) === 0;
    }

    /**
     * Size method
     * Gets the size of the list
     *
     * @returns {int} Number of Nodes in the list
     * @throws none
     **/
    size(){
        return this.length;
    }
}

Want to learn more Data Structures and algrothimns in JavaScript?

1 Comment

Aly Chiman · January 4, 2019 at 2:40 pm

Hello there,

My name is Aly and I would like to know if you would have any interest to have your website here at urlbytes.com promoted as a resource on our blog alychidesign.com ?

We are in the midst of updating our broken link resources to include current and up to date resources for our readers. Our resource links are manually approved allowing us to mark a link as a do-follow link as well
.
If you may be interested please in being included as a resource on our blog, please let me know.

Thanks,
Aly

Leave a Reply

Your email address will not be published. Required fields are marked *