awelgorithms

Build a stack from values

Updated

Building from a sequence means pushing each value in order. The first value is the bottom. The last value is the top. Empty input stays empty.

What to focus on

Left-to-right push: values [4, 1, 9] become stack [4, 1, 9] with top 9. If you needed the first value on top, you would push right-to-left. Say so.

Worked examples

three values

a = [4, 1, 9]
  1. push 4, then 1, then 9
  2. top 9

Result: [4, 1, 9]

one value

a = [7]
  1. one push

Result: [7]

empty

a = []
  1. no pushes

Result: []

Try it

Write fromArray(values). values is a list of integers to push left to right. The last value becomes the top. Push each value in order. Empty input stays empty. [4, 1, 9] has top 9. [7] has top 7. [] stays [].

values is a list of integers. You may build an empty stack.