Implement Stack using Array

Easy

Implement a stack using an array as the underlying container.

The Stack class should support the following methods:

  • int size()
  • boolean isEmpty()
  • int top()
  • void push(int element)
  • void pop()

Main method:

  • Stack created of size 10
  • Printed: stack.top() stack.isEmpty() stack.size()
  • Pushed: 5
  • Printed: stack.top() stack.isEmpty() stack.size()
  • Pushed: 6
  • Printed: stack.top() stack.isEmpty() stack.size()
  • Pushed: 7
  • Printed: stack.top() stack.isEmpty() stack.size()
  • Popped
  • Printed: stack.top() stack.isEmpty() stack.size()
  • Popped
  • Printed: stack.top() stack.isEmpty() stack.size()
  • Popped
  • Printed: stack.top() stack.isEmpty() stack.size()

Expected Output

-1 true 0
5 false 1
6 false 2
7 false 3
6 false 2
5 false 1
-1 true 0
Editorial Link: Editorial