Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 959 Bytes

File metadata and controls

31 lines (24 loc) · 959 Bytes

Stack

Instructions

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 stack
  • remove method - removes the "top" element from the stack
  • peek method - returns "top" element (the one that should be returned) without removing (removeping) it from the stack
  • isEmpty() - returns true if there are elements on the stack, otherwise return false
  • size - numbers of items in the stack

The stack can be implemented in few different ways by using different underlying data structures. Implement each of them:

  • List
  • Linked list

Challenge | Solution

Examples

stack = Stack.new
stack.add(1)
stack.add(2)
stack.remove # 2
stack.remove # 1
stack.remove # null