-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.kt
43 lines (36 loc) · 1018 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
package com.ds.implementation.stack.linkedList
import com.ds.implementation.linkedlist.singly.ListNode
import com.ds.implementation.stack.common.StackOperation
class Stack<T> : StackOperation<T> {
private var top: ListNode<T>? = null
override fun push(value: T): Boolean {
val newTop = ListNode(value)
if (top == null) {
top = newTop
} else {
newTop.next = top
top = newTop
}
return true
}
override fun pop(): T? {
if (top == null) {
return null
} else {
val topValue = top?.value
val newTop = top?.next
top = newTop
return topValue
}
}
override fun print(msg: String?) {
msg?.let { println(it) }
var current = top
println("Top of Stack is ${current?.value}")
while (current != null)
{
println("Entry -> ${current.value}")
current = current.next
}
}
}