Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 30 / 68

Stacks and queues (the basics of data structures)

This lesson covers implementing a stack and a queue in Julia, with the aim of understanding how to add and remove data for different purposes.

A stack takes out the item put in most recently (last in, first out); a queue takes out the item put in first (first in, first out). Picture a pile of books against a line of people waiting.

The sample code pushes values with push!(stack, "A") and then takes the most recent with pop!(stack) (the stack) and the earliest with popfirst!(queue) (the queue). The exclamation mark in the names is Julia convention.

A common early stumble is what that ! means. By convention a Julia function ending in "!" modifies its argument in place, and knowing that makes function behaviour much easier to predict.

In professional work, the mechanism of function calls is itself managed with a stack, so this also builds computer science fundamentals.

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

๐Ÿงช This site can't compile or run Julia 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)