Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 30 / 68

Stacks and Queues (Basic Data Structures)

In this lesson you will learn how to build stacks and queues in Kotlin, and understand which way of adding and removing data suits which job. This is for anyone searching for "Kotlin stack queue how to use".

A stack is "last in, first out" and a queue is "first in, first out". The difference between a pile of books and a queue of people is a helpful picture.

The sample code uses the double-ended ArrayDeque<String>: pushing values with addLast() and taking the last one back with removeLast() gives a stack, while removeFirst() takes the first one and gives a queue.

A common stumbling block for beginners is that the only difference is where you take items from (removeLast or removeFirst). ArrayDeque is flexible enough to play both roles from a single structure.

In real-world development, the stack underpins the mechanism of function calls itself, making it an important structure that also builds your computer science foundations.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)