Data Structures

Explore common frontend data structures with JavaScript examples covering the fundamentals and implementations of arrays, linked lists, stacks, queues, hash tables, trees, and graphs.

🎯 Introduction

Knowing common data structures and how to implement them in JavaScript helps you handle different data storage and manipulation needs. Simple examples introduce linear and nonlinear structures and strengthen your frontend fundamentals.


✨ Linear Structures

🧩 Array

An array is a simple linear structure that stores a sequence of values and provides quick access by index.

const arr = [10, 20, 30, 40, 50];
console.log(arr[2]); // 30

// Add an element
arr.push(60);
console.log(arr); // [10, 20, 30, 40, 50, 60]

// Remove elements
arr.splice(1, 2); // Remove 2 elements starting at index 1
console.log(arr); // [10, 40, 50, 60]

🔗 Linked List

A linked list consists of nodes. Each node contains data in value and a reference to the next node in next. Here, plain objects and variables represent the list.

const node3 = { value: 3, next: null };
const node2 = { value: 2, next: node3 };
const node1 = { value: 1, next: node2 };

console.log(node1);
// Output: { value: 1, next: { value: 2, next: { value: 3, next: null } } }

📚 Stack

A stack follows last in, first out (LIFO), with operations performed only at the top. An array can represent it.

const stack = [];

// Push onto the stack
stack.push(1);
stack.push(2);
stack.push(3);

console.log(stack); // [1, 2, 3]

// Pop from the stack
const top = stack.pop();
console.log(top); // 3
console.log(stack); // [1, 2]

🚶 Queue

A queue follows first in, first out (FIFO): insert at the back and remove from the front. An array can represent this structure too.

const queue = [];

// Enqueue
queue.push(1);
queue.push(2);
queue.push(3);

console.log(queue); // [1, 2, 3]

// Dequeue
const front = queue.shift();
console.log(front); // 1
console.log(queue); // [2, 3]

🌳 Nonlinear Structures

🗝️ Hash Table

A hash table provides fast access to values by key. Here, an ES6 Map represents this behavior.

const hashTable = new Map();

// Insert
hashTable.set('apple', 'red');
hashTable.set('banana', 'yellow');

// Access
console.log(hashTable.get('apple')); // red

// Delete
hashTable.delete('banana');

// Check whether a key exists
console.log(hashTable.has('banana')); // false

🌲 Tree

A tree consists of nodes, each of which can have multiple children. Objects and arrays represent the nodes here.

const root = {
    value: 'Root',
    children: [],
};

const child1 = { value: 'Child 1', children: [] };
const child2 = { value: 'Child 2', children: [] };

root.children.push(child1);
root.children.push(child2);

console.log(root);

❓ Questions and Answers

Q1: How do arrays and linked lists differ?

In the conventional array model, elements are stored contiguously, making access fast but insertion and deletion slower. Linked list nodes are stored separately, making insertion and deletion convenient but access slower.

Q2: How do stacks and queues differ?

A stack is last in, first out; a queue is first in, first out.


🎉 You have learned the fundamentals of arrays, linked lists, stacks, queues, hash tables, trees, and graphs, along with JavaScript implementation skills! Keep practicing to strengthen your frontend skills!