@try.bivek/dsts
v1.0.1
Published
A lightweight TypeScript library providing essential data structures with full type support.
Readme
DSTS - Data Structures TypeScript
A lightweight TypeScript library providing essential data structures with full type support.
📦 Installation
npm i @try.bivek/dsts🚀 Quick Start
import { LinkedList, Stack, Queue, MinHeap } from '@try.bivek/dsts';
// Create a typed linked list
const list = new LinkedList<number>();
list.insertAtHead(42);
// Create a typed stack
const stack = new Stack<string>();
stack.push("Hello World");
// Create a typed queue
const queue = new Queue<boolean>();
queue.enqueue(true);
// Create a min heap
const heap = new MinHeap();
heap.insert(10);📚 Data Structures
LinkedList
A generic singly linked list implementation.
import { LinkedList } from '@try.bivek/dsts';
const list = new LinkedList<number>();
// Insert at head
list.insertAtHead(1);
list.insertAtHead(2); // List: 2 -> 1
// Insert at tail
list.insertAtTail(3); // List: 2 -> 1 -> 3
// Delete by value
list.deleteValue(1); // List: 2 -> 3
// Access head node
const head = list.head; // ListNode<number> | null
if (head) {
console.log(head.value); // 2
console.log(head.next?.value); // 3
}Methods:
insertAtHead(value: T): void- Insert element at the beginninginsertAtTail(value: T): void- Insert element at the enddeleteValue(value: T): void- Delete first occurrence of value
Properties:
head: ListNode<T> | null- Reference to the first node
Stack
A generic LIFO (Last In, First Out) stack implementation.
import { Stack } from '@try.bivek/dsts';
const stack = new Stack<string>();
// Push elements
stack.push("first");
stack.push("second");
stack.push("third");
// Peek at top element
console.log(stack.peek()); // "third"
// Pop elements
console.log(stack.pop()); // "third"
console.log(stack.pop()); // "second"
// Check if empty
console.log(stack.isEmpty()); // false
console.log(stack.pop()); // "first"
console.log(stack.isEmpty()); // trueMethods:
push(value: T): void- Add element to toppop(): T | undefined- Remove and return top elementpeek(): T | undefined- Return top element without removingisEmpty(): boolean- Check if stack is empty
Queue
A generic FIFO (First In, First Out) queue implementation.
import { Queue } from '@try.bivek/dsts';
const queue = new Queue<number>();
// Enqueue elements
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
// Peek at front element
console.log(queue.peek()); // 1
// Dequeue elements
console.log(queue.dequeue()); // 1
console.log(queue.dequeue()); // 2
// Check if empty
console.log(queue.isEmpty()); // false
console.log(queue.dequeue()); // 3
console.log(queue.isEmpty()); // trueMethods:
enqueue(value: T): void- Add element to reardequeue(): T | undefined- Remove and return front elementpeek(): T | undefined- Return front element without removingisEmpty(): boolean- Check if queue is empty
MinHeap
A min heap implementation for numbers (smallest element at root).
import { MinHeap } from '@try.bivek/dsts';
const heap = new MinHeap();
// Insert elements
heap.insert(10);
heap.insert(5);
heap.insert(15);
heap.insert(3);
// Access internal array (for debugging)
console.log(heap.heap); // [3, 5, 15, 10]
// Extract minimum
console.log(heap.extractMin()); // 3
console.log(heap.extractMin()); // 5
console.log(heap.extractMin()); // 10
console.log(heap.extractMin()); // 15
console.log(heap.extractMin()); // undefined (empty heap)Methods:
insert(val: number): void- Insert a number into the heapextractMin(): number | undefined- Remove and return the minimum element
Properties:
heap: number[]- Internal array representation of the heap
🎯 Usage Examples
Building a Task Priority System
import { MinHeap, Queue } from '@try.bivek/dsts';
interface Task {
id: string;
priority: number;
description: string;
}
class TaskManager {
private priorities = new MinHeap();
private taskQueue = new Queue<Task>();
addTask(task: Task) {
this.priorities.insert(task.priority);
this.taskQueue.enqueue(task);
}
getNextTaskPriority(): number | undefined {
return this.priorities.extractMin();
}
processNextTask(): Task | undefined {
return this.taskQueue.dequeue();
}
}Implementing Undo Functionality
import { Stack } from '@try.bivek/dsts';
interface Action {
type: string;
data: any;
timestamp: Date;
}
class UndoRedoManager {
private undoStack = new Stack<Action>();
private redoStack = new Stack<Action>();
executeAction(action: Action) {
this.undoStack.push(action);
// Clear redo stack when new action is performed
while (!this.redoStack.isEmpty()) {
this.redoStack.pop();
}
}
undo(): Action | undefined {
const action = this.undoStack.pop();
if (action) {
this.redoStack.push(action);
}
return action;
}
redo(): Action | undefined {
const action = this.redoStack.pop();
if (action) {
this.undoStack.push(action);
}
return action;
}
}🔧 TypeScript Support
This package is written in TypeScript and provides full type definitions. All generic data structures (LinkedList<T>, Stack<T>, Queue<T>) support any type:
// String stack
const stringStack = new Stack<string>();
// Object queue
interface User {
id: number;
name: string;
}
const userQueue = new Queue<User>();
// Custom type linked list
type Product = {
id: string;
price: number;
};
const productList = new LinkedList<Product>();📝 API Reference
ListNode
class ListNode<T> {
value: T;
next: ListNode<T> | null;
constructor(value: T);
}Time Complexities
| Data Structure | Operation | Time Complexity | |---------------|-----------|-----------------| | LinkedList | insertAtHead | O(1) | | LinkedList | insertAtTail | O(n) | | LinkedList | deleteValue | O(n) | | Stack | push | O(1) | | Stack | pop | O(1) | | Stack | peek | O(1) | | Queue | enqueue | O(1) | | Queue | dequeue | O(1) | | Queue | peek | O(1) | | MinHeap | insert | O(log n) | | MinHeap | extractMin | O(log n) |
🤝 Contributing
This package is open for contributions. Feel free to submit issues and pull requests.
📄 License
MIT License
🔗 Links
- NPM Package: @try.bivek/dsts
- Author: Bivek Gharti
Happy coding! 🚀
