-
-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathStack.kt
More file actions
33 lines (27 loc) · 629 Bytes
/
Stack.kt
File metadata and controls
33 lines (27 loc) · 629 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Stack<T> {
private val list = mutableListOf<T>()
fun push(item: T) {
list.add(item)
}
fun pop(): T? {
return if (list.isEmpty()) {
null
} else list.removeAt(list.size - 1)
}
fun size(): Int = list.size
fun top(): T? {
return if (list.isEmpty()) {
null
} else list[list.size - 1]
}
}
fun main(args: Array<String>) {
val stack = Stack<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
println("Top: ${stack.top()}")
println(stack.pop())
println("Size: ${stack.size()}")
println(stack.pop())
}