Skip to content

Kotlin Deque

  • by

1. Overview

In this article, we will learn the Kotlin deque interface and its implementations with examples. A deque is a double-ended queue that allows you to add or remove elements from both the ends (rear and front).

  • Enqueue – Insertion of a new element to the queue
  • Dequeue – Removal of the existing elements from the queue
Kotlin deque
Kotlin deque

2. Kotlin deque

Kotlin deque is an interface so you need to use its concrete implementation. The LinkedList is the deque implementation available in Kotlin.

Kotlin Deque Hierary

As shown in the above diagram, the LinkedList is the implementation of Deque and Queue. A queue allows to add elements at the read end or remove elements from the front end. We also have PriorityQueue which is an implementation of AbstractQueue.

To use the Linked List as Queue, refer to this article.

The LinkedList deque stores the elements in a standard linked list data structure and thus makes the insertion or removal faster.

3. Linked List deque

You can create a deque object from the LinkedList implementation. For example, the below studentQueue is a Deque object that supports adding or removing elements at both ends.

The functions add and addLast adds the elements at the end or rear, whereas addFirst function adds the element at the starting or front.

import java.util.Deque
import java.util.LinkedList

fun main() {
  val studentQueue: Deque<String> = LinkedList<String>(mutableListOf("Agni", "Bob", "Chris", "David", "Emily"))

  println(studentQueue) 

  studentQueue.add("Felix")
  println(studentQueue) 

  studentQueue.addFirst("Afra")
  println(studentQueue) 
    

  studentQueue.addLast("Geek")
  println(studentQueue) 
}

3.1. Linked List deque remove and get elements

The poll, remove and removeFirst functions remove the element from the front end of the deque. However, the removeLast function removes the element from the rear end of the deque.

The poll doesn’t throw an exception if the element is not available and simply returns null whereas remove throws NoSuchElementException.

import java.util.Deque
import java.util.LinkedList

fun main() {
  val studentQueue: Deque<String> = LinkedList<String>(mutableListOf("Agni", "Bob", "Chris", "David", "Emily"))

  println(studentQueue) 

  val removeElement = studentQueue.remove()
  println(studentQueue) 
  
  val removeLast = studentQueue.removeLast()
  println(studentQueue) 
  
  
  val removeFirst = studentQueue.removeFirst()
  println(studentQueue) 
}

3.1. Linked List Kotlin deque get element without removal

You can use either element, or peek function to get the element from the queue. Unlike the poll function, these functions don’t remove the element from the queue.

The primary difference between both these functions is that element throws a NoSuchElementException if there are no elements available in the Queue to fetch. But, the peek function returns null and doesn’t throw any error.

import java.util.Deque
import java.util.LinkedList

fun main() {
  val studentQueue: Deque<String> = LinkedList<String>(mutableListOf("Agni"))

  val element = studentQueue.element()
  println(element + " : " + studentQueue) 
  
  studentQueue.remove() 

  val noneAvailable = studentQueue.peek() 
  println(noneAvailable + " : " +studentQueue) 
  

  studentQueue.element() 
}

4. Conclusion

To sum up, we have seen the LinkedList deque, implementation of the Deque.

Leave a Reply

Your email address will not be published. Required fields are marked *