Section 3.17 Implementing a Deque in Kotlin
As we have done in previous sections, we will create a new class for the implementation of the abstract data type deque. Again, the Kotlin list will provide a very nice set of capabilities upon which to build the details of the deque. Our implementation (Listing 3.17.1) will assume that the tail of the deque is at position 0 in the list.
class ListDeque<T> : DequeADT<T> {
/*
* The tail of the deque is at the beginning
* of the ArrayList; the head is the last item
*/
private val items = mutableListOf<T>()
/*
* Returns true if there are no items in the deque;
* false otherwise.
*/
override fun isEmpty(): Boolean {
return items.isEmpty()
}
/*
* Add an item to the head of the deque
*/
override fun addHead(item: T) {
items.add(item)
}
/*
* Add an item to the tail of the deque
*/
override fun addTail(item: T) {
items.add(0, item)
}
/*
* Remove the item at the head of the deque and return it.
* If the deque is empty, throws an exception.
*/
override fun removeHead(): T {
if (this.isEmpty()) {
throw NoSuchElementException("Deque is empty.")
}
return this.items.removeAt(this.size() - 1)
}
/*
* Remove the item at the tail of the deque and return it.
* If the deque is empty, throws an exception.
*/
override fun removeTail(): T {
if (this.isEmpty()) {
throw NoSuchElementException("Deque is empty.")
}
return items.removeFirst()
}
/*
* Return the item at the head of the deque, but do not remove it.
* If the deque is empty, throws an exception.
*/
override fun peekHead(): T {
if (this.isEmpty()) {
throw NoSuchElementException("Deque is empty.")
}
return items[items.count() - 1]
}
/*
* Return the item at the tail of the deque, but do not remove it.
* If the deque is empty, throws an exception.
*/
override fun peekTail(): T {
if (isEmpty()) {
throw NoSuchElementException("Deque is empty.")
}
return items[0]
}
/*
* Returns the number of items in the deque.
*/
override fun size(): Int {
return items.count()
}
/*
* Convert to string as an array from tail to head
*/
override fun toString(): String {
return "tail ${items.toString()} head"
}
}
And here is its output:
d.isEmpty() | true | tail [] head d.addTail(4) | | tail [4] head d.addTail(505) | | tail [505, 4] head d.addHead(1066) | | tail [505, 4, 1066] head d.addHead(4711) | | tail [505, 4, 1066, 4711] head d.size | 4 | tail [505, 4, 1066, 4711] head d.isEmpty() | false | tail [505, 4, 1066, 4711] head d.addTail(217) | | tail [217, 505, 4, 1066, 4711] head d.removeTail() | 217 | tail [505, 4, 1066, 4711] head d.removeHead() | 4711 | tail [505, 4, 1066] head
You can see many similarities to Kotlin code already described for stacks and queues. You are also likely to observe that in this implementation adding and removing items from the head is \(O(1)\) whereas adding and removing from the tail is \(O(n)\text{.}\) This is to be expected, given the common operations that appear for adding and removing items. Again, the important thing is to be certain that we know where the head and tail are assigned in the implementation.
You have attempted of activities on this page.

