A stack can be implemented using any linear collection. In this section, we're going implement it using Array and LinkedList. Using an array, we can implement stacks of fixed and dynamic sizes.
Implementing stacks
Stacks with a fixed size
Although a fixed sized stack can be implemented using any linear data structure, we'll implement it using an array for simplicity.
In this implementation, as the stack size is fixed, the push() operation throws StackOverflowException after the stack is full. Similarly, the pop() operation throws StackUnderflowException when the stack is empty.
Here's is the implementation:
class FixedStack<E> {
private val elements: Array<Any?>
private var size = 0
constructor...