Enable passing arguments to custom functions
This commit is contained in:
25
api/src/main/kotlin/io/smnp/collection/Stack.kt
Normal file
25
api/src/main/kotlin/io/smnp/collection/Stack.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package io.smnp.collection
|
||||
|
||||
class Stack<T> private constructor(private val list: MutableList<T>) : List<T> by list {
|
||||
fun push(item: T) {
|
||||
list.add(item)
|
||||
}
|
||||
|
||||
fun pop(): T? {
|
||||
if(list.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
|
||||
val last = list.last()
|
||||
list.removeAt(list.size-1)
|
||||
return last
|
||||
}
|
||||
|
||||
fun top() = list.last()
|
||||
|
||||
companion object {
|
||||
fun <T> of(vararg items: T): Stack<T> {
|
||||
return Stack(mutableListOf(*items))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user