Stacks are partially defined by the operations you can make on them, such as pushing and popping items and peeking at the topmost item. Discover the method names used for these operations as you stub out the stack class using Python.
- [Instructor] Now that we've discussed…the stack abstract data type,…we know what we want the stack to do,…and we can start to stub out a stack class…and its methods.…First, we need to define the class itself.…So let's call it Stack.…We talked about how we need to use…a Python list behind the scenes…to represent the stack.…So we can create an init method to do that.…And we'll create a variable called self.items,…and initialize that to our empty list.…
The other basic functionality we need…is we need to be able to add an item to the stack.…The word we use for that is push.…And we'll also need to pass into push…the item that we want to add to the stack.…Since we're just stemming things out now,…we'll use the pass keyword to remind…ourselves to come back to that later.…Just like we added, we also need to be able…to remove an item from the stack…and we use the word pop for that.…Now, because the list's built in pop method…always returns the last item of the list anyway,…we don't need to specify an index…or an item that we want to remove.…
- Abstract types and data structures
- Stacks as a linear abstract data type
- Creating the Stack class and its methods
- Adding items to the top and bottom of a stack
- Creating the Queue class and its methods
- Manipulating items in a queue
- Creating the Dequeue class and its methods
- Adding and removing items from a dequeue
Skill Level Advanced
Learning the Python 3 Standard Librarywith Kathryn Hodge2h 9m Intermediate
What you should know1m 26s
1. Abstract Data Types and Data Structures
2. The Stack
3. The Queue
4. The Deque
Next steps1m 43s
- 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.