Welcome to our exploration of queues and deques using Kotlin. These data structures frequently appear in everyday programming, managing everything from system processes to printer queues. In this lesson, our goal is to understand and implement queues
and deques
in Kotlin using MutableList
. Let's dive in!
A queue, similar to waiting in line at a store, operates on the "First In, First Out" or FIFO principle. Kotlin's MutableList
can be used to implement queues. We can add items to the end of the list and remove items from the start, making use of add()
and removeFirstOrNull()
.
Kotlin1// Create a queue and add items 2val queue: MutableList<String> = mutableListOf() 3queue.add("Apple") 4queue.add("Banana") 5queue.add("Cherry") 6 7// Remove an item 8println(queue.removeFirstOrNull()) // Expects "Apple"
The dequeued item, "Apple"
, was the first item we inserted, demonstrating the FIFO principle of queues.
Before trying to remove items from our queue, let's ensure it is not empty. This precaution will prevent runtime errors when attempting to dequeue from an empty queue.
Kotlin1// Create a queue and enqueue items 2val queue: MutableList<String> = mutableListOf() 3queue.add("Item 1") 4queue.add("Item 2") 5 6// Check if the queue is non-empty, then dequeue an item 7if (queue.isNotEmpty()) { 8 println(queue.removeFirstOrNull()) // Expects "Item 1" 9}
A deque, or "double-ended queue," allows the addition and removal of items from both ends. In Kotlin, MutableList
can also be used to implement deques. We can add items to both ends of our deque using add()
for the right end and add(0, item)
for the left. Similarly, we can remove elements from the left and right ends using removeFirstOrNull()
and removeLastOrNull()
.
Kotlin1// Create a deque and add items 2val deque: MutableList<String> = mutableListOf() 3deque.add("Middle") 4deque.add("Right end") 5deque.add(0, "Left end") 6 7// Remove an item from the right 8println(deque.removeLastOrNull()) // Expects "Right end" 9 10// Remove an item from the left 11println(deque.removeFirstOrNull()) // Expects "Left end"
Kotlin’s MutableList
provides flexibility, but it does not have a built-in rotation method. We can implement rotation manually using a loop.
Kotlin1// Create a deque 2val deque: MutableList<String> = mutableListOf() 3deque.add("Apple") 4deque.add("Banana") 5deque.add("Cherry") 6 7// Rotate the deque to the right by 1 place 8for (i in 0 until 1) { 9 deque.add(0, deque.removeLastOrNull()!!) 10} 11 12println(deque) // Expects [Cherry, Apple, Banana]
Here, the manual rotation loop shifts all items to the right by one position.
Congratulations on finishing this detailed study of queues and deques in Kotlin! You've learned their operating principles and how to construct and manipulate them effectively using MutableList
in the Kotlin programming language. As we proceed, we will continue to explore additional data structures like these. This journey opens up a wealth of opportunities for expressing your ideas and solving complex problems. Are you ready for forthcoming practice exercises to consolidate this new knowledge? Let's continue our exploration!