The Stack
class is designed to:
Each Node
contains:
val
) of type Object
.next
).top
: Points to the top node of the stack.push(Object v)
Adds a new element to the top of the stack.
Creates a new Node
and updates the top
reference.
pop()
Removes and returns the top element of the stack.
Throws a RuntimeException
if the stack is empty.
peek()
Returns the top element without removing it.
Throws a RuntimeException
if the stack is empty.
isEmpty()
Returns true
if the stack is empty, otherwise false
.