@caelus-dts/linked-list
Overview
The LinkedList
class provides a robust implementation of a doubly linked list data structure in TypeScript. It allows dynamic resizing and offers a comprehensive set of methods for managing and manipulating the list's elements. This structure is particularly efficient for scenarios requiring frequent insertions and deletions.
Features
- Doubly Linked: Elements are linked in both directions, enabling efficient traversal in both forward and reverse order.
- Dynamic Resizing: The list grows and shrinks as needed, accommodating varying numbers of elements.
- Type-Safe: Built using TypeScript generics, ensuring type safety and preventing common errors.
- Comprehensive API: Provides a wide range of methods for adding, removing, searching, and accessing elements.
- Iterable: Supports JavaScript's iterable protocol, allowing easy traversal using loops and other iterable methods.
Installation
1) Using npm
npm install @caelus-dts/linked-list
2) Using yarn
yarn add @caelus-dts/linked-list
3) Using pnpm
pnpm add @caelus-dts/linked-list
Usage
import LinkedList from '@caelus-dts/linked-list';
const list = new LinkedList<number>(); // Create a new linked list of numbers
list.push(10, 20, 30); // Add elements to the end
list.unshift(5, 0); // Add elements to the beginning
console.log(list.first); // Output: 0
console.log(list.last); // Output: 30
list.remove(2); // Remove the element at index 2
console.log(list.toArray()); // Output: [0, 5, 20, 30]
list.clear(); // Clear the list
const customCompareList = new LinkedList<string>(null, (a, b) => a.toLowerCase() === b.toLowerCase()); //Case insensitive comparison
customCompareList.push("apple", "Banana", "ORANGE");
console.log(customCompareList.contains("Orange")); // Output: true (because of the custom compare function)
for (const value of list) {
console.log(value); // Iterate over the list
}
API Documentation
constructor(values?: Iterable<T> | null, compareFunc?: CompareFunc<T>)
Creates a new LinkedList
.
values
: Optional initial values to populate the list. Can be any iterable.compareFunc
: Optional custom comparison function for methods likecontains
,indexOf
, andcount
. Defaults to strict equality.
Gets the number of elements in the list.size: number
Checks if the list is empty.isEmpty: boolean
Gets the first node in the list.front: ListNode<T> | undefined
Gets the last node in the list.back: ListNode<T> | undefined
Gets the value of the first element.first: T | undefined
Gets the value of the last element.last: T | undefined
Adds elements to the end of the list.push(...elements: T[]): number
Adds elements to the beginning of the list.unshift(...elements: T[]): number
Removes and returns the first element.shift(): T | undefined
Removes and returns the last element.pop(): T | undefined
Converts the list to an array.toArray(): T[]
Removes all elements from the list.clear(): void
Inserts an element at the specified index.insert(index: number, value: T): number
Removes the element at the specified index.remove(index: number): boolean
Gets the element at the specified index.get(index: number): T | undefined
Finds the first index of a value.indexOf(value: T): number
Checks if the list contains a value.contains(value: T): boolean
Counts the occurrences of a valuecount(value: T): number
Contributing
Contributions are welcome! Please fork the repository and submit a pull request.