forked from Vatsalparsaniya/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.kt
More file actions
34 lines (25 loc) · 706 Bytes
/
Copy pathStack.kt
File metadata and controls
34 lines (25 loc) · 706 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
34
class Stack<T:Comparable<T>>(list:MutableList<T>) {
var items: MutableList<T> = list
fun isEmpty():Boolean = this.items.isEmpty()
fun count():Int = this.items.count()
fun push(element:T) {
val position = this.count()
this.items.add(position, element)
}
override fun toString() = this.items.toString()
fun pop():T? {
if (this.isEmpty()) {
return null
} else {
val item = this.items.count() - 1
return this.items.removeAt(item)
}
}
fun peek():T? {
if (isEmpty()) {
return null
} else {
return this.items[this.items.count() - 1]
}
}
}