From a8c24e908c65cea78bdab10ece0045b3c3501482 Mon Sep 17 00:00:00 2001 From: Samson Alajede Date: Thu, 17 Oct 2024 17:30:52 +0100 Subject: [PATCH] Added reverse queue implementation in javascript --- .../src/queue/reverse_queue/reverse_queue.js | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 code/data_structures/src/queue/reverse_queue/reverse_queue.js diff --git a/code/data_structures/src/queue/reverse_queue/reverse_queue.js b/code/data_structures/src/queue/reverse_queue/reverse_queue.js new file mode 100644 index 0000000000..776c2fc222 --- /dev/null +++ b/code/data_structures/src/queue/reverse_queue/reverse_queue.js @@ -0,0 +1,121 @@ +/** + * Part of Cosmos by OpenGenus Foundation + */ + +function Queue() { + this.queue = []; + this.stack = []; + this.frontEl = null; + this.total = 0; + + this.enqueue = function (el) { + if (this.total == 0) { + this.frontEl = el; + } + + this.queue.push(el); + this.total++; + }; + + this.dequeue = function () { + // Confirm if stack is empty and return + if (this.total == 0) { + console.log("The queue is empty!"); + + return; + } + + // Move all queue entries to stack if stack is empty + if (this.stack.length == 0) { + while (this.queue.length > 0) { + this.stack.push(this.queue.pop()); + } + } + + this.total--; + // Return the topmost element + return this.stack.pop(); + }; + + this.size = function () { + return this.total; + }; + + this.front = function () { + if (this.stack.length > 0) { + return this.stack[this.stack.length - 1]; + } + + return this.frontEl; + }; + + this.reverse = function () { + if (this.total == 0) { + console.log("The queue is empty"); + + return; + } + + this.stack.reverse(); + this.queue.reverse(); + while (this.queue.length > 0) { + this.stack.push(this.queue.pop()); + } + + this.frontEl = this.stack[this.stack.length - 1]; + }; + + this.print = function () { + if (this.total == 0) { + console.log(this.queue); + return; + } + + let output = []; + for (let i = this.stack.length - 1; i >= 0; --i) { + output.push(this.stack[i]); + } + for (let q of this.queue) { + output.push(q); + } + + console.log(output); + }; +} + +const queue = new Queue(); +const items = [1, 2, 3, 4]; +for (let i of items) { + queue.enqueue(i); +} +console.log("\nCurrent queue"); +console.log("==============="); +queue.print(); + +console.log("\nCurrent queue front"); +console.log("==============="); +console.log(queue.front()); + +const el = queue.dequeue(); +console.log("\nRemoved element:"); +console.log("==============="); +console.log(el); + +console.log("\nCurrent queue"); +console.log("==============="); +queue.print(); + +queue.enqueue(5); +queue.enqueue(6); +console.log("\nUpdated queue"); +console.log("==============="); +queue.print(); + +console.log("\nReversed queue"); +console.log("==============="); +queue.reverse(); +queue.print(); + +console.log("\nReversed queue front"); +console.log("==============="); +console.log(queue.front());