You are currently viewing Building a Linked List in JavaScript: User Input and Display

Building a Linked List in JavaScript: User Input and Display

Introduction:
Linked lists are fundamental data structures that provide flexibility and efficient element management. Understanding how to create a linked list in JavaScript, take user input to populate it, and display the final outcome is essential for grasping foundational programming concepts. In this tutorial, we’ll guide you through building a linked list and putting it into practice


Node and Linked List: The Building Blocks
To begin, let’s define two core components: the Node and LinkedList classes. The Node class represents an element within the linked list, encapsulating data and a reference to the next node. The LinkedList class manages these nodes, allowing us to perform various operations.

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.head = null;
    }
}

Adding Elements to the Linked List
The LinkedList class’s append method lets us add elements to the list. We create a new node with the provided data and attach it to the end of the list. If the list is empty, the new node becomes the head.

class LinkedList {
    // ... (constructor and other methods)

    append(data) {
        const newNode = new Node(data);
        if (!this.head) {
            this.head = newNode;
            return;
        }
        let current = this.head;
        while (current.next) {
            current = current.next;
        }
        current.next = newNode;
    }
}

Displaying the Linked List
The display method of the LinkedList class allows us to showcase the list’s contents. We iterate through each node, printing its data. The linked list is concluded with “null,” indicating the end of the list.

class LinkedList {
    // ... (constructor and other methods)

    display() {
        let current = this.head;
        while (current) {
            process.stdout.write(`${current.data} -> `);
            current = current.next;
        }
        console.log('null');
    }
}

Taking User Input to Populate the List
To make our linked list practical, let’s take user input to populate it. By asking the user for the number of elements they want to add, followed by the actual elements, we dynamically construct the linked list.

// Create an instance of LinkedList
const linkedList = new LinkedList();

// Take input from the user to add elements to the linked list
const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Enter the number of elements: ', (n) => {
    n = parseInt(n);
    let count = 0;

    const addElement = () => {
        rl.question(`Enter element ${count + 1}: `, (data) => {
            linkedList.append(parseInt(data));
            count++;

            if (count < n) {
                addElement();
            } else {
                // Display the linked list
                console.log('Linked List:');
                linkedList.display();
                rl.close();
            }
        });
    };

    addElement();
});

Conclusion:
In this tutorial, we’ve ventured into creating a linked list in JavaScript using the Node and LinkedList classes. By taking user input and displaying the final result, we’ve laid a strong foundation for comprehending how data is organized within this dynamic data structure. A firm grasp of linked lists is pivotal for tackling advanced algorithms and data structures, making it an indispensable step toward mastering programming concepts.


Feel free to adapt this JavaScript-based blog-style explanation to your website and audience.

Leave a Reply