A Common-Sense Guide to Data Structures and Algorithms: Level Up Your Core Programming Skills by Jay Wengrow

A Common-Sense Guide to Data Structures and Algorithms: Level Up Your Core Programming Skills by Jay Wengrow

Author:Jay Wengrow [Wengrow, Jay]
Language: eng
Format: azw3, epub, pdf
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Published: 2017-08-03T04:00:00+00:00


Stacks

A stack stores data in the same way that arrays do—it’s simply a list of elements. The one catch is that stacks have the following three constraints:

Data can only be inserted at the end of a stack.

Data can only be read from the end of a stack.

Data can only be removed from the end of a stack.

You can think of a stack as an actual stack of dishes: you can’t look at the face of any dish other than the one at the top. Similarly, you can’t add any dish except to the top of the stack, nor can you remove any dish besides the one at the top. (At least, you shouldn’t.) In fact, most computer science literature refers to the end of the stack as its top, and the beginning of the stack as its bottom.

While these restrictions seem—well—restrictive, we’ll see shortly how they are to our benefit.

To see a stack in action, let’s start with an empty stack.

Inserting a new value into a stack is also called pushing onto the stack. Think of it as adding a dish onto the top of the dish stack.

Let’s push a 5 onto the stack:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.