-
Notifications
You must be signed in to change notification settings - Fork 87
/
Deque.kt
55 lines (45 loc) · 1.17 KB
/
Deque.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
package deque
/**
* Implementation of the Deque functionality which provides a double ended queue.
*
* This implementation of Deque is backed by a MutableList
*
* Created by Andy Bowes on 05/05/2016.
*/
class Deque<T>(){
var backingList : MutableList<T> = arrayListOf()
fun addFirst(element:T){
backingList.add(0,element)
}
fun getFirst():T?{
if (backingList.isEmpty()){
return null
}
val value = backingList.first()
removeFirst()
return value
}
fun removeFirst(){
if (backingList.isNotEmpty()) backingList.removeAt(0)
}
fun peekFirst(): T?{
return if (backingList.isNotEmpty()) backingList.first() else null
}
fun addLast(element:T){
backingList.add(element)
}
fun getLast():T?{
if (backingList.isEmpty()){
return null
}
val value = backingList.last()
removeLast()
return value
}
fun removeLast(){
if (backingList.isNotEmpty()) backingList.removeAt(backingList.size - 1)
}
fun peekLast():T?{
return if (backingList.isNotEmpty()) backingList.last() else null
}
}