In the Queue class, why does the `peek()` method need to check the size of the queue?


#1

Question

For this exercise, the peek() method performs a check on the size of the queue before returning a value. Is this check needed?

Answer

Yes, the peek() method must perform some type of check to ensure that the queue is not empty before attempting to return a value. Either the method must check the size of the queue to ensure it is not 0 or it must check that self.head is not set to Null. Attempting to return a value when the queue is empty could result in an error which could terminate the program.