Implement a stack data structure. Adding to the stack should store an element until it is removed. First element added to a stack will be the last that is removed (FILO).
The stack should be a class with members:
add
method - adds element to the stackremove
method - removes the “top” element from the stackpeek
method - returns “top” element (the one that should be returned) without removing (removeping) it from the stackisEmpty()
- returns true if there are elements on the stack, otherwise return falsesize
- numbers of items in the stackThe stack can be implemented in few different ways by using different underlying data structures. Implement each of them:
Challenge | Solution |
val s = new Stack()
s.add(1)
s.add(2)
s.remove() // 2
s.remove() // 1
s.remove() // null