The enqueue() method adds an item to the rear of the queue. This video shows how to use the Python list's built-in insert() method to implement this behavior.
- [Instructor] Let's start coding the minimal functionality…we need for our Queue class.…We'll start with the enqueue method.…Again, as we talked about, we have to pass in as a parameter…the item that we want to add into the queue,…and in the body of our method,…we're going to need to do something with that.…Now we know that we're going to be operating…on our empty list, which is called items,…but instead of appending the item to the end of the list,…like we did for a stack,…we want to now insert the item…into the zeroth index…of the list.…
And we talked about how the reason we do that is…because we want to sort of save…the end of the list for popping…and use the front of the list for inserting.…So I'll save that.…We can go back to the terminal.…I'll start up the Python interpreter here,…and we can try this out.…I'll create my own Queue object first,…and next I will enqueue an item into it.…
So my_q.enqueue(),…and let's add in a string containing the word apple.…If I want to check that it worked,…I just need to look at the items attribute.…
- 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.