Queue Using Linked List in Kotlin

Kotlin
class Queue<T> {
    private var head: Node<T>? = null
    private var tail: Node<T>? = null
    private var size = 0

This implementation uses a singly linked list to represent the queue. The Queue class has two private fields: head points to the first node in the list (which is the front of the queue), and tail points to the last node in the list (which is the back of the queue). It also has a private field size that stores the number of nodes in the list.

Kotlin
fun enqueue(data: T) {
    val newNode = Node(data)
    if (isEmpty()) {
        head = newNode
    } else {
        tail?.next = newNode
    }
    tail = newNode
    size++
}

The enqueue function adds a new node with the given data to the back of the queue by creating a new Node instance with the data and setting its next field to null. If the queue is empty, it sets the head field to point to the new node. Otherwise, it sets the next field of the current tail node to point to the new node. Finally, it updates the tail field to point to the new node and increments the size field.

Kotlin
fun dequeue(): T? {
    if (isEmpty()) {
        return null
    }
    val data = head?.data
    head = head?.next
    if (head == null) {
        tail = null
    }
    size--
    return data
}

The dequeue function removes and returns the data of the front node in the queue by checking if the queue is empty, if not it gets the data of the head node, sets the head field to point to the next node in the list, and updates the tail field to null if the queue is now empty. Finally, it decrements the size field and returns the data of the original head node.

Kotlin
fun peek(): T? {
    if (isEmpty()) {
        return null
    }
    return head?.data
}

The peek function returns the data of the front node in the queue without removing it by checking if the queue is empty, if not it returns the data of the head node.

Kotlin
fun isEmpty(): Boolean {
    return size == 0
}

The isEmpty function checks if the queue is empty by returning true if the size of the queue is zero.

Kotlin
fun size(): Int {
    return size
}

The size function returns the number of nodes in the queue by returning the size field.

Kotlin
private class Node<T>(val data: T) {
    var next: Node<T>? = null
}

The Node class is a private inner class that represents a node in the linked list. It has a field data to store the data of the node, and a field next to point to the next node in the list.

Kotlin
class Queue<T> {
    private var head: Node<T>? = null
    private var tail: Node<T>? = null
    private var size = 0

    fun enqueue(data: T) {
        val newNode = Node(data)
        if (isEmpty()) {
            head = newNode
        } else {
            tail?.next = newNode
        }
        tail = newNode
        size++
    }

    fun dequeue(): T? {
        if (isEmpty()) {
            return null
        }
        val data = head?.data
        head = head?.next
        if (head == null) {
            tail = null
        }
        size--
        return data
    }

    fun peek(): T? {
        if (isEmpty()) {
            return null
        }
        return head?.data
    }

    fun isEmpty(): Boolean {
        return size == 0
    }

    fun size(): Int {
        return size
    }

    private class Node<T>(val data: T) {
        var next: Node<T>? = null
    }
}
fun main() {
    val queue = Queue<String>()
    queue.enqueue("apple")
    queue.enqueue("banana")
    queue.enqueue("cherry")
    println("Queue size: ${queue.size()}")
    println("Front item: ${queue.peek()}")
    println("Dequeued item: ${queue.dequeue()}")
    println("Front item: ${queue.peek()}")
    println("Queue size: ${queue.size()}")
}

This code creates a new Queue instance with the element type of String, adds three elements to the queue using the enqueue function, prints the size of the queue using the size function, prints the front element of the queue using the peek function, removes the front element of the queue using the dequeue function, prints the new front element of the queue using the peek function, and prints the size of the queue again using the size function.

Leave a Comment