File tree 1 file changed +4
-35
lines changed
1 file changed +4
-35
lines changed Original file line number Diff line number Diff line change 1
- class Node {
2
- value : any ;
3
- next : any ;
4
- constructor ( value : any ) {
5
- this . value = value ;
6
- this . next = null ;
7
- }
8
- }
9
1
10
2
class Stack {
11
3
array : any = [ ]
12
- top : any ;
13
- bottom : any ;
14
- length : number ;
15
4
constructor ( ) {
16
- this . top = null ;
17
- this . bottom = null ;
18
- this . length = 0 ;
19
5
}
20
6
21
7
peek ( ) {
22
- if ( this . length == 0 ) return null
23
- return this . top
8
+ if ( this . array . length == 0 ) return null
9
+ return this . array [ this . array . length - 1 ]
24
10
}
25
11
26
12
push ( value : any ) {
27
13
this . array . push ( value )
28
- if ( this . length === 0 ) {
29
- this . top = this . array [ 0 ]
30
- this . bottom = this . array [ 0 ]
31
- } else {
32
- this . top = this . array [ this . length ]
33
- }
34
- this . length ++
35
-
36
14
}
37
15
38
16
pop ( ) {
39
- if ( this . length == 0 ) return null
40
- const remove = this . array . pop ( )
41
- if ( this . length == 0 ) {
42
- this . bottom = null
43
- this . top = null
44
- } else {
45
- this . top = this . array [ this . array . length - 1 ]
46
- }
47
- this . length --
48
- return remove
17
+ return this . array . pop ( )
49
18
}
50
19
51
20
isEmpty ( ) {
52
- return this . length === 0
21
+ return this . array . length === 0
53
22
}
54
23
55
24
//isEmpty
You can’t perform that action at this time.
0 commit comments