A comprehensive collection of data structures implemented in JavaScript with full documentation, testing, and modern ES6+ features.
- ✅ Singly Linked List - Linear data structure with nodes containing data and next pointer
- ✅ Doubly Linked List - Linear data structure with nodes containing data, next, and previous pointers
- ✅ Stack - LIFO (Last In, First Out) data structure
- ✅ Queue - FIFO (First In, First Out) data structure
- ✅ Hash Table - Key-value storage with collision resolution
- ✅ Priority Queue - Queue where elements are served based on priority
- ✅ Binary Tree - Hierarchical data structure with at most two children per node
- ✅ Graph - Network of nodes connected by edges (directed/undirected)
npm install
// ES6 Modules
import { SimpleLinkedList } from './SimpleLinkedList.js';
import { DoubleLinkedList } from './DoubleLinkedList.js';
import { Stack } from './Stack.js';
import { Queue } from './Queue.js';
import { HashTable } from './HashTable.js';
import { PriorityQueue } from './PriorityQueue.js';
import { BinaryTree } from './tree/BinaryTree.js';
import { Graph } from './graph/Graph.js';
import { SimpleLinkedList } from './SimpleLinkedList.js';
const list = new SimpleLinkedList();
list.add(1);
list.add(2);
list.add(3);
console.log(list.toString()); // [1, 2, 3]
// Iterate over the list
for (const value of list) {
console.log(value);
}
// Convert to array
const array = list.toArray(); // [1, 2, 3]
import { Stack } from './Stack.js';
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.peek()); // 3
console.log(stack.pop()); // 3
console.log(stack.toString()); // Stack(2, 1)
import { Queue } from './Queue.js';
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.front()); // 1
console.log(queue.dequeue()); // 1
console.log(queue.toString()); // Queue(2, 3)
import { HashTable } from './HashTable.js';
const hashTable = new HashTable();
hashTable.set('name', 'John');
hashTable.set('age', 30);
console.log(hashTable.get('name')); // John
console.log(hashTable.has('age')); // true
console.log(hashTable.toString()); // HashTable(name: John, age: 30)
import { PriorityQueue } from './PriorityQueue.js';
const pq = new PriorityQueue();
pq.enqueue('low priority', 1);
pq.enqueue('high priority', 3);
pq.enqueue('medium priority', 2);
console.log(pq.peek()); // high priority
console.log(pq.dequeue()); // high priority
import { BinaryTree } from './tree/BinaryTree.js';
const tree = new BinaryTree(1);
tree.insert(2, null, 'left');
tree.insert(3, null, 'right');
console.log(tree.inorderTraversal()); // [2, 1, 3]
console.log(tree.getHeight()); // 1
import { Graph } from './graph/Graph.js';
const graph = new Graph();
graph.addNode('A', 'B', 'C');
graph.addEdge('A', 'B', 5);
graph.addEdge('B', 'C', 3);
console.log(graph.breadthFirstSearch('A')); // ['A', 'B', 'C']
console.log(graph.dijkstra('A', 'C')); // Shortest path
Run the test suite:
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
All data structures implement these common methods:
isEmpty()
- Returns true if the structure is emptygetSize()
/size()
- Returns the number of elementsclear()
- Removes all elementstoArray()
- Converts to array representationtoString()
- Returns string representation[Symbol.iterator]()
- Enables iteration withfor...of
loops
Each method includes JSDoc comments with Big O time complexity:
- O(1) - Constant time operations
- O(log n) - Logarithmic time operations
- O(n) - Linear time operations
- O(n log n) - Linearithmic time operations
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
ISC License - see LICENSE file for details.