A stack is a LIFO (last-in, first-out) data structure. The
core operations are push
which adds an element to the stack
and pop
with removes the top element of the stack.
Implement a Stack
which can...
push
an element onto the stackpop
remove the top element from the stack and return the elementcount
the number of elements on the stackpeek
at the top element without removing itmax
find the largest value in the stack
You can assume that this stack only holds integers.
While you can use a Ruby or JavaScript Array
, don't use the following built-in
methods on Array:
push
/unshift
(or shovel operator in Ruby)pop
/shift
max
/max_by
/min
/min_by
count
/length
first
/last
And, for an added challenge, can you do it without using negative array indexes?