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..8ad7931c --- /dev/null +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -0,0 +1,85 @@ +/** + * Revision to PR #35 where I implemented bullshit thinking of + * new breakthrough :D + */ + +class TwoStacks { + constructor(capacity) { + this.data = []; + this.top1 = -1; + this.top2 = capacity; + this.overflow = new Error('Overflow: Stack is full'); + + this.capacity = capacity; + } + + push1(value) { + if (this.top1 < this.top2 - 1) { + this.top1 += 1; + this.data[this.top1] = value; + } else { + throw this.overflow; + } + } + + push2(value) { + if (this.top1 < this.top2 - 1) { + this.top2 -= 1; + this.data[this.top2] = value; + } else { + throw this.overflow; + } + } + + pop1() { + if (this.top1 >= 0) { + const item = this.data[this.top1]; + delete this.data[this.top1]; + this.top1 -= 1; + return item; + } + return -1; + } + + pop2() { + if (this.top2 < this.capacity) { + const item = this.data[this.top2]; + delete this.data[this.top2]; + this.top2 += 1; + return item; + } + return -1; + } +} + +module.exports = TwoStacks; + +/** Test cases */ + +/* +const s = new TwoStacks(4); + +s.push1('a'); +console.log(s.data); + +s.push2('a2'); +console.log(s.data); + +s.push1('b'); +console.log(s.data); + +s.push2('b2'); +console.log(s.data); + +s.push2('d2'); +console.log(s.data); + +s.push2('c23'); +console.log(s.data); + +console.log(s.pop2()); +console.log(s.data); + +console.log(s.pop1()); +console.log(s.data); +*/