What is ArrayDeque || Kotlin Deque?

What is ArrayDeque || Kotlin Deque?

Jan 01, 2023

mcode-app.medium.com

https://mcode-app.medium.com/

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 ends (rear and front).

  1. Enqueue → Insertion of a new element to the queue

  2. Dequeue → Removal of the existing elements from the queue

mcode-app.medium.com

Kotlin Deque →

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

https://mcode-app.medium.com/

As shown in the above diagram the “LinkedList” is the implementation of “Deque” and “Queue”

“Queue” is allows you to add elements at the read end or remove elements from the front end. We also have PriorityQueue which is an implementation of AbastractQueue.

To use the Linked List as a “Queue”

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

Sponsor me on GitHub →

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"))
// initialized queue
println(studentQueue) /* [Agni, Bob, Chris, David, Emily] */
// add at rear
studentQueue.add("Felix")
println(studentQueue) /* [Agni, Bob, Chris, David, Emily, Felix] */
// add at front
studentQueue.addFirst("Afra")
println(studentQueue) /* [Afra, Agni, Bob, Chris, David, Emily, Felix] */

// add at rear
studentQueue.addLast("Geek")
println(studentQueue) /* [Afra, Agni, Bob, Chris, David, Emily, Felix, Geek] */
}

Pov. (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"))
// initialized queue
println(studentQueue) /* [Agni, Bob, Chris, David, Emily] */
// remove at end
val removeElement = studentQueue.remove()
println(studentQueue) /* [Bob, Chris, David, Emily] */

val removeLast = studentQueue.removeLast()
println(studentQueue) /* [Bob, Chris, David] */


val removeFirst = studentQueue.removeFirst()
println(studentQueue) /* [Chris, David] */
}

Conclusion

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

Get involved

¿Te gusta esta publicación?

Comprar Mcode un café

Más de Mcode