I am implementing a generic Priority Queue as part of a homework project. I am wondering what to return when the PriorityQueue is empty. I could not return null.
What is the best method to handle this case? What is the best design choice when implementing such datastructures?
class PQueue<T> : IPQueue<T>
{
T[] items;
//..
public T RemoveMax()
{
if(heapSize < 1) //Heap Empty
return default(T);
T max = items[0];
//..
return max;
}
}
I would look for guidance in the framework classes here, i.e.
Queue<T>– which throws anInvalidOperationExceptionif you try and dequeue an item from an empty queue. This only makes sense though if you give consumers access to the number of items in the queue or at least if the queue is empty or not, i.e.: