@@ -4,16 +4,16 @@ export default class Stack {
44 constructor ( ) {
55 // We're going to implement Stack based on LinkedList since these
66 // 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.
88 this . linkedList = new LinkedList ( ) ;
99 }
1010
1111 /**
1212 * @return {boolean }
1313 */
1414 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 ;
1717 }
1818
1919 /**
@@ -25,27 +25,27 @@ export default class Stack {
2525 return null ;
2626 }
2727
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 ;
3030 }
3131
3232 /**
3333 * @param {* } value
3434 */
3535 push ( value ) {
3636 // 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 ) ;
3939 }
4040
4141 /**
4242 * @return {* }
4343 */
4444 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 ;
4949 }
5050
5151 /**
@@ -54,8 +54,7 @@ export default class Stack {
5454 toArray ( ) {
5555 return this . linkedList
5656 . toArray ( )
57- . map ( linkedListNode => linkedListNode . value )
58- . reverse ( ) ;
57+ . map ( linkedListNode => linkedListNode . value ) ;
5958 }
6059
6160 /**
0 commit comments