Package Exports
- datastructskit
- datastructskit/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (datastructskit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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,
Stack,
Queue,
} 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.
Stack
You can create a new stack by calling the Stack
constructor:
const stack = new Stack();
This creates a new stack with no elements.
The stack has the following properties:
items
: An array that holds the elements of the stack.length
: The number of elements in the stack.
And the following methods:
peek
The peek
method returns the element at the top of the stack without removing it.
const topElement = stack.peek();
isEmpty
The isEmpty
method returns true
if the stack is empty, false
otherwise.
const emptyStatus = stack.isEmpty();
push
The push
method adds a new element to the top of the stack.
stack.push(value);
It returns the updated Stack
instance, allowing you to chain other methods to perform additional operations on the stack. For example:
stack.push(value).push(anotherValue);
pop
The pop
method removes and returns the element at the top of the stack.
const removedElement = stack.pop();
clear
The clear
method removes all elements from the stack, making it empty.
stack.clear();
Data Types
Note that the Stack
class supports any data type. You can use any valid JavaScript data type as the value for an element in the stack.
Queue
To create a new queue, call the Queue
constructor:
const queue = new Queue();
This creates a new empty queue.
The queue has the following properties:
first
: A reference to the first node in the queue.last
: A reference to the last node in the queue.size
: The number of nodes in the queue.
And the following methods:
isEmpty
The isEmpty
method returns true
if the queue is empty, false
otherwise.
const emptyStatus = queue.isEmpty();
toArray
The toArray
method returns an array containing all the values in the queue, in the order they were added.
const items = queue.toArray();
peek
The peek
method returns the value of the first node in the queue without removing it.
const firstValue = queue.peek();
enqueue
The enqueue
method adds a new element to the end of the queue.
queue.enqueue(value);
Certainly! Here's an updated version of the documentation for the enqueue
method that includes information about chaining other methods:
enqueue
The enqueue
method adds a new element to the end of the queue.
queue.enqueue(value);
It returns the updated Queue
instance, allowing you to chain other methods to perform additional operations on the queue. For example:
queue.enqueue(value).enqueue(anotherValue);
dequeue
The dequeue
method removes and returns the value of the first node in the queue.
const removedValue = queue.dequeue();
clear
The clear
method removes all nodes from the queue, making it empty.
queue.clear();
Note that the enqueue
, dequeue
, and peek
methods operate on the first and last nodes of the queue using the LinkedListNode
class.
Data Types
The Queue
class supports any data type. You can use any valid JavaScript data type as the value for a node in the queue.
license
The MIT license, Full license is here