Deque Implementation in Kotlin

1. Introduction

In this tutorial, we will learn how to implement a simple Deque data structure using Kotlin programming language.

A Deque, or double-ended queue, is a data structure that allows you to add or remove elements from both its start and its end. This hybrid linear structure provides the capabilities of both stacks and queues. Common operations associated with a deque include addFront, addRear, removeFront, removeRear, peekFront, and peekRear.

2. Implementation Steps

1. Define a class named Deque.

2. Initialize an empty list to store the deque elements.

3. Implement the addFront method to add elements to the start of the deque.

4. Implement the addRear method to add elements to the end of the deque.

5. Implement the removeFront method to remove the first element.

6. Implement the removeRear method to remove the last element.

7. Implement the peekFront method to view the first element without removing it.

8. Implement the peekRear method to view the last element without removing it.

9. Implement an isEmpty method to check if the deque is empty.

3. Implementation in Kotlin Programming

class Deque<T> {
    private val elements: MutableList<T> = mutableListOf()
    
    // 3. Implement the `addFront` method
    fun addFront(item: T) {
        elements.add(0, item)
    }
    
    // 4. Implement the `addRear` method
    fun addRear(item: T) {
        elements.add(item)
    }
    
    // 5. Implement the `removeFront` method
    fun removeFront(): T? {
        if (isEmpty()) {
            return null
        }
        return elements.removeAt(0)
    }
    
    // 6. Implement the `removeRear` method
    fun removeRear(): T? {
        if (isEmpty()) {
            return null
        }
        return elements.removeAt(elements.size - 1)
    }
    
    // 7. Implement the `peekFront` method
    fun peekFront(): T? {
        return elements.firstOrNull()
    }
    
    // 8. Implement the `peekRear` method
    fun peekRear(): T? {
        return elements.lastOrNull()
    }
    
    // 9. Implement the `isEmpty` method
    fun isEmpty() = elements.isEmpty()
}

// Client code to demonstrate the deque functionality
val deque = Deque<Int>()
deque.addRear(1)
deque.addRear(2)
deque.addFront(0)
println(deque.peekFront())  // Expected Output: 0
println(deque.peekRear())   // Expected Output: 2
println(deque.removeFront())  // Expected Output: 0
println(deque.removeRear())  // Expected Output: 2

Output:

0
2
0
2

Explanation:

1. A Deque class is defined with type parameter T to make it generic.

2. Inside the class, a mutable list named elements is initialized. This list holds the deque elements.

3. The addFront method takes an item of type T and adds it to the start of the elements list.

4. The addRear method takes an item and adds it to the end of the elements list.

5. The removeFront method removes and returns the first item from the elements list. If the deque is empty, it returns null.

6. The removeRear method removes and returns the last item from the elements list. If the deque is empty, it returns null.

7. The peekFront method returns the first item from the elements list without removing it. If the deque is empty, it returns null.

8. The peekRear method returns the last item from the elements list without removing it. If the deque is empty, it returns null.

9. The isEmpty method checks if the elements list is empty and returns a boolean value.

Comments