Bounded queues require limits on the number of nodes that can be contained, while other queues don’t. To account for this, we will need to make some modifications to our Queue
class so that we can keep track of and limit size where needed.
We’ll be adding two new properties to help us out here:
- A
size
property to keep track of the queue’s current size - A
max_size
property that bounded queues can utilize to limit the total node count
In addition, we will add three new methods:
get_size()
will return the value of thesize
propertyhas_space()
will returnTrue
if the queue has space for another nodeis_empty()
will return true if thesize
is 0
Instructions
Add a new parameter max_size
to your __init__()
method that has a default value of None
. Inside the method:
- create a
max_size
instance variable assigned tomax_size
- create another instance variable
size
and set it equal to 0
Inside Queue
define a new method get_size()
that returns the size
instance property.
Below get_size()
, define another method has_space()
. Inside the method, check if a self.max_size
has been set.
- If there’s no
max_size
set, then we will always have space in the queue, so we can returnTrue
- If so, return
True
if themax_size
is greater thanself.get_size()
Define another method is_empty
for Queue
. The method should return True
if the queue is empty (if the size of the queue is 0).
Now we’ll make sure we aren’t attempting to peek()
on an empty queue. After all, a deli server can’t get an order from a line with no customers!
At the top of your peek()
method body, use is_empty()
to see if the queue is empty.
- if so, the method should just print “Nothing to see here!”
- if not,
peek()
will perform the same as it did before