Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 30 / 68

Stacks and queues (the basics of data structures)

This lesson covers implementing a stack and a queue in Objective-C, 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 uses an NSMutableArray: it pushes with addObject:, then takes the most recent value with lastObject and removeLastObject (the stack) and the earliest with firstObject (the queue).

A common early stumble is that Objective-C has no dedicated type for either — you build the behaviour yourself out of NSMutableArray, staying conscious of which end you take from.

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 Objective-C 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)