kotlin-coding-challenges

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:

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

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