|
| 1 | + |
| 2 | + |
| 3 | +# Day 38 - Implementation of Stack Data Structure |
| 4 | + |
| 5 | +## What all did we see till now? |
| 6 | + |
| 7 | +Now that we are proceeding towards the phase 2 of Daily Codes initiative, here are the things that we focused on phase 1, |
| 8 | + |
| 9 | +- Basic questions |
| 10 | +- String Manipulation programs |
| 11 | +- Recursion |
| 12 | +- Array based programs |
| 13 | +- Time and space complexity in various programs |
| 14 | +- Searching algorithms |
| 15 | +- Sorting algorithms |
| 16 | +- Applications based on searching and sorting algorithms |
| 17 | + |
| 18 | +### Help us improve |
| 19 | + |
| 20 | +It's been a long journey, and I hope the people who are following this initiative daily are getting benefitted. |
| 21 | +Moreover, we would love to have your valuable feedback, and suggestions |
| 22 | + |
| 23 | +Just comment on issue [#244](https://github.com/CodeToExpress/dailycodebase/issues/244) - https://github.com/CodeToExpress/dailycodebase/issues/244 - whatever you would like us to know |
| 24 | + |
| 25 | +**Thanks for being a part, we love you all. Let's make this world a better place through code** |
| 26 | + |
| 27 | +## Today's question - Implement a Stack |
| 28 | + |
| 29 | +Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out) |
| 30 | + |
| 31 | +There are many real-life examples of a stack, like a stack of books, where you can add another book at top and remove the book at the top. Another example can be a stack of plates kept one over the other in the kitchen, one can remove the plate kept at top and remove the element kept at the top. |
| 32 | + |
| 33 | +A stack has various methods - |
| 34 | + |
| 35 | +1. `isEmpty()` - **To check whether the stack is empty** |
| 36 | +2. `isFull()` - **To check whether the stack is full** |
| 37 | +3. `push()` - **To add a new element to the stack** |
| 38 | +4. `pop()` - **To remove the topmost element from the stack** |
| 39 | +5. `peek()` - **To view the topmost element in the stack** |
| 40 | + |
| 41 | +Try to implement a stack 😁 |
| 42 | + |
| 43 | +## Solution |
| 44 | + |
| 45 | +## JavaScript Implementation |
| 46 | + |
| 47 | +### [Solution 1](./JavaScript/stack1.js) |
| 48 | + |
| 49 | +Implementation by using JS's array push and pop methods |
| 50 | + |
| 51 | +```js |
| 52 | +/** |
| 53 | + * Implemntation of Stack Data Structure in JavaScript |
| 54 | + * @author MadhavBahlMD |
| 55 | + * @date 11/02/2019 |
| 56 | + * Method 1 - Using JavaScript's push and pop methods |
| 57 | + */ |
| 58 | + |
| 59 | +class Stack { |
| 60 | + // constructor to initialize the stack and it's capacity |
| 61 | + constructor (limit) { |
| 62 | + this.myStack = []; |
| 63 | + this.top = limit; |
| 64 | + } |
| 65 | + |
| 66 | + // isEmpty method to check whether the stack is empty |
| 67 | + isEmpty () { |
| 68 | + if (this.myStack.length === 0) return true; |
| 69 | + else return false; |
| 70 | + } |
| 71 | + |
| 72 | + // isFull method to check whether the stack is full |
| 73 | + isFull () { |
| 74 | + if (this.myStack.length === this.top) return true; |
| 75 | + else return false; |
| 76 | + } |
| 77 | + |
| 78 | + // push method to add a record to the stack |
| 79 | + push (record) { |
| 80 | + if (!this.isFull()) { |
| 81 | + console.log (`Pushing ${record} to the stack!`); |
| 82 | + this.myStack.push (record); |
| 83 | + } else { |
| 84 | + console.log ('Sorry! The Stack is full!'); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // pop method to remove an element from the stack |
| 89 | + pop () { |
| 90 | + if (!this.isEmpty()) { |
| 91 | + console.log (`Popped element is: ${this.myStack[this.myStack.length-1]}`); |
| 92 | + return this.myStack.pop (); |
| 93 | + } else { |
| 94 | + console.log ('Sorry! The Stack is empty'); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // peek method to view the top element |
| 99 | + peek () { |
| 100 | + console.log (`Current element is: ${this.myStack[this.myStack.length - 1]}`); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +const stk = new Stack (10); |
| 105 | +stk.pop (); |
| 106 | +stk.push (1); |
| 107 | +stk.push (2); |
| 108 | +stk.pop (); |
| 109 | +stk.peek (); |
| 110 | +``` |
| 111 | + |
| 112 | +### [Solution 2](./JavaScript/stack2.js) |
| 113 | + |
| 114 | +Implementation without using JS's array push and pop methods |
| 115 | + |
| 116 | +```js |
| 117 | +// to be added |
| 118 | +``` |
0 commit comments