JavaScript Essentials - Data Structures
Implementing data structures in JavaScript
Data structures are essential for organizing and managing data efficiently. This tutorial covers the implementation of common data structures in JavaScript, including arrays, linked lists, stacks, queues, sets, and maps.
Key Points:
- Understand and implement arrays, linked lists, stacks, queues, sets, and maps.
- Each data structure has unique properties and use cases.
- Choosing the right data structure can improve the performance and efficiency of your code.
Arrays
Arrays are ordered collections of elements that can be accessed by their index. They are useful for storing lists of data.
// Array example
let fruits = ['Apple', 'Banana', 'Cherry'];
console.log(fruits[1]); // Output: Banana
fruits.push('Date');
console.log(fruits); // Output: ['Apple', 'Banana', 'Cherry', 'Date']
Linked Lists
Linked lists are collections of nodes, where each node contains a value and a reference to the next node. They are useful for dynamic data structures where elements are frequently added or removed.
// Linked list node
class ListNode {
constructor(value) {
this.value = value;
this.next = null;
}
}
// Linked list
class LinkedList {
constructor() {
this.head = null;
}
append(value) {
const newNode = new ListNode(value);
if (!this.head) {
this.head = newNode;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
}
print() {
let current = this.head;
while (current) {
console.log(current.value);
current = current.next;
}
}
}
const list = new LinkedList();
list.append('A');
list.append('B');
list.append('C');
list.print(); // Output: A, B, C
Stacks
Stacks are collections of elements that follow the Last-In-First-Out (LIFO) principle. They are useful for problems involving reversal or backtracking.
// Stack
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.items.length === 0) {
return 'Underflow';
}
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
}
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.pop()); // Output: 30
console.log(stack.peek()); // Output: 20
Queues
Queues are collections of elements that follow the First-In-First-Out (FIFO) principle. They are useful for scheduling tasks or managing order of execution.
// Queue
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.items.length === 0) {
return 'Underflow';
}
return this.items.shift();
}
front() {
if (this.items.length === 0) {
return 'No elements in Queue';
}
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
}
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.dequeue()); // Output: 1
console.log(queue.front()); // Output: 2
Sets
Sets are collections of unique elements. They are useful for operations involving uniqueness, such as finding distinct values or eliminating duplicates.
// Set
const set = new Set();
set.add(1);
set.add(2);
set.add(3);
set.add(1); // Duplicate, won't be added
console.log(set); // Output: Set { 1, 2, 3 }
Maps
Maps are collections of key-value pairs. They are useful for storing and retrieving values associated with keys.
// Map
const 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
Summary
In this tutorial, you learned about implementing various data structures in JavaScript, including arrays, linked lists, stacks, queues, sets, and maps. Understanding and using the appropriate data structure for a given problem can significantly improve the efficiency and performance of your code.