JS Linked Queue
A queue data structure implemented as a dubly linked list with Typesctipt with support for generic types.
Changelog
- 0.2.1
- first method now return type
T
insteadNode<T>
- first method now return type
Installation
Install the package using a javascript package manager:
- Yarn
> yarn add js-linked-queue
- Npm
> npm install --save js-linked-queue
Usage
Using js-linked-queue is simple and can be used either with Javascript or Typescript.
Here an example with typescript
; // Because the queue supports generics you can // put anything as a value for a queue node // Create the queue ; // To add people to queue use the enqueue function peopleQueue.enqueuenew Person"Mark", 23; peopleQueue.enqueuenew Person"Lucas", 43; // To delete an item from the head of the queue use dequeue peopleQueue.dequeue; // reduce the queue size by one element // Use first() to get the head of the queue peopleQueue.first; // Person { name: Lucas, age: 43} // You can even populate a queue from an existing object array ; ; // With a for-of loop you can iterate through the queue's elements: for of family
Simple usage for javascript version of the example.
Api
Queue's apis.
- #####
Queue<T>.constructor(input: T[] = [])
The default queue constructor accept an array of generic typeT
that by default is set as an empty array - ####
instance.enqueue(item:T): boolean
The enqueue method take an item of typeT
and return aboolean
: true for correct enqueuing, false otherwise - ####
instance.dequeue(): T
The dequeue method is used to remove an element from the head of the queue and returns the valueT
of theNode<T>
deleted from the queue - ####
instance.first(): T
The first method return the value of typeT
inside the head node of the queue - ####
instance.isEmpty(): boolean
The isEmpty method return aboolean
; true if the queue is empty otherwise false - ####
instance.size(): number
The size method return the queue size as anumber
- ####
instance.clear(): void
The clear method is used to completely delete the queue's elements - ####
[Symbol.iterator]
Used for iterating though the queue's elements