DataStructsKit
The DataStructsKit
package provides a simple implementation of a collection of data structure. The package is designed to be lightweight and easy to use, and currently supports the following data structures:
Installation
npm install datastructskit
Usage
To use the DataStructsKit
package, simply import the data structure you want to use from the package:
import {
LinkedListNode,
LinkedList,
DoublyLinkedList,
DoublyLinkedListNode,
} from "datastructskit";
Linked lists & Doubly linked lists
You can create a new linked list(singly) or a doubly linked list by calling the LinkedList
or DoublyLinkedList
constructor:
const linkedList = new LinkedList("head");
const doublyLinkedList = new DoublyLinkedList("head");
This creates a new linked list with a single node, with the value "head"
.
The generated list has the following properties:
head
: A reference to the first node in the linked list.tail
: A reference to the last node in the linked list.length
: The number of nodes in the linked list.
And the following methods:
add
The add
method adds a new node to the end of the linked list.
linkedList.add("new tail");
insert
The insert
method inserts a new node at a specified position in the linked list.
linkedList.insert(1, "new node");
toArray
The toArray
method returns an array of all the values in the linked list.
const values = linkedList.toArray();
has
The has
method returns true
if the specified value is found in the linked list, false
otherwise.
const hasValue = linkedList.has("new tail");
delete
The delete
method deletes the node at the specified index
in the linked list.
linkedList.delete(1);
Datatypes
Note that the LinkedList
and the DoublyLinkedList
support any datatype, and not just strings or numbers. You can use any valid JavaScript datatype as the value for a node in the list.
license
The MIT license, Full license is here