Stack Algorithm

The Stack Using Array Algorithm is an efficient data structure that implements the stack concept using an array as its underlying storage mechanism. The stack is a linear data structure that follows the Last-In-First-Out (LIFO) order, where the last element added to the stack is the first one to be removed. It consists of two main operations: pushing (adding) an element onto the stack and popping (removing) the element from the top of the stack. The array-based implementation of this algorithm allows for fast and easy access to the stack's top element, making it a popular choice for various applications such as parsing, expression evaluation, and function call management. In the array-based stack implementation, an integer variable called "top" is used to keep track of the index of the topmost element in the stack. When a new element is pushed onto the stack, the "top" index is incremented, and the element is placed at that index in the array. Conversely, when an element is popped from the stack, the "top" index is decremented, effectively removing the element from the stack. One of the key considerations for this algorithm is the stack's capacity, which is determined by the size of the underlying array. When the stack becomes full, it may require resizing (either by doubling or shrinking) to accommodate additional elements or to conserve memory. This dynamic resizing can be implemented using a dynamic array or by allocating and deallocating memory as needed. Overall, the Stack Using Array Algorithm offers a simple and efficient method for implementing a stack data structure with constant time complexity for both push and pop operations, making it an attractive choice for a wide range of applications.
/**
 * Created by gazollajunior on 03/04/16.
 */


class Stack<T:Comparable<T>>(list:MutableList<T>):Iterator<T> {

    var itCounter: Int = 0

    var items: MutableList<T> = list


    fun isEmpty():Boolean = this.items.isEmpty()

    fun count():Int = this.items.count()

    fun push(element:T) {
        val position = this.count()
        this.items.add(position, element)
    }

    override  fun toString() = this.items.toString()

    fun pop():T? {
        if (this.isEmpty()) {
            return null
        } else {
            val item =  this.items.count() - 1
            return this.items.removeAt(item)
        }
    }

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

// Note Let the default implementation exist

//    override fun toString(): String {
//        val topDivider = "---Stack---\n"
//        val bottomDivider = "\n-----------"
//
//        val stackElements = array.map {
//            "$it"
//        }.reversed().joinToString("\n")
//
//        return topDivider + stackElements + bottomDivider
//
//    }

    override fun hasNext(): Boolean {
        val hasNext = itCounter < count()

        // As soon as condition fails, reset the counter
        if (!hasNext) itCounter = 0

        return hasNext
    }

    override fun next(): T {
        if (hasNext()) {
            val topPos: Int = (count() - 1) - itCounter
            itCounter++
            return this.items[topPos]
        } else {
            throw NoSuchElementException("No such element")
        }
    }

}

fun main(args: Array<String>) {

    var initialValue =  mutableListOf<Int>(10)
    var stack = Stack<Int>(initialValue)
    println(stack)
    stack.push(22)
    println(stack)
    stack.push(55)
    println(stack)
    stack.push(77)
    println(stack)
    stack.pop()
    println(stack)

    for (item in stack) println("Item in stack : " + item)

}

LANGUAGE:

DARK MODE: