Class Queue<E, R>

  1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
  2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
  3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
  4. Task Scheduling: Managing the order of task execution in operating systems or applications.
  5. Data Buffering: Acting as a buffer for data packets in network communication.
  6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
  7. Real-time Queuing: Like queuing systems in banks or supermarkets.

Type Parameters

  • E = any
  • R = any

Hierarchy

  • LinearBase<E, R>
    • Queue

Accessors

  • get first(): undefined | E
  • Time Complexity: O(1) Space Complexity: O(1)

    The first function returns the first element of the array _elements if it exists, otherwise it returns undefined.

    Returns undefined | E

    The get first() method returns the first element of the data structure, represented by the _elements array at the _offset index. If the data structure is empty (length is 0), it returns undefined.

  • get last(): undefined | E
  • Time Complexity: O(1) Space Complexity: O(1)

    The last function returns the last element in an array-like data structure, or undefined if the structure is empty.

    Returns undefined | E

    The method get last() returns the last element of the _elements array if the array is not empty. If the array is empty, it returns undefined.

Methods

  • The function _createInstance returns a new instance of the Queue class with the specified options.

    Parameters

    • Optionaloptions: QueueOptions<E, R>

      The options parameter in the _createInstance method is of type QueueOptions<E, R>, which is used to configure the behavior of the queue being created. It allows you to specify settings or properties that can influence how the queue operates.

    Returns this

    An instance of the Queue class with an empty array and the provided options is being returned.

  • Time Complexity: O(n) Space Complexity: O(n)

    The function _getIterator returns an iterable iterator for the elements in the class.

    Returns IterableIterator<E, any, any>

  • The function _getReverseIterator returns an iterator that iterates over elements in reverse order.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(n) Space Complexity: O(1)

    The function is an implementation of the Symbol.iterator method that returns an IterableIterator.

    Parameters

    • Rest...args: any[]

      The args parameter in the code snippet represents a rest parameter. It allows the function to accept any number of arguments as an array. In this case, the args parameter is used to pass any number of arguments to the _getIterator method.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(n) Space Complexity: O(1)

    The function addAt inserts a new element at a specified index in an array, returning true if successful and false if the index is out of bounds.

    Parameters

    • index: number

      The index parameter represents the position at which the newElement should be added in the array.

    • newElement: E

      The newElement parameter represents the element that you want to insert into the array at the specified index.

    Returns boolean

    The addAt method returns a boolean value - true if the new element was successfully added at the specified index, and false if the index is out of bounds (less than 0 or greater than the length of the array).

  • Time Complexity: O(1) Space Complexity: O(1)

    The at function returns the element at a specified index adjusted by an offset, or undefined if the index is out of bounds.

    Parameters

    • index: number

      The index parameter represents the position of the element you want to retrieve from the data structure.

    Returns undefined | E

    The at method is returning the element at the specified index adjusted by the offset _offset.

  • Time Complexity: O(1) Space Complexity: O(1)

    The clear function resets the elements array and offset to their initial values.

    Returns void

  • Time Complexity: O(n) Space Complexity: O(n)

    The clone() function returns a new Queue object with the same elements as the original Queue.

    Returns this

    The clone() method is returning a new instance of the Queue class.

  • Time Complexity: O(n) Space Complexity: O(1)

    The compact function in TypeScript slices the elements array based on the offset and resets the offset to zero.

    Returns boolean

    The compact() method is returning a boolean value of true.

  • Parameters

    • Rest...items: Queue<E, R>[]

      The concat method takes in an array of items, where each item can be either of type E or an instance of LinearBase<E, R>.

    Returns this

    The concat method is returning a new instance of the class that it belongs to, with the items passed as arguments concatenated to it.

  • Time Complexity: O(n) Space Complexity: O(1)

    The delete function removes an element from the list.

    Parameters

    • element: E

      Specify the element to be deleted

    Returns boolean

    A boolean value indicating whether the element was successfully deleted or not

  • Time Complexity: O(n) Space Complexity: O(1)

    The deleteAt function deletes the element at a given index.

    Parameters

    • index: number

      Determine the index of the element to be deleted

    Returns undefined | E

    A boolean value

  • Time Complexity: O(n) Space Complexity: O(1)

    The every function checks if every element in the array satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      The predicate parameter is a callback function that takes three arguments: the current element being processed, its index, and the array it belongs to. It should return a boolean value indicating whether the element satisfies a certain condition or not.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns boolean

    The every method is returning a boolean value. It returns true if every element in the array satisfies the provided predicate function, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(1)

    The fill function in TypeScript fills a specified range in an array-like object with a given value.

    Parameters

    • value: E

      The value parameter in the fill method represents the element that will be used to fill the specified range in the array.

    • Optionalstart: number = 0

      The start parameter specifies the index at which to start filling the array with the specified value. If not provided, it defaults to 0, indicating the beginning of the array.

    • end: number = ...

      The end parameter in the fill function represents the index at which the filling of values should stop. It specifies the end of the range within the array where the value should be filled.

    Returns this

    The fill method is returning the modified object (this) after filling the specified range with the provided value.

  • Time Complexity: O(n) Space Complexity: O(n)

    The filter function creates a new Queue object containing elements from the original Queue that satisfy a given predicate function.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      The predicate parameter is a callback function that takes three arguments: the current element being iterated over, the index of the current element, and the queue itself. It should return a boolean value indicating whether the element should be included in the filtered queue or not.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns Queue<E, R>

    The filter method is returning a new Queue object that contains the elements that satisfy the given predicate function.

  • Type Parameters

    • S

    Parameters

    • predicate: ElementCallback<E, R, S>
    • OptionalthisArg: any

    Returns undefined | S

  • Parameters

    • predicate: ElementCallback<E, R, unknown>
    • OptionalthisArg: any

    Returns undefined | E

  • Time Complexity: O(n) Space Complexity: O(1)

    The findIndex function iterates over an array and returns the index of the first element that satisfies the provided predicate function.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      The predicate parameter in the findIndex function is a callback function that takes three arguments: item, index, and the array this. It should return a boolean value indicating whether the current element satisfies the condition being checked for.

    • OptionalthisArg: any

      The thisArg parameter in the findIndex function is an optional parameter that specifies the value to use as this when executing the predicate function. If provided, the predicate function will be called with thisArg as its this value. If @returns ThefindIndex` method is returning the index of the first element in the array that satisfies the provided predicate function. If no such element is found, it returns -1.

    Returns number

  • Time Complexity: O(n) Space Complexity: O(1)

    The forEach function iterates over each element in an array-like object and calls a callback function for each element.

    Parameters

    • callbackfn: ElementCallback<E, R, void>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array that forEach was called upon.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If `thisArg

    Returns void

  • Time Complexity: O(n) Space Complexity: O(1)

    The function checks if a given element exists in a collection.

    Parameters

    • element: E

      The parameter "element" is of type E, which means it can be any type. It represents the element that we want to check for existence in the collection.

    Returns boolean

    a boolean value. It returns true if the element is found in the collection, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function indexOf searches for a specified element starting from a given index in an array-like object and returns the index of the first occurrence, or -1 if not found.

    Parameters

    • searchElement: E

      The searchElement parameter in the indexOf function represents the element that you want to find within the array. The function will search for this element starting from the fromIndex (if provided) up to the end of the array. If the searchElement is found within the

    • OptionalfromIndex: number = 0

      The fromIndex parameter in the indexOf function represents the index at which to start searching for the searchElement within the array. If provided, the search will begin at this index and continue to the end of the array. If fromIndex is not specified, the default

    Returns number

    The indexOf method is returning the index of the searchElement if it is found in the array starting from the fromIndex. If the searchElement is not found, it returns -1.

  • Time Complexity: O(1) Space Complexity: O(1)

    The function checks if a data structure is empty by comparing its length to zero.

    Returns boolean

    A boolean value indicating whether the length of the object is 0 or not.

  • Time Complexity: O(n) Space Complexity: O(1)

    The join function in TypeScript returns a string by joining the elements of an array with a specified separator.

    Parameters

    • Optionalseparator: string = ','

      The separator parameter is a string that specifies the character or characters that will be used to separate each element when joining them into a single string. By default, the separator is set to a comma (,), but you can provide a different separator if needed.

    Returns string

    The join method is being returned, which takes an optional separator parameter (defaulting to a comma) and returns a string created by joining all elements of the array after converting it to an array.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function lastIndexOf in TypeScript returns the index of the last occurrence of a specified element in an array.

    Parameters

    • searchElement: E

      The searchElement parameter is the element that you want to find the last index of within the array. The lastIndexOf method will search the array starting from the fromIndex (or the end of the array if not specified) and return the index of the last occurrence of the

    • fromIndex: number = ...

      The fromIndex parameter in the lastIndexOf method specifies the index at which to start searching for the searchElement in the array. By default, it starts searching from the last element of the array (this.length - 1). If a specific fromIndex is provided

    Returns number

    The last index of the searchElement in the array is being returned. If the searchElement is not found in the array, -1 is returned.

  • Time Complexity: O(n) Space Complexity: O(n)

    The map function in TypeScript creates a new Queue by applying a callback function to each element in the original Queue.

    Type Parameters

    • EM
    • RM

    Parameters

    • callback: ElementCallback<E, R, EM>

      The callback parameter is a function that will be applied to each element in the queue. It takes the current element, its index, and the queue itself as arguments, and returns a new element.

    • OptionaltoElementFn: ((rawElement: RM) => EM)

      The toElementFn parameter is an optional function that can be provided to convert a raw element of type RM to a new element of type EM. This function is used within the map method to transform each raw element before passing it to the callback function. If

        • (rawElement): EM
        • Parameters

          • rawElement: RM

          Returns EM

    • OptionalthisArg: any

      The thisArg parameter in the map function is used to specify the value of this when executing the callback function. It allows you to set the context (the value of this) within the callback function. If thisArg is provided, it will be

    Returns Queue<EM, RM>

    A new Queue object containing elements of type EM, which are the result of applying the callback function to each element in the original Queue object.

  • Time Complexity: O(n) Space Complexity: O(n)

    The print function logs the elements of an array to the console.

    Returns void

  • Time Complexity: O(1) Space Complexity: O(1)

    The push function adds an element to the end of the queue and returns true. Adds an element at the back of the queue.

    Parameters

    • element: E

      The element parameter represents the element that you want to add to the queue.

    Returns boolean

    Always returns true, indicating the element was successfully added.

  • Time Complexity: O(k) Space Complexity: O(k)

    The pushMany function iterates over elements and pushes them into an array after applying a transformation function if provided.

    Parameters

    • elements: Iterable<E, any, any> | Iterable<R, any, any>

      The elements parameter in the pushMany function is an iterable containing elements of type E or R.

    Returns boolean[]

    The pushMany function is returning an array of boolean values indicating whether each element was successfully pushed into the data structure.

  • Parameters

    • callbackfn: ReduceLinearCallback<E>

    Returns E

  • Parameters

    • callbackfn: ReduceLinearCallback<E>
    • initialValue: E

    Returns E

  • Type Parameters

    • U

    Parameters

    • callbackfn: ReduceLinearCallback<E, U>
    • initialValue: U

    Returns U

  • Time Complexity: O(n) Space Complexity: O(1)

    The reverse function in TypeScript reverses the elements of an array starting from a specified offset.

    Returns this

    The reverse() method is returning the modified object itself (this) after reversing the elements in the array and resetting the offset to 0.

  • Time Complexity: O(1) Space Complexity: O(1)

    The function setAt updates an element at a specified index in an array-like data structure.

    Parameters

    • index: number

      The index parameter is a number that represents the position in the array where the new element will be set.

    • newElement: E

      The newElement parameter represents the new value that you want to set at the specified index in the array.

    Returns boolean

    The setAt method returns a boolean value - true if the element was successfully set at the specified index, and false if the index is out of bounds (less than 0 or greater than the length of the array).

  • Time Complexity: O(1) Space Complexity: O(1)

    The shift function removes and returns the first element in the queue, and adjusts the internal data structure if necessary to optimize performance.

    Returns undefined | E

    The function shift() returns either the first element in the queue or undefined if the queue is empty.

  • Time Complexity: O(m) Space Complexity: O(m)

    The slice function in TypeScript creates a new instance by extracting a portion of elements from the original instance based on the specified start and end indices.

    Parameters

    • Optionalstart: number = 0

      The start parameter in the slice method represents the index at which to begin extracting elements from an array-like object. If no start parameter is provided, the default value is 0, meaning the extraction will start from the beginning of the array.

    • end: number = ...

      The end parameter in the slice method represents the index at which to end the slicing. By default, if no end parameter is provided, it will slice until the end of the array (i.e., this.length).

    Returns this

    The slice method is returning a new instance of the object with elements sliced from the specified start index (default is 0) to the specified end index (default is the length of the object).

  • Time Complexity: O(n) Space Complexity: O(1)

    The "some" function checks if at least one element in a collection satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      The predicate parameter is a callback function that takes three arguments: value, index, and array. It should return a boolean value indicating whether the current element satisfies the condition.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as the this value when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If `thisArg

    Returns boolean

    a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.

  • Time Complexity: O(n log n) Space Complexity: O(n)

    The sort function in TypeScript sorts the elements of a collection using a specified comparison function.

    Parameters

    • OptionalcompareFn: ((a: E, b: E) => number)

      The compareFn parameter is a function that defines the sort order. It takes two elements a and b as input and returns a number indicating their relative order. If the returned value is negative, a comes before b. If the returned value is positive, @returns Thesort` method is returning the instance of the object on which it is called (this), after sorting the elements based on the provided comparison function (compareFn).

        • (a, b): number
        • Parameters

          Returns number

    Returns this

  • Time Complexity: O(n) Space Complexity: O(n)

    The function overrides the splice method to remove and insert elements in a queue-like data structure.

    Parameters

    • start: number

      The start parameter in the splice method specifies the index at which to start changing the array. Items will be added or removed starting from this index.

    • OptionaldeleteCount: number = 0

      The deleteCount parameter in the splice method specifies the number of elements to remove from the array starting at the specified start index. If deleteCount is not provided, it defaults to 0, meaning no elements will be removed but new elements can still be inserted at

    • Rest...items: E[]

      The items parameter in the splice method represents the elements that will be added to the array at the specified start index. These elements will replace the existing elements starting from the start index for the deleteCount number of elements.

    Returns this

    The splice method is returning the removedQueue, which is an instance of the same class as the original object.

  • Time Complexity: O(n) Space Complexity: O(n)

    The toArray function converts a linked list into an array.

    Returns E[]

    The toArray() method is returning an array of type E[].

  • Time Complexity: O(n) Space Complexity: O(n)

    The function toReversedArray takes an array and returns a new array with its elements in reverse order.

    Returns E[]

    The toReversedArray() function returns an array of elements of type E in reverse order.

  • Time Complexity: O(n) Space Complexity: O(n)

    The print function logs the elements of an array to the console.

    Returns E[]

  • Time Complexity: O(n) Space Complexity: O(n)

    The function returns an iterator that yields all the values in the object.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(n) Space Complexity: O(n)

    The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.

    Type Parameters

    • E

    Parameters

    • elements: E[]

      The "elements" parameter is an array of elements of type E.

    Returns Queue<E, any>

    The method is returning a new instance of the Queue class, initialized with the elements from the input array.