diff --git a/README.md b/README.md index bad182f9..013bb350 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,19 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct ### Data Structures - [Singly Linked List](src/_DataStructures_/LinkedList) + - [N Element From Last](src/_DataStructures_/LinkedList/element-from-last) - [Middle Node](src/_DataStructures_/LinkedList/middle-node) - [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list) - [Reverse Linked List](src/_DataStructures_/LinkedList/reverse-linked-list) + - [Stack](src/_DataStructures_/Stack) + - [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack) - [Baseball Game](src/_DataStructures_/Stack/baseball-game) - [Minimum Stack](src/_DataStructures_/Stack/min-stack) - [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis) + - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) - [Queue](src/_DataStructures_/Queue) - [Weave](src/_DataStructures_/Queue/weave) @@ -65,9 +69,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct ## CONTRIBUTION Guide -It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully. +It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully. - When adding a new **problem** with solution + - Take care of the filename convention (Very Important) - Problem statement should be there with examples - Make sure you add the Run Time complexity of your solution @@ -76,7 +81,8 @@ It's great to know that you want to contribute to this repo. Thanks for taking i - Strictly follow ESLINT rules - When adding a Unit Test + - Take care of the file name convention - Make sure CI (Travis) is passing - -Keep an eye on this guide, it's subjected to change frequently. + +Keep an eye on this guide, it's subjected to change frequently. diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js new file mode 100644 index 00000000..1a764534 --- /dev/null +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -0,0 +1,122 @@ +/** + * So what special in this implementation? + * This deals with Dynamic array without a size hence the implemetation uses + * lot of space and I can sense that it can be further improved hence feel free + * to open PR + */ + +class TwoStacks { + constructor() { + this.data = []; + this.top1 = null; + this.top2 = null; + this.stack1Count = 0; + this.stack2Count = 0; + } + + push1(value) { + if (!this.data.length) { + this.data.push(value); + this.top1 = value; + this.stack1Count += 1; + } else { + const arr = []; + for (let i = 0; i < this.stack1Count; i += 1) { + arr.push(this.data[i]); + } + arr.push(value); + for (let i = this.stack1Count; i < this.data.length; i += 1) { + arr.push(this.data[i]); + } + this.data = arr; + this.top1 = value; + this.stack1Count += 1; + } + } + + push2(value) { + if (!this.data.length) { + this.data.push(value); + this.top2 = value; + this.stack2Count += 1; + } else { + const arr = []; + for (let i = 0; i < this.stack1Count; i += 1) { + arr.push(this.data[i]); + } + arr.push(value); + + for (let i = this.stack1Count; i < this.data.length; i += 1) { + arr.push(this.data[i]); + } + this.data = arr; + this.top2 = value; + this.stack2Count += 1; + } + } + + pop1() { + if (!this.top1) { + return null; + } + + const indexOfTop1 = this.data.indexOf(this.top1); + const arr = []; + + delete this.data[indexOfTop1]; + + this.data.forEach(el => arr.push(el)); + + const oldTop = this.top1; + this.top1 = this.data[indexOfTop1 - 1]; + this.data = arr; + return oldTop; + } + + pop2() { + return this.data.pop(); + } +} + +module.exports = TwoStacks; + +/** Test cases */ + +/* + +const s = new TwoStacks(); + +console.log(s.data); + +s.push1(5); +s.push1(4); +console.log(s.data); + + +s.push2(2) +s.push2(1); +console.log(s.data); + +s.push1(14); +console.log(s.data); +console.log(s.top1); + +console.log(s.pop1()) +console.log(s.data); + +console.log(s.pop1()) +console.log(s.data); + +console.log(s.pop2()) +console.log(s.data); + +console.log(s.pop2()) +console.log(s.data); + +console.log(s.pop2()) +console.log(s.data); + +console.log(s.pop1()) +console.log(s.data); + +*/