Skip to content

Commit ff40293

Browse files
committed
Add day 39
1 parent 8345932 commit ff40293

File tree

5 files changed

+331
-0
lines changed

5 files changed

+331
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
6666
| [Day 36](./day36) | [Radix Sort](./day36) | [http://codetoexpress.tech/dc/day36/](http://codetoexpress.tech/dc/day36/) | **Intermediate** |
6767
| [Day 37](./day37) | [Radix Sort](./day37) | [http://codetoexpress.tech/dc/day37/](http://codetoexpress.tech/dc/day37/) | **Misc** |
6868
| [Day 38](./day38) | [Implement Stack Data Structure](./day38) | [http://codetoexpress.tech/dc/day38/](http://codetoexpress.tech/dc/day38/) | **Beginner** |
69+
| [Day 39](./day39) | [Maximum Element and Reverse Stack](./day39) | [http://codetoexpress.tech/dc/day39/](http://codetoexpress.tech/dc/day39/) | **Intermediate** |
6970

7071
## [More Problems](./BONUS/README.md)
7172

day39/Daily Codes.png

143 KB
Loading

day39/JavaScript/maxElement.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Find maximum element in the stack
3+
* @author MadhavBahlMD
4+
* @date 12/02/2019
5+
*/
6+
7+
class Stack {
8+
constructor (limit) {
9+
this.myStack = [];
10+
this.tempStack = [];
11+
this.capacity = limit;
12+
}
13+
14+
// Implement isFull() method
15+
isFull () {
16+
if (this.myStack.length === this.capacity) return true;
17+
return false;
18+
}
19+
20+
// Implement isEmpty() method
21+
isEmpty () {
22+
if (this.myStack.length === 0) return true;
23+
return false;
24+
}
25+
26+
// Implement push(x) method
27+
push (element) {
28+
if (this.isFull()) return false;
29+
this.myStack.push(element);
30+
return true;
31+
}
32+
33+
// Implement pop() method
34+
pop () {
35+
if (this.isEmpty()) return -1;
36+
return this.myStack.pop();
37+
}
38+
39+
// Implement the peek() function
40+
peek () {
41+
return this.myStack[this.myStack.length - 1];
42+
}
43+
44+
// Implement findMax() method
45+
findMax () {
46+
this.tempStack = this.myStack;
47+
let max = this.peek();
48+
49+
while (!this.isEmpty()) {
50+
let currentTop = this.pop();
51+
if (currentTop > max) max = currentTop;
52+
}
53+
54+
this.myStack = this.tempStack;
55+
return max;
56+
}
57+
}
58+
59+
const stk = new Stack (10);
60+
stk.push (1);
61+
stk.push (2);
62+
stk.push (19);
63+
stk.push (12);
64+
stk.push (16);
65+
stk.push (4);
66+
stk.push (25);
67+
stk.push (5);
68+
69+
console.log (stk.findMax());

day39/JavaScript/stackReverse.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Reverse the stack
3+
* @author MadhavBahlMD
4+
* @date 12/02/2019
5+
*/
6+
7+
class Stack {
8+
constructor (limit) {
9+
this.myStack = [];
10+
this.tempStack = [];
11+
this.capacity = limit;
12+
}
13+
14+
// Implement isFull() method
15+
isFull () {
16+
if (this.myStack.length === this.capacity) return true;
17+
return false;
18+
}
19+
20+
// Implement isEmpty() method
21+
isEmpty () {
22+
if (this.myStack.length === 0) return true;
23+
return false;
24+
}
25+
26+
// Implement push(x) method
27+
push (element) {
28+
if (this.isFull()) return false;
29+
this.myStack.push(element);
30+
return true;
31+
}
32+
33+
// Implement pop() method
34+
pop () {
35+
if (this.isEmpty()) return -1;
36+
return this.myStack.pop();
37+
}
38+
39+
// Implement the peek() function
40+
peek () {
41+
return this.myStack[this.myStack.length - 1];
42+
}
43+
44+
// Implement reverse() method
45+
reverse () {
46+
this.tempStack = [];
47+
while (!this.isEmpty()) {
48+
let top = this.pop ();
49+
console.log ('temp = ', top);
50+
this.tempStack.push (top);
51+
}
52+
this.myStack = this.tempStack;
53+
}
54+
55+
// Implement displayAll() method
56+
displayAll () {
57+
console.log ('/* ==== My Stack ==== */');
58+
console.log ('Top');
59+
60+
this.tempStack = this.myStack;
61+
while (!this.isEmpty())
62+
console.log (this.pop());
63+
this.myStack = this.tempStack;
64+
65+
console.log ('Bottom');
66+
}
67+
}
68+
69+
const stk = new Stack (10);
70+
stk.push (1);
71+
stk.push (2);
72+
stk.push (19);
73+
stk.push (12);
74+
stk.push (16);
75+
stk.push (4);
76+
stk.push (25);
77+
stk.push (5);
78+
79+
stk.displayAll ();
80+
stk.reverse ();
81+
stk.displayAll ();

day39/README.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
![cover](./cover.png)
2+
3+
# Day 39 - Maximum Element and Stack Reversal
4+
5+
## Question 1
6+
7+
Find the maximum element in the stack
8+
9+
## Question 2
10+
11+
Reverse the given stack
12+
13+
**HINT**
14+
15+
- You can use another stack, pop the elements of first stack and push them onto the second stack
16+
- You can reverse without another data structure using recursion
17+
18+
## Solution to Question 1
19+
20+
### [JavaScript Implementation](./JavaScript/maxElement.js)
21+
22+
```js
23+
/**
24+
* Find maximum element in the stack
25+
* @author MadhavBahlMD
26+
* @date 12/02/2019
27+
*/
28+
29+
class Stack {
30+
constructor (limit) {
31+
this.myStack = [];
32+
this.tempStack = [];
33+
this.capacity = limit;
34+
}
35+
36+
// Implement isFull() method
37+
isFull () {
38+
if (this.myStack.length === this.capacity) return true;
39+
return false;
40+
}
41+
42+
// Implement isEmpty() method
43+
isEmpty () {
44+
if (this.myStack.length === 0) return true;
45+
return false;
46+
}
47+
48+
// Implement push(x) method
49+
push (element) {
50+
if (this.isFull()) return false;
51+
this.myStack.push(element);
52+
return true;
53+
}
54+
55+
// Implement pop() method
56+
pop () {
57+
if (this.isEmpty()) return -1;
58+
return this.myStack.pop();
59+
}
60+
61+
// Implement the peek() function
62+
peek () {
63+
return this.myStack[this.myStack.length - 1];
64+
}
65+
66+
// Implement findMax() method
67+
findMax () {
68+
this.tempStack = this.myStack;
69+
let max = this.peek();
70+
71+
while (!this.isEmpty()) {
72+
let currentTop = this.pop();
73+
if (currentTop > max) max = currentTop;
74+
}
75+
76+
this.myStack = this.tempStack;
77+
return max;
78+
}
79+
}
80+
81+
const stk = new Stack (10);
82+
stk.push (1);
83+
stk.push (2);
84+
stk.push (19);
85+
stk.push (12);
86+
stk.push (16);
87+
stk.push (4);
88+
stk.push (25);
89+
stk.push (5);
90+
91+
console.log (stk.findMax());
92+
```
93+
94+
## Solution to Question 2
95+
96+
### [JavaScript Implementation](./JavaScript/stackReverse.js)
97+
98+
```js
99+
/**
100+
* Reverse the stack
101+
* @author MadhavBahlMD
102+
* @date 12/02/2019
103+
*/
104+
105+
class Stack {
106+
constructor (limit) {
107+
this.myStack = [];
108+
this.tempStack = [];
109+
this.capacity = limit;
110+
}
111+
112+
// Implement isFull() method
113+
isFull () {
114+
if (this.myStack.length === this.capacity) return true;
115+
return false;
116+
}
117+
118+
// Implement isEmpty() method
119+
isEmpty () {
120+
if (this.myStack.length === 0) return true;
121+
return false;
122+
}
123+
124+
// Implement push(x) method
125+
push (element) {
126+
if (this.isFull()) return false;
127+
this.myStack.push(element);
128+
return true;
129+
}
130+
131+
// Implement pop() method
132+
pop () {
133+
if (this.isEmpty()) return -1;
134+
return this.myStack.pop();
135+
}
136+
137+
// Implement the peek() function
138+
peek () {
139+
return this.myStack[this.myStack.length - 1];
140+
}
141+
142+
// Implement reverse() method
143+
reverse () {
144+
this.tempStack = [];
145+
while (!this.isEmpty()) {
146+
let top = this.pop ();
147+
console.log ('temp = ', top);
148+
this.tempStack.push (top);
149+
}
150+
this.myStack = this.tempStack;
151+
}
152+
153+
// Implement displayAll() method
154+
displayAll () {
155+
console.log ('/* ==== My Stack ==== */');
156+
console.log ('Top');
157+
158+
this.tempStack = this.myStack;
159+
while (!this.isEmpty())
160+
console.log (this.pop());
161+
this.myStack = this.tempStack;
162+
163+
console.log ('Bottom');
164+
}
165+
}
166+
167+
const stk = new Stack (10);
168+
stk.push (1);
169+
stk.push (2);
170+
stk.push (19);
171+
stk.push (12);
172+
stk.push (16);
173+
stk.push (4);
174+
stk.push (25);
175+
stk.push (5);
176+
177+
stk.displayAll ();
178+
stk.reverse ();
179+
stk.displayAll ();
180+
```

0 commit comments

Comments
 (0)