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.
- 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.
- Using
npm
npm install @caelus-dts/linked-list
- Using
yarn
yarn add @caelus-dts/linked-list
- Using
pnpm
pnpm add @caelus-dts/linked-list
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
}
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.
Checks if the list is empty.
Gets the first node in the list.
Gets the last node in the list.
Gets the value of the first element.
Gets the value of the last element.
Adds elements to the end of the list.
Adds elements to the beginning of the list.
Removes and returns the first element.
Removes and returns the last element.
Converts the list to an array.
Removes all elements from the list.
Inserts an element at the specified index.
Removes the element at the specified index.
Gets the element at the specified index.
Finds the first index of a value.
Checks if the list contains a value.
Counts the occurrences of a value
Contributions are welcome! Please fork the repository and submit a pull request.