@@ -4,16 +4,16 @@ export default class Stack {
4
4
constructor ( ) {
5
5
// We're going to implement Stack based on LinkedList since these
6
6
// structures are quite similar. Compare push/pop operations of the Stack
7
- // with append/deleteTail operations of LinkedList.
7
+ // with prepend/deleteHead operations of LinkedList.
8
8
this . linkedList = new LinkedList ( ) ;
9
9
}
10
10
11
11
/**
12
12
* @return {boolean }
13
13
*/
14
14
isEmpty ( ) {
15
- // The stack is empty if its linked list doesn't have a tail .
16
- return ! this . linkedList . tail ;
15
+ // The stack is empty if its linked list doesn't have a head .
16
+ return ! this . linkedList . head ;
17
17
}
18
18
19
19
/**
@@ -25,27 +25,27 @@ export default class Stack {
25
25
return null ;
26
26
}
27
27
28
- // Just read the value from the end of linked list without deleting it.
29
- return this . linkedList . tail . value ;
28
+ // Just read the value from the start of linked list without deleting it.
29
+ return this . linkedList . head . value ;
30
30
}
31
31
32
32
/**
33
33
* @param {* } value
34
34
*/
35
35
push ( value ) {
36
36
// Pushing means to lay the value on top of the stack. Therefore let's just add
37
- // the new value at the end of the linked list.
38
- this . linkedList . append ( value ) ;
37
+ // the new value at the start of the linked list.
38
+ this . linkedList . prepend ( value ) ;
39
39
}
40
40
41
41
/**
42
42
* @return {* }
43
43
*/
44
44
pop ( ) {
45
- // Let's try to delete the last node (the tail ) from the linked list.
46
- // If there is no tail (the linked list is empty) just return null.
47
- const removedTail = this . linkedList . deleteTail ( ) ;
48
- return removedTail ? removedTail . value : null ;
45
+ // Let's try to delete the first node (the head ) from the linked list.
46
+ // If there is no head (the linked list is empty) just return null.
47
+ const removedHead = this . linkedList . deleteHead ( ) ;
48
+ return removedHead ? removedHead . value : null ;
49
49
}
50
50
51
51
/**
@@ -54,8 +54,7 @@ export default class Stack {
54
54
toArray ( ) {
55
55
return this . linkedList
56
56
. toArray ( )
57
- . map ( linkedListNode => linkedListNode . value )
58
- . reverse ( ) ;
57
+ . map ( linkedListNode => linkedListNode . value ) ;
59
58
}
60
59
61
60
/**
0 commit comments