Just like the previous example, top
is the pointer which tracks the position where new elements should be stored. If the top is equal to null
(which is its starting point), then the stack is empty.
1push(item) {
2 this.top = new Node(item, this.top);
3}