Queues
Queues are very similar to stacks, with the exception of how elements are taken out of the datastructure.
FIFO (First-In-First-Out) stands for elements that are added to a data structure first, are the first to be returned.
A queue is a datastructure to hold elements. It follows a FIFO principle.
An example of this might be a line for ice cream. The first person that gets in line is the first in the queue and will be the first served. The longer a line is, the longer it takes for the person at the end to be served.
Our queue
looks very similar to our stack
from the previous lesson. Since it's familiar, we include the entire
class definition. We make a class with the same sort if init, pretty
string, and method override for getting an item. As well as the same
methods push
, pop
,
peek
, is_empty
. The only difference is
the index used for when we need something from the top of the queue.
Practice Question
Please create a queue that has a fixed size of five. This means that
once it gets to some maximum size, if a new element is added to it, the
first element is removed to make room for it. For example
a FixedSizeQueue
with a maximum size of five has seven
elements pushed to it, the values one through seven. When it's printed,
it should print out [3,4,5,6,7]
.