Queue
Initialize the queue
You can create a queue with the given size. Size is optional argument with the default value 65535.
import { Queue } from "quark-dsa";
const queue = new Queue(200); // Creates queue with size 200.
Supported methods
Method | Description | Returns |
---|---|---|
enqueue(item) | Inserts an item at the rear of the queue. | void |
dequeue() | Removes an item from the front of the queue and returns it. | dequeued item |
getFront() | Returns the front item of the queue. | front item of the queue |
getRear() | Returns the rear item of the queue. | rear item of the queue |
isEmpty() | Checks if the queue is empty | true if queue is empty otherwise false |
isFull() | Checks if the queue is full. It will check if length of the queue is equal to its size. | true if queue is full otherwise false |
length() | Gets the length of the queue. It is difference between rear and front of the queue. | length of the queue |
search(item) | Returns the 0-based position of the item in the queue. | returns the position of the item in the queue. If item is absent, it will return -1 |
Usage
import { Queue } from "quark-dsa";
const queue = new Queue(); // new queue is created.
queue.enqueue(3); // inserts 3 to the rear of the queue.
queue.enqueue(8); // inserts 8 to the rear of the queue.
queue.length(); // returns 2;
queue.search(8); // returns 1;
queue.search(98); // returns -1;
queue.dequeue(); // removes the front item of the queue and then returns it.
queue.getFront(); // returns the front item of the queue, 8.
queue.enqueue(10); // inserts 10 to the rear of the queue.
queue.getRear(); // returns the rear item of the queue, 10.
queue.dequeue(); // removes the front item of the queue and then returns it.
queue.dequeue(); // removes the front item of the queue and then returns it.
queue.length(); // returns 0;
queue.isEmpty(); // returns true as queue is empty now.