Stack in Kotlin And how it works

A stack is a fundamental data structure that stores and retrieves elements in a specific order. It is also known as a Last-In-First-Out (LIFO) data structure, which means that the element added last to the stack is the first one to be removed.

The main operations of a stack are:

  1. Push: Adds an element to the top of the stack
  2. Pop: Removes the top element from the stack
  3. Peek: Returns the top element without removing it
  4. IsEmpty: Checks whether the stack is empty

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

Steps to create Stack in Kotlin are :-

Step 1 : Create the class for stack and also create mutable list inside it.

Kotlin
class Stack<T> {
    private val items = mutableListOf<T>()

Step 2 : Create the Push method inside the class Which will add the element in list.

Kotlin
  fun push(item: T) {
        items.add(item)
    }

Step 3 : Create Pop method inside Class and check if stack is empty return NULL else remove the item from top of stack and reduce the size by 1

Kotlin
 fun pop(): T? {
        if (isEmpty()) {
            return null
        }
        return items.removeAt(items.size - 1)
    }

Step 4 : Create peek method and return the value which is on top of stack

Kotlin
fun peek(): T? {
        if (isEmpty()) {
            return null
        }
        return items[items.size - 1]
    }

Step 5 : Create a method to check is stack is empty or not and return true or flase.

Kotlin
 fun isEmpty(): Boolean {
        return items.isEmpty()
    }

Step 6 : Create the size method which will return the size of stack.

Kotlin
    fun size(): Int {
        return items.size
    }

Step 7 : Call very method of stack class from main function.

Kotlin
fun main() {
    val stack = Stack<Int>()
    stack.push(1)
    stack.push(2)
    stack.push(3)
    println(stack.peek())  // Output: 3
    println(stack.pop())  // Output: 3
    println(stack.pop())  // Output: 2
    println(stack.isEmpty())  // Output: false
   
}

The implement of stack is done using Kotlin and the source code is below. you can run and edit by your own.

Kotlin
class Stack<T> {
    private val items = mutableListOf<T>()

    fun push(item: T) {
        items.add(item)
    }

    fun pop(): T? {
        if (isEmpty()) {
            return null
        }
        return items.removeAt(items.size - 1)
    }

    fun peek(): T? {
        if (isEmpty()) {
            return null
        }
        return items[items.size - 1]
    }

    fun isEmpty(): Boolean {
        return items.isEmpty()
    }

    fun size(): Int {
        return items.size
    }
}

fun main() {
    val stack = Stack<Int>()
    stack.push(1)
    stack.push(2)
    stack.push(3)
    println(stack.peek())  // Output: 3
    println(stack.pop())  // Output: 3
    println(stack.pop())  // Output: 2
    println(stack.isEmpty())  // Output: false
   
}

Leave a Comment