Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use stack in Kotlin?

Tags:

stack

kotlin

People also ask

Does Kotlin have stack?

Notice that the Kotlin compiler can type infer the element type from the list so you can use Stack instead of the more verbose Stack<String> .

Can I learn DSA in Kotlin?

Learn Data Structures & Algorithms in Kotlin!In this book, you'll learn how to implement key data structures in Kotlin, and how to use them to solve a robust set of algorithms. This book is for intermediate Kotlin or Android developers who already know the basics of the language and want to improve their knowledge.

What is stack and where can it be used?

A Stack can be used for evaluating expressions consisting of operands and operators. Stacks can be used for Backtracking, i.e., to check parenthesis matching in an expression. It can also be used to convert one form of expression to another form. It can be used for systematic Memory Management.


import java.util.ArrayDeque

var stack = ArrayDeque<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
println(stack)           // --> [4, 3, 2, 1]
println(stack.isEmpty()) // --> false

println(stack.peek())    // --> 4
println(stack)           // --> [4, 3, 2, 1]

println(stack.pop())     // --> 4
println(stack)           // --> [3, 2, 1]

stack.push(9)
println(stack)           // --> [9, 3, 2, 1]

You can use following:

/**
 * Stack as type alias of Mutable List
 */
typealias Stack<T> = MutableList<T>

/**
 * Pushes item to [Stack]
 * @param item Item to be pushed
 */
inline fun <T> Stack<T>.push(item: T) = add(item)

/**
 * Pops (removes and return) last item from [Stack]
 * @return item Last item if [Stack] is not empty, null otherwise
 */
fun <T> Stack<T>.pop(): T? = if (isNotEmpty()) removeAt(lastIndex) else null

/**
 * Peeks (return) last item from [Stack]
 * @return item Last item if [Stack] is not empty, null otherwise
 */
fun <T> Stack<T>.peek(): T? = if (isNotEmpty()) this[lastIndex] else null

This is done in the same way as you would in Java, but with Kotlin syntax - notably different are the val keyword and lack of new keyword. For example:

import java.util.Stack
...
val someList = ArrayList()
...
val stack = Stack()
stack.addAll(someList)

This is a few years old but I suspect there's room for a different approach. If you want to use a stack structure in Kotlin you certainly don't need to resort to Java. You could easily just create a new class with an internal Kotlin list and stack-like public functions, or use Kotlin's extension methods to give an existing Kotlin collection "stack-like" functionality, for example:

fun <T> MutableList<T>.push(item: T) = this.add(this.count(), item)
fun <T> MutableList<T>.pop(): T? = if(this.count() > 0) this.removeAt(this.count() - 1) else null
fun <T> MutableList<T>.peek(): T? = if(this.count() > 0) this[this.count() - 1] else null
fun <T> MutableList<T>.hasMore() = this.count() > 0 

Then, optionally, you could use a typealias to make it more obvious what you're trying to do when using those functions:

typealias Stack = MutableList<MyClass>

Then create one and use it:

val myStack: Stack = mutableListOf()
myStack.push(MyClass())
myStack.pop()

etc