operator << for printing a priority queue?
It seems like the operator would need access to the private data inside of the priority queue object.
<< operator in our assignment is declared with a special keyword called friend that makes it so that this operator is able to directly access the private data inside the priority queue if needed.
new keyword.
clear method?
Don't they do the same thing, deleting all elements from the queue?
clear method is called explicitly when a client wants to wipe the elements and then start over and use the same list to store something else.
A destructor is called implicitly by C++ when an object is being thrown away forever; it won't ever be used to store anything else after that.
The implementations might be similar, but their external purpose is different.
PQNode and PQNode* ? Which one should I use?
PQNode ; you want only PQNode* .
The former is an object, the latter is a pointer to an object.
You always want pointers to PQNode objects in this assignment because objects created with new live longer; they are not cleaned up when the current function exits.
PQNode?
new keyword. Like this:
PQNode* node = new PQNode();
Or, you can pass any of the value, priority, and next values on construction:
PQNode* node = new PQNode(value, priority, next);
You *must* declare all your nodes as pointers; do not declare them as regular objects like the following code, because it will break when the list node goes out of scope:
// do not do this! PQNode node; // bad bad bad node.data = 42; // bad bad bad node.next = nullptr; // bad bad bad ...
>= =< > < == !=
current->next->data before checking whether current or current->next are NULL.