Skip to content

Commit a3d5768

Browse files
committed
refactored from ES6 classes to factory function to Stack Data Strucutre
1 parent 0b36c2d commit a3d5768

File tree

5 files changed

+188
-262
lines changed

5 files changed

+188
-262
lines changed

04-chapter-Stack/04-chapter-Stacks.js

Lines changed: 0 additions & 240 deletions
This file was deleted.

04-chapter-Stack/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
(function (exports) {
2-
const stack = require('./stack.module')
3-
4-
Object.assign(exports, stack)
2+
const {stack} = require('./stack.module')
3+
Object.assign(exports, {stack})
54
}((typeof module.exports !== undefined) ? module.exports : window))

04-chapter-Stack/readme.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Stacks with ES6
2+
3+
### Code Examples
4+
- [Stack Data Structure](./stack.module.js)
5+
- [Import Module](./index.js)
6+
7+
### Description
8+
9+
Pattern type: Last item In is the First item Out (LIFO)
10+
11+
![](https://cdn-images-1.medium.com/max/800/0*S3Kr9Cpm16ZmCEad.png)
12+
13+
common operations you can perform on graphs:
14+
- `push`: adds a new element to the stack
15+
- `pop`: remove the last item pushed from the stack
16+
- `peek`: returns the last item pushed to stack
17+
- `length`: returns the size of the stack
18+
- `isEmpty`: returns true if the stack has elements or false if it has no elements
19+
- `getStack`: returns the data of the stack
20+
21+
### Example use case
22+
- Using the back and forward buttons in your browser
23+
- dfs search algorithm for graphs search
24+
- balanced arithmetic operations
25+
- convert infix to postfix arithmetic operations
26+
- JS engine uses a stack data structure to execute the algorithms
27+
28+
29+
### Resources
30+
31+
- [A Gentle Introduction to Data Structures: How Stacks Work](https://medium.freecodecamp.com/data-structures-stacks-on-stacks-c25f2633c529#.3omw867a9)

04-chapter-Stack/stack.module.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
(function (exports) {
2-
let dataStore
3-
let top
2+
const stack = (data = [], t = 0) => {
3+
let dataStore = data
4+
let top = t
45

5-
const stackFactory = (data = [], t = -1) => {
6-
let stackProto = {}
7-
dataStore = data
8-
top = t
9-
10-
stackProto.push = (element) => {
11-
dataStore[top++] = element
12-
}
13-
14-
stackProto.pop = () => {
15-
dataStore.splice(--top, 1)
6+
const stackProto = {
7+
push (element) {
8+
dataStore[top++] = element
9+
},
10+
pop: () => dataStore.splice(--top, 1),
11+
peek: () => dataStore[top - 1],
12+
length: () => top,
13+
isEmpty: () => top === 0,
14+
getStack: () => dataStore
1615
}
1716

18-
stackProto.peek = () => dataStore[top - 1]
19-
20-
stackProto.length = () => top
21-
22-
stackProto.isEmpty = () => top === -1
17+
return Object.create(stackProto)
2318
}
2419

25-
Object.assign(exports, {stack: stackFactory})
20+
Object.assign(exports, {stack})
2621
}((typeof module.exports !== undefined) ? module.exports : window))

0 commit comments

Comments
 (0)