diff --git a/Data-Structures/Queue/Queue.js b/Data-Structures/Queue/Queue.js index aa514e977c..af7aa884a7 100644 --- a/Data-Structures/Queue/Queue.js +++ b/Data-Structures/Queue/Queue.js @@ -1,56 +1,114 @@ /* Queue * A Queue is a data structure that allows you to add an element to the end of -* a list and remove the item at the front. A queue follows a "First In First Out" -* system, where the first item to enter the queue is the first to be removed. This -* implementation uses an array to store the queue. +* a list and remove the item at the front. A queue follows a FIFO (First In First Out) +* system, where the first item to enter the queue is the first to be removed, +* All these operation complexities are O(1). +* This implementation following the linked list structure. */ -// Functions: enqueue, dequeue, peek, view, length, empty class Queue { - // constructor + #size + constructor () { - // This is the array representation of the queue - this.queue = [] + this.head = null + this.tail = null + this.#size = 0 + + return Object.seal(this) } - // methods - // Add a value to the end of the queue - enqueue (item) { - this.queue.push(item) + get length () { + return this.#size } - // Removes the value at the front of the queue + /** + * @description - Add a value to the end of the queue + * @param {*} data + * @returns {number} - The current size of queue + */ + enqueue (data) { + const node = { data, next: null } + + if (!this.head && !this.tail) { + this.head = node + this.tail = node + } else { + this.tail.next = node + this.tail = node + } + + return ++this.#size + } + + /** + * @description - Removes the value at the front of the queue + * @returns {*} - The first data of the queue + */ dequeue () { - if (this.empty()) { + if (this.isEmpty()) { throw new Error('Queue is Empty') } - return this.queue.shift() // remove the item at position 0 from the array and return it + const firstData = this.peekFirst() + + this.head = this.head.next + + if (!this.head) { + this.tail = null + } + + this.#size-- + + return firstData } - // Return the length of the queue - length () { - return this.queue.length + /** + * @description - Return the item at the front of the queue + * @returns {*} + */ + peekFirst () { + if (this.isEmpty()) { + throw new Error('Queue is Empty') + } + + return this.head.data } - // Return the item at the front of the queue - peek () { - if (this.empty()) { + /** + * @description - Return the item at the tail of the queue + * @returns {*} + */ + peekLast () { + if (this.isEmpty()) { throw new Error('Queue is Empty') } - return this.queue[0] + return this.tail.data } - // List all the items in the queue - view (output = value => console.log(value)) { - output(this.queue) + /** + * @description - Return the array of Queue + * @returns {Array<*>} + */ + toArray () { + const array = [] + let node = this.head + + while (node) { + array.push(node.data) + node = node.next + } + + return array } - // Return Is queue empty ? - empty () { - return this.queue.length === 0 + /** + * @description - Return is queue empty or not + * @returns {boolean} + */ + isEmpty () { + return this.length === 0 } } -export { Queue } +export default Queue diff --git a/Data-Structures/Queue/test/Queue.test.js b/Data-Structures/Queue/test/Queue.test.js index 734ac74aac..2a8b44ee7f 100644 --- a/Data-Structures/Queue/test/Queue.test.js +++ b/Data-Structures/Queue/test/Queue.test.js @@ -1,48 +1,46 @@ -import { Queue } from '../Queue' +import Queue from '../Queue' -describe('Queue', () => { - it('Check enqueue/dequeue', () => { - const queue = new Queue() - queue.enqueue(1) - queue.enqueue(2) - queue.enqueue(8) - queue.enqueue(9) +describe('Testing the Queue DS', () => { + const queue = new Queue() - expect(queue.dequeue()).toBe(1) - expect(queue.dequeue()).toBe(2) + it('Testing enqueue method', () => { + expect(queue.enqueue(1)).toBe(1) + expect(queue.enqueue(2)).toBe(2) + expect(queue.enqueue(8)).toBe(3) + expect(queue.enqueue(9)).toBe(4) }) - it('Check length', () => { - const queue = new Queue() - - queue.enqueue(1) - queue.enqueue(2) - queue.enqueue(8) - queue.enqueue(9) + it('Testing length after enqueue', () => { + expect(queue.length).toBe(4) + }) - expect(queue.length()).toBe(4) + it('Testing peekFirst & peekLast methods', () => { + expect(queue.peekFirst()).toBe(1) + expect(queue.peekLast()).toBe(9) }) - it('Check peek', () => { - const queue = new Queue() + it('Testing toArray method', () => { + expect(queue.toArray()).toEqual([1, 2, 8, 9]) + }) - queue.enqueue(1) - queue.enqueue(2) - queue.enqueue(8) - queue.enqueue(9) + it('Testing dequeue method', () => { + expect(queue.dequeue()).toBe(1) + expect(queue.dequeue()).toBe(2) + }) - expect(queue.peek()).toBe(1) + it('Testing length after dequeue', () => { + expect(queue.length).toBe(2) }) - it('Check empty', () => { + it('Testing isEmpty method', () => { const queue = new Queue() - expect(queue.empty()).toBeTruthy() + expect(queue.isEmpty()).toBeTruthy() queue.enqueue(1) queue.enqueue(2) queue.enqueue(8) queue.enqueue(9) - expect(queue.empty()).toBeFalsy() + expect(queue.isEmpty()).toBeFalsy() }) })