-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.kt
More file actions
71 lines (60 loc) · 1.24 KB
/
stack.kt
File metadata and controls
71 lines (60 loc) · 1.24 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
fun main(args: Array<String>) {
println("Hello")
var s = Stack<Int>()
s.push(1)
s.push(2)
s.push(3)
println(s.count())
println(s)
println(s.pop())
println(s.pop())
println(s.pop())
println(s.pop())
var q = Queue<Int>()
q.insert(1)
q.insert(2)
q.insert(3)
println(q)
println(q.remove())
println(q.remove())
println(q.remove())
println(q.remove())
}
class Stack<T>() {
var items: MutableList<T> = mutableListOf()
fun push(item: T) {
items.add(item)
}
fun pop(): T? {
if (items.isEmpty())
return null
var item = items.last()
items.removeAt(items.size-1)
return item
}
fun count(): Int {
return items.size
}
override fun toString(): String {
return items.toString()
}
}
class Queue<T>() {
var items: MutableList<T> = mutableListOf()
fun insert(item: T) {
items.add(item)
}
fun remove(): T? {
if (items.isEmpty()) {
return null
}
var item = items.removeAt(0)
return item
}
fun count(): Int {
return items.size
}
override fun toString(): String {
return items.toString()
}
}