A double-ended queue (deque) implementation that efficiently supports insertion and removal of elements at both ends.
import { Deque } from 'jsr:@mskr/data-structures';
const deque = new Deque<number>();
size: number
- Number of elements in the dequeisEmpty(): boolean
- Whether the deque is empty
// Add to front - O(1)
deque.addFirst(1);
// Add to back - O(1)
deque.addLast(2);
// Remove from front - O(1)
const first = deque.removeFirst();
// Remove from back - O(1)
const last = deque.removeLast();
// Peek at front element - O(1)
const first = deque.peekFirst();
// Peek at back element - O(1)
const last = deque.peekLast();
// Check if element exists - O(n)
const exists = deque.contains(1);
// Forward iteration (front to back)
for (const value of deque) {
console.log(value);
}
// Reverse iteration (back to front)
for (const value of deque.reverseIterator()) {
console.log(value);
}
// Remove all elements - O(1)
deque.clear();
// Convert to array - O(n)
const array = deque.toArray();
const deque = new Deque<number>();
// Add elements at both ends
deque.addFirst(1); // [1]
deque.addLast(2); // [1, 2]
deque.addFirst(0); // [0, 1, 2]
console.log([...deque]); // [0, 1, 2]
const queue = new Deque<string>();
// Enqueue
queue.addLast('first');
queue.addLast('second');
// Dequeue
const first = queue.removeFirst(); // "first"
const second = queue.removeFirst(); // "second"
const stack = new Deque<number>();
// Push
stack.addFirst(1);
stack.addFirst(2);
// Pop
const top = stack.removeFirst(); // 2
try {
const empty = new Deque<number>();
empty.removeFirst(); // Throws EmptyStructureError
} catch (error) {
if (error instanceof EmptyStructureError) {
console.log('Deque is empty!');
}
}
Key advantages of Deque:
- O(1) operations at both ends
- Can be used as both a queue and a stack
- Bidirectional iteration support
- Memory-efficient implementation