Kotlin: Implement Stack using Arrays

1. Introduction

A Stack is a Last In First Out (LIFO) data structure that allows operations at one end, often referred to as the "top". In this guide, we will demonstrate how to implement a stack using arrays in Kotlin.

2. Program Overview

The stack will support the following operations:

1. Push: To add an element to the top of the stack.

2. Pop: To remove and return the top element of the stack.

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

4. Check if the stack is empty.

We will use a fixed-size array to hold the stack elements and a variable to keep track of the top position in the stack.

3. Code Program

class Stack(private val size: Int) {
    private val array = IntArray(size)
    private var top = -1  // This will track the top position. Initialized to -1 because the stack is initially empty.

    // Push operation
    fun push(value: Int) {
        if (top < size - 1) {
            top++
            array[top] = value
        } else {
            println("Stack Overflow!")
        }
    }

    // Pop operation
    fun pop(): Int? {
        return if (top >= 0) {
            val poppedValue = array[top]
            top--
            poppedValue
        } else {
            println("Stack Underflow!")
            null
        }
    }

    // Peek operation
    fun peek(): Int? {
        return if (top >= 0) {
            array[top]
        } else {
            println("Stack is Empty!")
            null
        }
    }

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

fun main() {
    val stack = Stack(5) // Stack of size 5

    stack.push(10)
    stack.push(20)
    stack.push(30)
    stack.push(40)
    stack.push(50)

    println("Top element: ${stack.peek()}")
    println("Popped element: ${stack.pop()}")
    println("Is stack empty? ${stack.isEmpty()}")
}

Output:

Top element: 50
Popped element: 50
Is stack empty? false

4. Step By Step Explanation

1. Class Declaration & Initialization:

- We declare a Stack class that accepts a size (for the fixed-size array).- An array (array) is initialized with the given size.- The top variable is initialized at -1, signifying an empty stack.

2. Push Operation:

- Check if there's space left in the stack.- If so, increment the top and store the value.- If not, print an error message ("Stack Overflow").

3. Pop Operation:

- If the stack isn't empty, return the top element and decrement the top variable.- If the stack is empty, print an error message ("Stack Underflow").

4. Peek Operation:

- Return the top element without popping it.- If the stack is empty, inform the user.

5. isEmpty Function:

- Returns a Boolean indicating if the stack is empty. 

The main function demonstrates how to use the stack. The push operation is executed five times, and then the peek and pop operations are used. The program concludes by checking if the stack is empty using the isEmpty function. 

With this program, one can understand the foundational principles behind a stack and its standard operations using arrays in Kotlin.

Comments