Queues also have size() and is_empty() methods, just like the other data structures in this course. The size() method returns the number of items in the queue and the is_empty() method returns a Boolean value describing whether or not the queue is empty. Learn how to implement each of these methods in your queue class in this video.
- [Narrator] The two remaining methods we have…for the queue class, size and is_empty, are…exactly the same as the size and is_empty methods…that you would use for a stack.…Let's code these, and then we'll…test them out in the terminal.…Now when we want to find "size", what we're talking…about is really the length of the list.…So we can simply return the length of self dot items.…And even if items is empty, it should still return zero.…So we don't need to account for that situation.…
Let's add our docstring, and the summary here is simply…"Returns the size of the queue...…"which is represented by the length of the list."…And the run time would be oh of one,…which is also called constant time because…we're simply returning the length.…Let's move on to the is_empty method.…For the is_empty method, we just want to know…whether or not the queue, or the list…that's representing it, is empty.…The way we can do that is by returning a comparison of…the list we have to an empty list.…
So we would simply say "return self dot items…
- 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.