-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.kt
134 lines (107 loc) · 3.33 KB
/
Queue.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package structures
import kotlin.collections.ArrayList
/**
* data structure: queue
*
* description: the queue is organized on a FIFO basis (first in, first out), all operations are performed in O(1),
* except for the operation of deleting from the middle of the queue, which in the worst case takes O(n) time
*
*/
interface Queue<T> {
/**
* adding to the front of the queue
*
* @param item - added element
*/
fun offer(item: T)
/**
* returns the element from the front of the queue
*
* if the queue is empty, throws an IllegalStateException
*/
fun element() : T
/**
* returns the element from the front of the queue
*
* if the queue is empty it will return null
*/
fun peek() : T?
/**
* removes and returns an element from the front of the queue
*
* if the queue is empty, throws an IllegalStateException
*/
fun remove() : T
/**
* removes and returns an element from the front of the queue
*
* if the queue is empty it will return null
*/
fun poll() : T?
/**
*
* @return returns true if the queue is empty
*/
fun isEmpty() : Boolean
/**
* clears the queue
*
*/
fun clear()
/**
* removes an element from the middle of the queue
*
* @return returns true if the element was successfully removed
*/
fun remove(item: T) : Boolean
/**
* implementation using dynamic ArrayList
*
*/
class ArrayListQueue<T> : Queue<T> {
private val data = ArrayList<T>()
override fun offer(item: T) = data.add(0, item)
override fun isEmpty() = data.isEmpty()
override fun clear() = data.clear()
override fun element() = if (isEmpty()) thr("queue is empty!") else data.first()
override fun peek() = if (isEmpty()) null else data.first()
override fun remove() = if (isEmpty()) thr("queue is empty!") else data.removeFirst()
override fun poll() = if (isEmpty()) null else data.removeFirst()
override fun remove(item: T) : Boolean {
return if (data.contains(item)) {
data.remove(item)
true
} else {
false
}
}
private fun thr(msg: String) : Nothing {
throw IllegalStateException(msg)
}
}
/**
* implementation using linked list LinkedList
*
*/
class LinkedListQueue<T> : Queue<T> {
private val data = java.util.LinkedList<T>()
override fun offer(item: T) = data.add(0, item)
override fun isEmpty() = data.isEmpty()
override fun clear() = data.clear()
override fun element() : T = if (isEmpty()) thr("queue is empty!") else data.peekFirst()
override fun peek() = if (isEmpty()) null else data.peekFirst()
override fun remove(): T = if (isEmpty()) thr("queue is empty!") else data.removeFirst()
override fun poll() = if (isEmpty()) null else data.removeFirst()
override fun remove(item: T) : Boolean {
return if (data.contains(item)) {
data.remove(item)
true
} else {
false
}
}
private fun thr(msg: String) : Nothing {
throw IllegalStateException(msg)
}
}
}