© Elshad Karimov 2020
E. KarimovData Structures and Algorithms in Swifthttps://doi.org/10.1007/978-1-4842-5769-2_4

4. Stacks

Elshad Karimov1 
(1)
New York, New York, USA
 
A stack is a last in first out (LIFO) data structure. The structure of a stack can be imagined as a pile of objects stacked vertically (Figure 4-1). When extracting these objects, the last added to the stack is the first one removed. Stacks are similar to arrays but with limited control.
../images/490882_1_En_4_Chapter/490882_1_En_4_Fig1_HTML.png
Figure 4-1

Stack structure

There are three methods that stacks implement. By using push() method, a new element can be added to the top of the stack, pop() method to remove an element from the top, and ...

Get Data Structures and Algorithms in Swift: Implement Stacks, Queues, Dictionaries, and Lists in Your Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.