What is Queue and how it works ?

I assume you are referring to a data structure called a “queue”. A queue is a linear data structure that follows a First-In-First-Out (FIFO) order, which means that the element added first to the queue is the first one to be removed.

The main operations of a queue are:

  1. Enqueue: Adds an element to the back of the queue
  2. Dequeue: Removes the front element from the queue
  3. Peek/Front: Returns the front element without removing it
  4. IsEmpty: Checks whether the queue is empty

A queue can be implemented using an array or a linked list. When implemented with an array, the queue has a fixed size, and enqueuing or dequeuing an element requires moving all the other elements to maintain the order. When implemented with a linked list, the size of the queue can grow or shrink dynamically, and enqueuing or dequeuing an element only requires changing the pointers of the linked list.

Steps to create a Queue in Kotlin:

Step 1 : Create a class queue and define front, rear and list.

Kotlin
class Queue {
    private val items = mutableListOf<Int>()
    private var front = 0
    private var rear = -1

Step 2 : Create the method to enqueue . which will insert the element in queue

Kotlin
fun enqueue(item: Int) {
        items.add(item)
        rear++
    }

Step 3 : Create a method dequeue inside the class. which will remove the element from queue

Kotlin
   fun dequeue(): Int? {
        if (isEmpty()) {
            return null
        }
        val item = items[front]
        front++
        if (front > rear) {
            front = 0
            rear = -1
        }
        return item
    }

Step 4 : Create a function peek which will return the first value of queue.

Kotlin
  fun peek(): Int? {
        if (isEmpty()) {
            return null
        }
        return items[front]
    }

Step 5 : Create a function for is queue empty and size.

Kotlin
    fun isEmpty(): Boolean {
        return front > rear
    }

    fun size(): Int {
        return rear - front + 1
    }

Step 6 : Create a main function and call the methods which are required.

The program of queue in Kotlin is completed and the complete code is below. you can run and edit it.

Kotlin
class Queue {
    private val items = mutableListOf<Int>()
    private var front = 0
    private var rear = -1

    fun enqueue(item: Int) {
        items.add(item)
        rear++
    }

    fun dequeue(): Int? {
        if (isEmpty()) {
            return null
        }
        val item = items[front]
        front++
        if (front > rear) {
            front = 0
            rear = -1
        }
        return item
    }

    fun peek(): Int? {
        if (isEmpty()) {
            return null
        }
        return items[front]
    }

    fun isEmpty(): Boolean {
        return front > rear
    }

    fun size(): Int {
        return rear - front + 1
    }
}
fun main() {
    val queue = Queue()
	queue.enqueue(1)
	queue.enqueue(2)
	queue.enqueue(3)
	println(queue.peek())  // Output: 1
	println(queue.dequeue())  // Output: 1
	println(queue.dequeue())  // Output: 2
	println(queue.isEmpty())  // Output: false

   
}

Leave a Comment