From 368d86abf4326920ab3c995c57191275ad166587 Mon Sep 17 00:00:00 2001 From: hmizz Date: Sat, 2 May 2020 23:48:56 +0100 Subject: [PATCH 1/2] adding an implementation of a queue using 2 stacks --- Data Structures/Queue/QueueUsing2Stacks.js | 49 ++++++++++++++++++++++ 1 file changed, 49 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 0000000000..90025cd414 --- /dev/null +++ b/Data Structures/Queue/QueueUsing2Stacks.js @@ -0,0 +1,49 @@ + // 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++; + } + } + } \ No newline at end of file From 930c17df86fe57edcc71d1286b8cb62b1a4c391e Mon Sep 17 00:00:00 2001 From: vinayak Date: Wed, 6 May 2020 11:06:53 +0530 Subject: [PATCH 2/2] Update QueueUsing2Stacks.js --- Data Structures/Queue/QueueUsing2Stacks.js | 113 ++++++++++++--------- 1 file changed, 64 insertions(+), 49 deletions(-) diff --git a/Data Structures/Queue/QueueUsing2Stacks.js b/Data Structures/Queue/QueueUsing2Stacks.js index 90025cd414..6218688ef0 100644 --- a/Data Structures/Queue/QueueUsing2Stacks.js +++ b/Data Structures/Queue/QueueUsing2Stacks.js @@ -1,49 +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++; - } - } - } \ No newline at end of file +// 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