-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.kt
45 lines (33 loc) Β· 869 Bytes
/
stack.kt
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
34
35
36
37
38
39
40
41
42
43
44
45
// This representation of the Stack structure in Kotlin was made based on a video for members of the Lucas Montano channel.
class Stack<T> {
private val elements: MutableList<T> = mutableListOf()
override fun toString(): String {
return elements.toString()
}
fun push(item: T) {
elements.add(item)
}
fun pop(): T? {
if (elements.isNotEmpty()) {
return elements.removeAt(elements.size - 1)
}
return null
}
fun peek(): T? {
if (elements.isNotEmpty()) {
return elements.last()
}
return null
}
}
fun main() {
val nameStack = Stack<String>()
nameStack.push("Ramon")
nameStack.push("Ayane")
val popped = nameStack.pop()
println("$popped foi removido")
println("Elementos restante $nameStack")
nameStack.peek()
println("Elementos restantes $nameStack")
println(nameStack.toString())
}