As said in my last post, I found the idea of a priority queue to be intriguing, and decided to do some more research in the matter. I was actually surprised to find that this is an important part of queues in c++, and not just something Fardad decided to include for fun (not that he would do that of course! everything is always for a reason when it comes to his teaching style.)
There’s even a page explaining the concept here:
http://www.cplusplus.com/reference/queue/priority_queue/
Here is one nice example of a priority queue that someone wrote: http://www.codingfriends.com/index.php/2010/06/21/priority-queues-part-1/
I find it funny that they mention it being similar to an ER queue in a hospital.
Here’s a small chunk of their code (disclaimer: not written by me! see link to codingfriends for more)
// find the highest chunk of space where newElem could be placed into the best chunk
PQueue::chunkSpace* PQueue::findHighestChunk(chunkSpace *&space, int newElem)
{
if (space == NULL) return NULL;
// found the previous chunk to insert into
if (space->values[space->lastBlock-1] > newElem && space->lastBlock > 0)
return space;
else
{
chunkSpace *retPlace = findHighestChunk(space->nextBlock, newElem);
if (retPlace == NULL)
return space;
else
return retPlace;
}
|