From c2a50029b4cac7243625cd8e4c5f45c9eba258d8 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 12:58:59 +0530 Subject: [PATCH 1/3] update: add new stack problem --- .../Stack/2-stacks-using1-array/index.js | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/_DataStructures_/Stack/2-stacks-using1-array/index.js 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..47219a05 --- /dev/null +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -0,0 +1,115 @@ +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); + +*/ From 3eab705998e62965d50a7a737740814c6cc611f7 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 12:59:52 +0530 Subject: [PATCH 2/3] update: entry in readme --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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. From 3694b785981f100269010377760d7f53024ee407 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sun, 6 Oct 2019 13:01:17 +0530 Subject: [PATCH 3/3] update: infor added --- src/_DataStructures_/Stack/2-stacks-using1-array/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js index 47219a05..1a764534 100644 --- a/src/_DataStructures_/Stack/2-stacks-using1-array/index.js +++ b/src/_DataStructures_/Stack/2-stacks-using1-array/index.js @@ -1,3 +1,10 @@ +/** + * 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 = [];