0.2.1 • Published 5 years ago
js-linked-queue v0.2.1
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
import { Queue } from "js-linked-queue";
// Because the queue supports generics you can
// put anything as a value for a queue node
class Person {
public name;
public age;
constructor(name, age) {
this.name = name;
this.age = age;
}
}
// Create the queue
const peopleQueue = new Queue<Person>();
// To add people to queue use the enqueue function
peopleQueue.enqueue(new Person("Mark", 23));
peopleQueue.enqueue(new 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
const people: Person[] = [
new Person("Alex", 23),
new Person("Mary", 52),
new Person("Alice", 19),
new Person("Rita", 79),
new Person("Frank", 57)
];
const family = new Queue<Person>(people);
// With a for-of loop you can iterate through the queue's elements:
for(const relative of family) {
console.log(`Hello, ${relative.name}`);
}
Simple usage for javascript version of the example.
Api
Queue's apis.
The default queue constructor accept an array of generic typeQueue<T>.constructor(input: T[] = [])
T
that by default is set as an empty array
The enqueue method take an item of typeinstance.enqueue(item:T): boolean
T
and return aboolean
: true for correct enqueuing, false otherwise
The dequeue method is used to remove an element from the head of the queue and returns the valueinstance.dequeue(): T
T
of theNode<T>
deleted from the queue
The first method return the value of typeinstance.first(): T
T
inside the head node of the queue
The isEmpty method return ainstance.isEmpty(): boolean
boolean
; true if the queue is empty otherwise false
The size method return the queue size as ainstance.size(): number
number
The clear method is used to completely delete the queue's elementsinstance.clear(): void
Used for iterating though the queue's elements[Symbol.iterator]