class PQueue{
public :
/*other public functions and the constructors,destructors*/
private :
struct chunkT{
vector<int> nums ;
chunkT *next ;
};
/*other private functions*/
};
The implementation code uses the struct chunkT to generate a linked lists of the given type but gives many errors .
basically , the error messages reported demonstrate that the compiler did not identify the data type : chunkT
Please suggest a reason for this behaviour or give a sample function to be written in the implementation that uses a pointer to the struct chunkT .
It depends on where you use it. If you declare a member function that takes a chunkT argument in the public section above it will fail to compile because chunkT has not yet been defined.
peter - I will write the function in the private part only and not in the public part .
ne - The private structure is needed to maintain the internal data processing and the function using the struct type will also be in the private section !
/* Implementation notes: enqueue
* -----------------------------
* Since we're keeping the vector in no particular order, we just append this
* new element to the end. It's the easiest/fastest thing to do.
*/
void PQueue::enqueue(int newValue)
{
addNumber(first,newValue ) ;
}
/* Implementation notes: dequeueMax
* --------------------------------
* Since we're keeping the vector in no particular order, we have to search to
* find the largest element. Once found, we remove it from the vector and
* return that value.
*/
int PQueue::dequeueMax()
{
if (isEmpty())
Error("Tried to dequeue max from an empty pqueue!");
int num = first->numbers[0] ;
first->numbers.removeAt(0) ;
if(first->numbers.size() == 0){
first = first->next ;
}
return num ;
}
/*
* Class: PQueue
* -------------
* This is the class for a priority queue. This is not
* simple FIFO queue, it is a priority queue, where elements are
* retrieved in order of priority, not just by longevity in queue.
* The elements are integers and the integer is assumed to represent
* the priority (larger integer is higher priority).
*/
class PQueue
{
public:
/*
* Constructor: PQueue
* Usage: PQueue pq;
* PQueue *ppq = new PQueue;
* ---------------------------------
* Initializes a new pqueue to have no elements.
*/
PQueue();
/*
* Destructor: ~PQueue
* Usage: delete ppq;
* ------------------
* Deallocates all the storage associated with this pqueue.
*/
~PQueue();
/*
* Member function: isEmpty
* Usage: if (pq.isEmpty()) . . .
* -------------------------------
* Returns true if this pqueue contains no elements.
*/
bool isEmpty();
/*
* Member function: size
* Usage: nElemes = pq.size();
* ---------------------------
* Returns number of elements contained in this pqueue.
*/
int size();
/*
* Member function: enqueue
* Usage: pq.enqueue(val);
* -----------------------
* Adds the specified element to this pqueue. No effort is made to
* avoid duplicates.
*/
void enqueue(int newElem);
/*
* Member function: eequeueMax
* Usage: maxElem = pq.dequeueMax();
* ---------------------------------
* Removes the largest priority element from this pqueue and returns it.
* If this pqueue is empty, this function raises an error.
*/
int dequeueMax();
/*
* Member function: bytesUsed
* Usage: numBytes = pq.bytesUsed();
* ----------------------------------
* This function would not usually be included as part of the class,
* but this is here as part of evaluating the tradeoffs betweem
* implementations. Given a pqueue, this function counts up
* and return the total amount of space used given its
* current contents.
*/
int bytesUsed();
/*
* Member function: implementationName
* Usage: cout << pq.implementationName();
* ---------------------------------------
* This operation would not usually be included as part of the class
* class, but is included to help with analyzing and reporting results.
* This member function returns a string that describes the PQueue
* implementation strategy ("sorted linked list", "vector", etc.).
*/
string implementationName();
/*
* Member function: printDebuggingInfo
* Usage: pq.printDebuggingInfo();
* -------------------------------
* This operation would not usually be included as part of the class,
* but is included to give you a hook to put any helpful debugging
* print code (for example, something that prints the goopy details of
* the internal structure). You don't need to implement this routine and can
* ignore it entirely, but you may find it helpful to use as you are
* doing development.
*/
void printDebuggingInfo();
When declaring the structure, it should be declared without the *.
Also, vector should be declared as lower case, not with a leading cap (numerous places).