UNPKG

@monocircuit/utils

Version:

A repository that contains data structures, algorithms and more.

104 lines 3.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var LinkedList_1 = require("../LinkedList/LinkedList"); /** * A linear data structure that is build in a fashion that mostly exposes a * BigO notation of `O(1)` * * It follows the principle of FIFO (First In First Out). * * @spacecomplexity `O(n)` */ var Queue = /** @class */ (function () { /** * @param options Options that affect the initialization process of the `Queue`. */ function Queue(options) { this.__linkedList = new LinkedList_1.default(options); } Object.defineProperty(Queue.prototype, "length", { /** * Returns the amout of elements found in the `Queue`. * * @returns The length of the `Queue` */ get: function () { return this.__linkedList.length; }, enumerable: false, configurable: true }); /** * Checks wether or not the `Queue` does not contain any elements. This * works based on the `LinkedList` implementation. * * @returns A boolean that indicates wether or not the `Queue` is empty */ Queue.prototype.isEmpty = function () { return this.__linkedList.isEmpty(); }; /** * Fetches the element that currently sits on the end of the `Queue`. * * @returns The data that the top most element holds */ Queue.prototype.peek = function () { if (this.isEmpty()) { return null; } return this.__linkedList.head.data; }; /** * Adds a new element with some data to the end of the `Queue`. * * @param data A piece of data that will be held by the element in the `Queue` * @returns The `Queue` instance */ Queue.prototype.enqueue = function (data) { this.__linkedList.append(data); return this; }; /** * Deletes the element at the front of the queue. Which is the head of the * internal `LinkedList`. * * @returns The data that the deleted element held, or nothing if the * `Queue` was empty. */ Queue.prototype.dequeue = function () { var removedHead = this.__linkedList.deleteHead(); return removedHead ? removedHead.data : null; }; /** * Converts the `Queue` to an array. The indices are converted as to be * expected. * * @returns An array containing the elements of the `Queue` */ Queue.prototype.toArray = function () { return this.__linkedList.toArray(false).map(function (node) { return node.data; }); }; /** * Appends an array to the `Queue` instance. This is done using the * `fromArray()` method of the `LinkedList`. * * @param data An array of data that will be enqueued * @returns The `Queue` instance */ Queue.prototype.fromArray = function (data) { this.__linkedList.fromArray(data); return this; }; /** * Converts the `Queue` to string. * * @param callback A function that will handle the stringification * @returns A string containg the stringified data of the queue's elements. */ Queue.prototype.toString = function (callback) { return this.__linkedList.toString(callback); }; return Queue; }()); exports.default = Queue; //# sourceMappingURL=Queue.js.map