Creating Queues With .append()
00:22 Or maybe like standing in line. You get into the line at the end and eventually work your way up to the front. It is a First-In/First-Out structure. The first thing that gets in line, or into the queue, will be the first thing removed at the other end.
00:39 There are two main operations. Enqueue adds something to the end of the queue and dequeue removes the item at the front of the queue. It also returns that item to be used by the calling agent which asked for it.
This always pops off the element at the front of the list, which is being viewed as the front of this queue. So, here’s the implementation. This file is called
list_queue.py because it’s a queue data structure implemented using a list. It begins with
Here again is that
except block used to react to an attempt to dequeue from an empty queue. Dunder methods
.__repr__() have been implemented here similar to how they were for the stack. And you can see this work as well.
First was the
1. Notice how the first thing we put into the queue was the first thing taken out. This is one of the things that makes a queue different from a stack. First in, first out. So the
1 comes out first
Become a Member to join the conversation.