File tree Expand file tree Collapse file tree 5 files changed +188
-262
lines changed Expand file tree Collapse file tree 5 files changed +188
-262
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
( 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} )
5
4
} ( ( typeof module . exports !== undefined ) ? module . exports : window ) )
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 1
1
( function ( exports ) {
2
- let dataStore
3
- let top
2
+ const stack = ( data = [ ] , t = 0 ) => {
3
+ let dataStore = data
4
+ let top = t
4
5
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
16
15
}
17
16
18
- stackProto . peek = ( ) => dataStore [ top - 1 ]
19
-
20
- stackProto . length = ( ) => top
21
-
22
- stackProto . isEmpty = ( ) => top === - 1
17
+ return Object . create ( stackProto )
23
18
}
24
19
25
- Object . assign ( exports , { stack : stackFactory } )
20
+ Object . assign ( exports , { stack} )
26
21
} ( ( typeof module . exports !== undefined ) ? module . exports : window ) )
You can’t perform that action at this time.
0 commit comments