File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed
javascript-today-magazine Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * The Stack Data Structure
3
+ * A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
4
+ * This means the last element inserted inside the stack is removed first. 🔥🔥🔥
5
+ * You can think of the stack data structure as the pile of plates on top of another.
6
+ * Here's a JavaScript implementation of a stack data structure 👏👏👏
7
+ */
8
+ var Stack = /** @class */ ( function ( ) {
9
+ // private totalItems: number;
10
+ function Stack ( ) {
11
+ this . data = [ ] ;
12
+ this . data = [ ] ;
13
+ }
14
+ /**
15
+ * push
16
+ */
17
+ Stack . prototype . push = function ( record ) {
18
+ this . data . push ( record ) ;
19
+ } ;
20
+ /**
21
+ * pop
22
+ */
23
+ Stack . prototype . pop = function ( ) {
24
+ return this . data . pop ( ) ;
25
+ } ;
26
+ /**
27
+ * peek
28
+ */
29
+ Stack . prototype . peek = function ( ) {
30
+ return this . data [ this . data . length - 1 ] ;
31
+ } ;
32
+ return Stack ;
33
+ } ( ) ) ;
34
+ var instanceOfStack = new Stack ( ) ;
35
+ instanceOfStack . push ( 1 ) ;
36
+ instanceOfStack . push ( 2 ) ;
37
+ instanceOfStack . push ( 12 ) ;
38
+ instanceOfStack . push ( 14 ) ;
39
+ instanceOfStack . push ( 3 ) ;
40
+ console . log ( instanceOfStack ) ;
41
+ console . log ( instanceOfStack . peek ( ) ) ;
42
+ instanceOfStack . pop ( ) ;
43
+ console . log ( instanceOfStack ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * The Stack Data Structure
3
+ * A stack is a linear data structure that follows the principle of Last In First Out (LIFO).
4
+ * This means the last element inserted inside the stack is removed first. 🔥🔥🔥
5
+ * You can think of the stack data structure as the pile of plates on top of another.
6
+ * Here's a JavaScript implementation of a stack data structure 👏👏👏
7
+ */
8
+
9
+ class Stack {
10
+ private data : ( number | string | undefined ) [ ] = [ ] ;
11
+ // private totalItems: number;
12
+ constructor ( ) {
13
+ this . data = [ ] ;
14
+ }
15
+
16
+ /**
17
+ * push
18
+ */
19
+ public push ( record : number | string ) {
20
+ this . data . push ( record ) ;
21
+ }
22
+ /**
23
+ * pop
24
+ */
25
+ public pop ( ) {
26
+ return this . data . pop ( )
27
+ }
28
+
29
+ /**
30
+ * peek
31
+ */
32
+ public peek ( ) {
33
+ return this . data [ this . data . length - 1 ] ;
34
+ }
35
+ }
36
+
37
+ const instanceOfStack = new Stack ( ) ;
38
+ instanceOfStack . push ( 1 ) ;
39
+ instanceOfStack . push ( 2 ) ;
40
+ instanceOfStack . push ( 12 ) ;
41
+ instanceOfStack . push ( 14 ) ;
42
+ instanceOfStack . push ( 3 ) ;
43
+ console . log ( instanceOfStack ) ;
44
+ console . log ( instanceOfStack . peek ( ) ) ;
45
+ instanceOfStack . pop ( ) ;
46
+ console . log ( instanceOfStack ) ;
You can’t perform that action at this time.
0 commit comments