Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Queue

Nice to solve before

Instructions

Implement a queue data structure. Adding to the queue should store an element until it is removed. First element added to a queue will be the first that is removed (FIFO). The queue should be a class with methods:

  • add method - adds element to a queue (enqueue the element)
  • remove method - removes the element from a queue (enqueue the element)
  • peek method - returns last element (the one that should be returned) without removing it from the queue
  • isEmpty method - return true if queue is empty, otherwise return false

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

  • List
  • Linked list
  • Two Stacks

Challenge | Solution

Examples

Example 1

val q = Queue<Int>()
q.add(1)
q.remove() // 1
q.remove() // null

Example 2

val q = Queue<Char>()
q.isEmpty() // true
q.add('A')
q.isEmpty() // false
q.add('B')
q.add('C')
q.remove() // A
q.peek() // A
q.peek() // A
q.remove() // B
q.remove() // C
q.remove() // null