Created
October 31, 2021 06:03
-
-
Save superbderrick/0ca53682257e0ce44e8f9dc400eacf1a to your computer and use it in GitHub Desktop.
kotlin stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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