Skip to content

Commit 753fcc5

Browse files
committed
Add day 38
1 parent 29af59b commit 753fcc5

File tree

6 files changed

+245
-0
lines changed

6 files changed

+245
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Motivate yourself to code daily till 60 days, and see the magic! Coding will bec
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** |
6767
| [Day 37](./day37) | [Radix Sort](./day37) | [http://codetoexpress.tech/dc/day37/](http://codetoexpress.tech/dc/day37/) | **Misc** |
68+
| [Day 38](./day38) | [Implement Stack Data Structure](./day38) | [http://codetoexpress.tech/dc/day38/](http://codetoexpress.tech/dc/day38/) | **Beginner** |
6869

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

day37/ques.png

658 KB
Loading

day38/Daily Codes.png

143 KB
Loading

day38/JavaScript/stack1.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Implemntation of Stack Data Structure in JavaScript
3+
* @author MadhavBahlMD
4+
* @date 11/02/2019
5+
* Method 1 - Using JavaScript's push and pop methods
6+
*/
7+
8+
class Stack {
9+
// constructor to initialize the stack and it's capacity
10+
constructor (limit) {
11+
this.myStack = [];
12+
this.top = limit;
13+
}
14+
15+
// isEmpty method to check whether the stack is empty
16+
isEmpty () {
17+
if (this.myStack.length === 0) return true;
18+
else return false;
19+
}
20+
21+
// isFull method to check whether the stack is full
22+
isFull () {
23+
if (this.myStack.length === this.top) return true;
24+
else return false;
25+
}
26+
27+
// push method to add a record to the stack
28+
push (record) {
29+
if (!this.isFull()) {
30+
console.log (`Pushing ${record} to the stack!`);
31+
this.myStack.push (record);
32+
} else {
33+
console.log ('Sorry! The Stack is full!');
34+
}
35+
}
36+
37+
// pop method to remove an element from the stack
38+
pop () {
39+
if (!this.isEmpty()) {
40+
console.log (`Popped element is: ${this.myStack[this.myStack.length-1]}`);
41+
return this.myStack.pop ();
42+
} else {
43+
console.log ('Sorry! The Stack is empty');
44+
}
45+
}
46+
47+
// peek method to view the top element
48+
peek () {
49+
console.log (`Current element is: ${this.myStack[this.myStack.length - 1]}`);
50+
}
51+
}
52+
53+
const stk = new Stack (10);
54+
stk.pop ();
55+
stk.push (1);
56+
stk.push (2);
57+
stk.pop ();
58+
stk.peek ();

day38/JavaScript/stack2.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Implemntation of Stack Data Structure in JavaScript
3+
* @author MadhavBahlMD
4+
* @date 11/02/2019
5+
* Method 2 - Stack implementation from scratch (without push() and pop() methods)
6+
*/
7+
8+
class Stack {
9+
// Constructor function to initialize the stack
10+
constructor (capacity) {
11+
this.myStack = [];
12+
this.cap = capacity;
13+
this.first = 0;
14+
this.last = 0;
15+
this.size = -1;
16+
}
17+
18+
// isEmpty() method to check whether the stack is empty
19+
isEmpty () {
20+
if (this.size === -1) return true;
21+
return false;
22+
}
23+
24+
//isFull() method to check whether the stack is full
25+
isFull () {
26+
if (this.size === this.cap -1) return true;
27+
return false;
28+
}
29+
30+
// push() method to add an element to the stack
31+
push (element) {
32+
if (this.isFull()) {
33+
console.log ('OVERFLOW!');
34+
return 0;
35+
}
36+
console.log (`Pushing ${element} to the stack!`);
37+
this.size++;
38+
this.myStack[this.size] = element;
39+
return 1;
40+
}
41+
42+
// pop() method to remove topmost element
43+
pop () {
44+
if (this.isEmpty()) {
45+
console.log ('UNDERFLOW!');
46+
return 0;
47+
}
48+
console.log (`Popped element is: ${this.myStack[this.size]}`);
49+
this.size--;
50+
return 1;
51+
}
52+
53+
// peek() method to view the toopmost element
54+
peek () {
55+
if (this.isEmpty()) {
56+
console.log ('Stack is empty!');
57+
return 0;
58+
}
59+
console.log (`Current Element is: ${this.myStack[this.size]}`);
60+
}
61+
}
62+
63+
const stk = new Stack (10);
64+
stk.pop ();
65+
stk.push (1);
66+
stk.push (2);
67+
stk.pop ();
68+
stk.peek ();

day38/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
![cover](./cover.png)
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

Comments
 (0)