File tree Expand file tree Collapse file tree 2 files changed +72
-1
lines changed
Expand file tree Collapse file tree 2 files changed +72
-1
lines changed Original file line number Diff line number Diff line change @@ -64,8 +64,12 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
6464| [ Day 34] ( ./day34 ) | [ Merge Sort] ( ./day34 ) | [ http://codetoexpress.tech/dc/day34/ ] ( http://codetoexpress.tech/dc/day34/ ) | ** Intermediate** |
6565| [ Day 35] ( ./day35 ) | [ Quick Sort] ( ./day35 ) | [ http://codetoexpress.tech/dc/day35/ ] ( http://codetoexpress.tech/dc/day35/ ) | ** Intermediate** |
6666| [ Day 36] ( ./day36 ) | [ Radix Sort] ( ./day36 ) | [ http://codetoexpress.tech/dc/day36/ ] ( http://codetoexpress.tech/dc/day36/ ) | ** Intermediate** |
67+ <<<<<<< HEAD
6768| [ Day 37] ( ./day37 ) | [ Radix Sort] ( ./day37 ) | [ http://codetoexpress.tech/dc/day37/ ] ( http://codetoexpress.tech/dc/day37/ ) | ** Misc** |
6869| [ Day 38] ( ./day38 ) | [ Implement Stack Data Structure] ( ./day38 ) | [ http://codetoexpress.tech/dc/day38/ ] ( http://codetoexpress.tech/dc/day38/ ) | ** Beginner** |
70+ =======
71+ | [ Day 37] ( ./day37 ) | [ Search and Sort Applications] ( ./day37 ) | [ http://codetoexpress.tech/dc/day37/ ] ( http://codetoexpress.tech/dc/day37/ ) | ** Misc** |
72+ >>>>>>> 1caed584eb7d890c0d845eff768c09495004b36b
6973
7074## [ More Problems] ( ./BONUS/README.md )
7175
Original file line number Diff line number Diff line change @@ -114,5 +114,72 @@ stk.peek ();
114114Implementation without using JS's array push and pop methods
115115
116116``` js
117- // to be added
117+ /**
118+ * Implemntation of Stack Data Structure in JavaScript
119+ * @author MadhavBahlMD
120+ * @date 11/02/2019
121+ * Method 2 - Stack implementation from scratch (without push() and pop() methods)
122+ */
123+
124+ class Stack {
125+ // Constructor function to initialize the stack
126+ constructor (capacity ) {
127+ this .myStack = [];
128+ this .cap = capacity;
129+ this .first = 0 ;
130+ this .last = 0 ;
131+ this .size = - 1 ;
132+ }
133+
134+ // isEmpty() method to check whether the stack is empty
135+ isEmpty () {
136+ if (this .size === - 1 ) return true ;
137+ return false ;
138+ }
139+
140+ // isFull() method to check whether the stack is full
141+ isFull () {
142+ if (this .size === this .cap - 1 ) return true ;
143+ return false ;
144+ }
145+
146+ // push() method to add an element to the stack
147+ push (element ) {
148+ if (this .isFull ()) {
149+ console .log (' OVERFLOW!' );
150+ return 0 ;
151+ }
152+ console .log (` Pushing ${ element} to the stack!` );
153+ this .size ++ ;
154+ this .myStack [this .size ] = element;
155+ return 1 ;
156+ }
157+
158+ // pop() method to remove topmost element
159+ pop () {
160+ if (this .isEmpty ()) {
161+ console .log (' UNDERFLOW!' );
162+ return 0 ;
163+ }
164+ console .log (` Popped element is: ${ this .myStack [this .size ]} ` );
165+ this .size -- ;
166+ return 1 ;
167+ }
168+
169+ // peek() method to view the toopmost element
170+ peek () {
171+ if (this .isEmpty ()) {
172+ console .log (' Stack is empty!' );
173+ return 0 ;
174+ }
175+ console .log (` Current Element is: ${ this .myStack [this .size ]} ` );
176+ }
177+ }
178+
179+ const stk = new Stack (10 );
180+ stk .pop ();
181+ stk .push (1 );
182+ stk .push (2 );
183+ stk .pop ();
184+ stk .peek ();
118185```
You can’t perform that action at this time.
0 commit comments