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:
sizeproperty to keep track of the queue’s current size
max_sizeproperty 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 the
Trueif the queue has space for another node
is_empty()will return true if the
Add a new parameter
max_size to your
__init__() method that has a default value of
None. Inside the method:
- create a
max_sizeinstance variable assigned to
- create another instance variable
sizeand set it equal to 0
Queue define a new method
get_size() that returns the
size instance property.
get_size(), define another method
has_space(). Inside the method, check if a
self.max_size has been set.
- If there’s no
max_sizeset, then we will always have space in the queue, so we can return
- If so, return
max_sizeis greater than
Define another method
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