Here are some ideas. On a call to remove, themax can be set to NULL if the container is empty. On a call to insert(theobject), themax should be set to point to theobject if the value of themax's object is less than the value of theobject's object. Actually it is unnecessary to modify remove - just add a test for an empty container in insert - if the container is empty then certainly theobject will be the new themax.
void *themax;
void *findmax() {
....
}
Now we need to fill in the ..... We would like to just say
return themax;
but we are required to return a -1 if the container is empty. So we
need to add a test like this one:
if (themax == NULL) ...
But this is a little awkward because it requires changing the remove
method. So we might want to change this to
if (empty()) ...
So what is ...? Well, the output needs to be of type void*
but the value we need to output is -1. So we need to cast the -1 as a
void* like this:
return (void*)-1;
The only problem with this is that hw2a.cc as provided is not compatible
because it does this: *(int*)lst->findmax(). So we use
return (void*)new int(-1); instead.
if (empty() || valfn(themax) < valfn(theobject)) ...
What is ...? Well, all we need to do is have themax
point to theobject. Here is how to do it:
themax = theobject;
long val(void *object) { return (long)*(int*)object; }
The object is a pointer to void so it needs to be cast
to (int*). Then that pointer needs to be dereferenced to get
the value of the object: thus *(int*)object. Then that needs
to be cast to long to return a type as specified by the
prototype: thus, (long)*(int*)object. The val function
becomes an argument to the constructor of the PQExt class:
PQExt *pqe = new PQExt(val);
which means a constructor for the PQExt class has to be written.
This will do:
PQExt::PQExt (long (*v)(void*)) : PQueue(v, NULL) { }
It is important for you to struggle with this and master it.