From 4f0ed31fd2ea3beb34ec6b01ad951e38a0087653 Mon Sep 17 00:00:00 2001 From: hmizz <60817898+hmizz@users.noreply.github.com> Date: Wed, 6 May 2020 06:41:03 +0100 Subject: [PATCH] adding an implementation of a queue using 2 stacks (#137) * adding an implementation of a queue using 2 stacks * Update QueueUsing2Stacks.js Co-authored-by: vinayak --- Data Structures/Queue/QueueUsing2Stacks.js | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Data Structures/Queue/QueueUsing2Stacks.js diff --git a/Data Structures/Queue/QueueUsing2Stacks.js b/Data Structures/Queue/QueueUsing2Stacks.js new file mode 100644 index 000000000..6218688ef --- /dev/null +++ b/Data Structures/Queue/QueueUsing2Stacks.js @@ -0,0 +1,64 @@ +// implementation of Queue using 2 stacks +// contribution made by hamza chabchoub for a university project + +class Queue { + constructor () { + this.inputStack = [] + this.outputStack = [] + } + + // Push item into the inputstack + enqueue (item) { + this.inputStack.push(item) + } + + dequeue (item) { + // push all items to outputstack + this.outputStack = [] + if (this.inputStack.length > 0) { + while (this.inputStack.length > 0) { + this.outputStack.push(this.inputStack.pop()) + } + } + // display the top element of the outputstack + if (this.outputStack.length > 0) { + console.log(this.outputStack.pop()) + // repush all the items to the inputstack + this.inputStack = [] + while (this.outputStack.length > 0) { + this.inputStack.push(this.outputStack.pop()) + } + } + } + + // display elements of the inputstack + listIn () { + let i = 0 + while (i < this.inputStack.length) { + console.log(this.inputStack[i]) + i++ + } + } + + // display element of the outputstack + listOut () { + let i = 0 + while (i < this.outputStack.length) { + console.log(this.outputStack[i]) + i++ + } + } +} + +// testing + +const queue = new Queue() +queue.enqueue(1) +queue.enqueue(2) +queue.enqueue(8) +queue.enqueue(9) + +console.log(queue.dequeue()) +// ans = 1 +console.log(queue.dequeue()) +// ans = 2