Documentation
Queue

Queue

A Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of “First in, First out” (FIFO), where the first element added to the queue is the first one to be removed. Queues are commonly used in various algorithms and applications for their simplicity and efficiency in managing data flow.

Ultify's queue is implemented using Linked List so both time complexity of enqueue and dequeue is O(1)O(1).

Operations

  • Enqueue (Insert): Adds an element to the rear of the queue.
  • Dequeue (Delete): Removes and returns the element from the front of the queue.
  • Peek: Returns the element at the front of the queue without removing it.
  • Clear: Empty the queue.
  • IsEmpty: Checks if the queue is empty.
  • toArray: Convert a queue to an array.

APIs

Constructor

import { Queue } from '@ultify/datastructure'

// Create an empty stack
const foo = new Queue()
console.log("Empty queue:", foo.toArray())

// Create a queue with pre-filled values
const bar = new Queue(1, 2, 3, 4)
console.log("Pre-filled queue:", bar.toArray())

fromArray (static)

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = Queue.fromArray([1, 2, 3])
console.log(foo.toArray())

toArray

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue(1, 2, 3)

console.log(foo.toArray())

Enqueue

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue()
foo.enqueue(1)
foo.enqueue(2)

console.log(foo.toArray())

Dequeue

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue(1, 2)
console.log(foo.dequeue())

console.log(foo.toArray())

Peak

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue(1, 2)

console.log("Peek:", foo.peek())

Clear

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue(1, 2, 3)

foo.clear()

console.log(foo.toArray())

isEmpty

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue(0, 1, 2, 3)
console.log(foo.size)

console.log(foo.isEmpty())

size (readonly)

import { Queue } from '@ultify/datastructure'

// Create an empty Queue
const foo = new Queue(0, 1, 2, 3)
console.log(foo.size)