Users Pricing

blog

Home Blogs Implementation of Data Structures in JavaScript – MindStick
Implementation of Data Structures in JavaScript

Implementation of Data Structures in JavaScript

Ravi Vishwakarma 860 18 Jun 2024 Updated 18 Jun 2024

Data structures and algorithms are fundamental concepts in computer science and programming. They help you manage and organize data efficiently, and algorithms provide the methods to manipulate these data structures. Here’s an overview of some commonly used data structures and algorithms in JavaScript:

Data Structures

  1. Arrays
  2. Linked Lists
  3. Stacks
  4. Queues
  5. Hash Tables (Maps)
  6. Trees
  7. Graphs

Array:

Arrays are the most basic data structures that store elements consecutively.

<script type="text/javascript">
let array = [1, 2, 3, 4, 5];
array.push(6); // Adds 6 to the end of the array
array.pop();   // Removes the last element (6)
console.log(array);
</script>

Linked Lists

A linked list is a linear data structure where each element is a separate object. Each element (node) contains data and a reference (link) to the next node in the sequence.

<script type="text/javascript">
class Node {
    constructor(data, next = null) {
        this.data = data;
        this.next = next;
    }
}

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

    insertFirst(data) {
        this.head = new Node(data, this.head);
    }

    insertLast(data) {
        let node = new Node(data);
        let current;

        if (!this.head) {
            this.head = node;
        } else {
            current = this.head;
            while (current.next) {
                current = current.next;
            }
            current.next = node;
        }
    }

    removeFirst() {
        if (this.head) {
            this.head = this.head.next;
        }
    }

    removeLast() {
        if (!this.head) {
            return;
        }

        if (!this.head.next) {
            this.head = null;
            return;
        }

        let previous = this.head;
        let current = this.head.next;
        while (current.next) {
            previous = current;
            current = current.next;
        }
        previous.next = null;
    }

    printList() {
        let current = this.head;
        while (current) {
            console.log(current.data);
            current = current.next;
        }
    }
}
const list = new LinkedList();
list.insertFirst(1);
list.insertFirst(2);
list.insertLast(3);
list.printList();

</script>

Stacks

A stack is a linear data structure that follows the Last In First Out (LIFO) principle.

How to implement stack in JavaScript

Queues

A queue is a linear data structure that follows the First In First Out (FIFO) principle.

How to implement Queue in JavaScript

Hash Tables (Maps)

Hash tables (hash maps) store key-value pairs and provide efficient lookup, insertion, and deletion operations.

let map = new Map();
map.set('name', 'John');
map.set('age', 30);

console.log(map.get('name')); // Output: John
console.log(map.has('age')); // Output: true
map.delete('age');
console.log(map.has('age')); // Output: false

Ravi Vishwakarma

Employee

Backend-focused software engineer with over 4 years of experience building scalable systems using C#, .NET Core, and SQL Server—strong expertise in API design, performance optimization, and high-throughput systems, and experience in designing production-grade applications.


Markdown for AI

A clean, structured version of this page for AI assistants and LLMs.

Open .md