Skip to content

Instantly share code, notes, and snippets.

@superbderrick
Created October 31, 2021 06:03
Show Gist options
  • Save superbderrick/0ca53682257e0ce44e8f9dc400eacf1a to your computer and use it in GitHub Desktop.
Save superbderrick/0ca53682257e0ce44e8f9dc400eacf1a to your computer and use it in GitHub Desktop.
kotlin stack
class MutableStack<E>(vararg items: E) { // 1
private val elements = items.toMutableList()
fun push(element: E) = elements.add(element) // 2
fun peek(): E = elements.last() // 3
fun pop(): E = elements.removeAt(elements.size - 1)
fun isEmpty() = elements.isEmpty()
fun size() = elements.size
override fun toString() = "MutableStack(${elements.joinToString()})"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment