blog

Home / DeveloperSection / Blogs / Implementation of Data Structures in JavaScript

Implementation of Data Structures in JavaScript

Implementation of Data Structures in JavaScript

Ravi Vishwakarma 145 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

 


Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By