The Top Pointer
Instead of an integer index, `top` is a memory pointer that references the Head node of a Singly Linked List.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Implementing a Stack with a Linked List removes the fixed-size limitation of arrays. Memory is allocated dynamically on the heap for each push.
Instead of an integer index, `top` is a memory pointer that references the Head node of a Singly Linked List.
Pushing to a Stack is equivalent to "Insert at Beginning" in a Linked List. `newNode.next = top`, and then `top = newNode`.
Popping is equivalent to "Delete from Beginning". You save the value, and simply move the pointer: `top = top.next`.