Learn the concept of a stack so that you know when to use a stack in your programs. Discover the different operations we can perform on a stack so you know how to use a stack in your code.
- Stacks are another container type we can use to store our data. A stack is an ordered series of objects just like a list, but its intended use is slightly different. We push objectives onto the stack and pop objects off of it. Think of a stack of pancakes. Each pancake is stacked on top of the other. To take a pancake off the stack, we remove from the top because it's difficult to remove from the bottom. To add a pancake to the stack, we place it on top of the stack because it's difficult to add to the bottom.
Naturally, if you wanted to add or remove from the bottom of the stack, you would have to lift the entire stack in order to add that item. This is why for stacks we add and remove from the top. Stacks follow a last in, first out, or LIFO policy. We say LIFO because the last item on the stack will be the first item removed from the stack. Furthermore, the first item pushed on the stack will be the last item popped off of the stack. When adding an item to the stack, we use the terminology push because we're pushing an item onto the stack or adding it.
When removing an item, we use the word pop because we're popping an item off the stack or removing it. We can push as many items as we want onto the stack, but in order to retrieve a specific item further down on the stack we must pop off each item to get to it. This makes stack especially useful for keeping track of state or the order of when things have occurred.
- Data types: Booleans, numbers, strings, and more
- Multidimensional arrays
- Jagged arrays
- Search and sort arrays
- Linked lists
- Stacks and queues
- Hash functions and hash tables
- Trees and graphs
Skill Level Intermediate
Understand data structures1m 25s
1. Introduction to Data Structures
4. Stacks and Queues
5. Hash-Based Data Structures
6. Trees and Graphs
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.