Package Exports
- js-linked-queue
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 (js-linked-queue) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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
TinsteadNode<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-queueUsage
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[] = [])Tthat by default is set as an empty array
The enqueue method take an item of typeinstance.enqueue(item:T): booleanTand 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(): TTof theNode<T>deleted from the queue
The first method return the value of typeinstance.first(): TTinside the head node of the queue
The isEmpty method return ainstance.isEmpty(): booleanboolean; true if the queue is empty otherwise false
The size method return the queue size as ainstance.size(): numbernumber
The clear method is used to completely delete the queue's elementsinstance.clear(): void
Used for iterating though the queue's elements[Symbol.iterator]