Skip to content

Instantly share code, notes, and snippets.

@AndyBowes
Created August 1, 2017 16:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyBowes/d632f80ed38a29ca56f3c59280f153a9 to your computer and use it in GitHub Desktop.
Save AndyBowes/d632f80ed38a29ca56f3c59280f153a9 to your computer and use it in GitHub Desktop.
Kotlin Queue Implementation
class Queue<T> {
val elements: MutableList<T> = mutableListOf()
fun isEmpty() = elements.isEmpty()
fun count() = elements.size
fun enqueue(item: T) = elements.add(item)
fun dequeue() = if (!isEmpty()) elements.removeAt(0) else null
fun peek() = if (!isEmpty()) elements[0] else null
override fun toString(): String = elements.toString()
}
fun <T> Queue<T>.push(items: Collection<T>) = items.forEach { this.enqueue(it) }
@voquanghoa
Copy link

I used this snippet to solve some problems on Leetcode but than I found that LinkedList ArrayDeque is better 💃

Anyway, I really appreciate your gist. 💯 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment