Skip to content

Commit 617b99f

Browse files
committed
added notes on stack overflows and underflows
1 parent 3e70bf0 commit 617b99f

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

stack/js/index.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@
22
/**
33
* Simplest Implementation of Stack
44
*/
5-
/* let Stack = function () {
6-
const list = [],
7-
push = data => list.push(data),
8-
pop = () => list.pop(),
9-
size = () => list.length,
10-
viewStack = () => '(bottom) ' + list.join(' -> ') + ' (top)';
11-
return Object.freeze({
12-
push,
13-
pop,
14-
size,
15-
viewStack
16-
});
17-
}
18-
const s = new Stack();
19-
console.log('push operations');
20-
s.push(1); // 1 enters in stack
21-
s.push(2); // 2 enters in stack
22-
s.push(3); // 3 enters in stack
23-
24-
console.log('state of stack is');
25-
console.log(s.viewStack()); // (bottom) 1 -> 2 -> 3 (top)
26-
27-
console.log('pop operations');
28-
console.log(s.pop()); // 3
29-
console.log(s.pop()); // 2
30-
console.log(s.pop()); // 1 */
5+
/* let Stack = function () {
6+
const list = [],
7+
push = data => list.push(data),
8+
pop = () => list.length > 0 ? list.pop() : undefined,
9+
size = () => list.length,
10+
viewStack = () => '(bottom) ' + list.join(' -> ') + ' (top)';
11+
return Object.freeze({
12+
push,
13+
pop,
14+
size,
15+
viewStack
16+
});
17+
}
18+
const s = new Stack();
19+
console.log('push operations');
20+
s.push(1); // 1 enters in stack
21+
s.push(2); // 2 enters in stack
22+
s.push(3); // 3 enters in stack
23+
24+
console.log('state of stack is');
25+
console.log(s.viewStack()); // (bottom) 1 -> 2 -> 3 (top)
26+
27+
console.log('pop operations');
28+
console.log(s.pop()); // 3
29+
console.log(s.pop()); // 2
30+
console.log(s.pop()); // 1 */
3131

3232
let Stack = function () {
3333
const storage = Object.create(null); // key, value pairs

stack/readme.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
> 1. Element/data is inserted to top of the stack
1010
> 2. Element/data is deleted or removed from the top.
1111
12-
Basically delete or removal happens based on **Last In First Out**
12+
Basically, delete or removal happens based on **Last In First Out**
1313

1414
Two operations are supported by stack by default, they are
1515
* ```Push``` - To add data to stack
1616
* ```Pop``` - To remove data from the stack
1717

18+
**Two important concepts** need to know about stack before, we start coding is
19+
1. Stack overflows
20+
This represents the status of the stack when the size of a stack exceeds the maximum allowed number — stack bound — say n. Normally this number is defined before hand.
21+
2. Stack underflows
22+
It is the status when an empty stack is popped. This is not a big issue we can have protection check before we try to pop() a stack.
23+
1824
The simplest implementation of the Stack is
1925
```javascript
2026
/**
@@ -23,7 +29,7 @@ The simplest implementation of the Stack is
2329
let Stack = function () {
2430
const list = [],
2531
push = data => list.push(data),
26-
pop = () => list.pop(),
32+
pop = () => list.length > 0 ? list.pop() : undefined,
2733
size = () => list.length,
2834
viewStack = () => '(bottom) ' + list.join(' -> ') + ' (top)';
2935
return Object.freeze({
@@ -51,10 +57,10 @@ Above code is self explainatory, we basically used an array to acheive the imple
5157

5258
Though the big O time complexity of above code takes ```O(1)``` for push and pop
5359

54-
One drawbacks of above code is
60+
One drawbacks from the above code is
5561
* Array needs to keep its order, hence it takes up a block of space (consecutive memory allocation).
5662

57-
Lets implement with Object.
63+
Moreover every thing in JavaScript is an Object. Lets implement with Object.
5864

5965
1. Basic structure of a stack
6066
```javascript

0 commit comments

Comments
 (0)