Kotlin: Implement Queue using Arrays

1. Introduction

A Queue is a First In First Out (FIFO) data structure. It performs operations from two ends, often termed the "front" and "rear". In this guide, we'll showcase how to implement a queue using arrays in Kotlin.

2. Program Overview

The queue will support the following core operations:

1. Enqueue: To add an element to the rear of the queue.

2. Dequeue: To remove and return the front element of the queue.

3. Peek: To view the front element without removing it.

4. Check if the queue is empty.

We will utilize a fixed-size array to hold the queue elements and two variables to monitor the front and rear positions in the queue.

3. Code Program

class Queue(private val size: Int) {
    private val array = IntArray(size)
    private var front = -1  // Initially, front is -1 as the queue is empty.
    private var rear = -1   // Similarly, rear is -1 indicating an empty queue.

    // Enqueue operation
    fun enqueue(value: Int) {
        if (rear < size - 1) {
            if (front == -1) front = 0   // If first element, make front point to 0.
            rear++
            array[rear] = value
        } else {
            println("Queue Overflow!")
        }
    }

    // Dequeue operation
    fun dequeue(): Int? {
        return if (front > -1) {
            val dequeuedValue = array[front]
            front++
            if (front > rear) {   // If all elements are dequeued, reset front and rear to -1.
                front = -1
                rear = -1
            }
            dequeuedValue
        } else {
            println("Queue Underflow!")
            null
        }
    }

    // Peek operation
    fun peek(): Int? {
        return if (front != -1) {
            array[front]
        } else {
            println("Queue is Empty!")
            null
        }
    }

    // Check if the queue is empty
    fun isEmpty(): Boolean = front == -1
}

fun main() {
    val queue = Queue(5) // Queue of size 5

    queue.enqueue(10)
    queue.enqueue(20)
    queue.enqueue(30)
    queue.enqueue(40)
    queue.enqueue(50)

    println("Front element: ${queue.peek()}")
    println("Dequeued element: ${queue.dequeue()}")
    println("Is queue empty? ${queue.isEmpty()}")
}

Output:

Front element: 10
Dequeued element: 10
Is queue empty? false

4. Step By Step Explanation

1. Class Declaration & Initialization:

  • We introduce a Queue class that accepts a size (for the fixed-size array).
  • An array (array) is initialized using the provided size.
  • The front and rear variables start at -1, signifying an empty queue.

2. Enqueue Operation:

  • Check if there's space remaining in the queue.
  • If so, update the rear and store the value. Adjust the front if it's the first item.
  • If not, produce an error message ("Queue Overflow").

3. Dequeue Operation:

  • If the queue isn't empty, return the front element and adjust the front.
  • If after dequeuing, the queue is empty, reset front and rear to -1.
  • If the queue is empty from the start, display an error ("Queue Underflow").

4. Peek Operation:

  • Return the front element without dequeuing.
  • If the queue is vacant, notify the user.

5. isEmpty Function:

  • Returns a Boolean value denoting if the queue is empty.

The main function exemplifies how to utilize the queue. The enqueue operation is performed five times, followed by the peek and dequeue operations. The demo concludes by inspecting if the queue is empty using the isEmpty function.

Through this program, learners can grasp the fundamental concepts behind a queue and its standard operations using arrays in Kotlin.

Comments