Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C + + Data Structure PLEASE POST CODE AND OUTPUT! PROGRAMMING PROJECT 2 8 . This exercise uses a vector object to implement a priority
C Data Structure PLEASE POST CODE AND OUTPUT! PROGRAMMING PROJECT This exercise uses a vector object to implement a priority queue class vpriorityqueue. Assume that the name of the vector object included by composition is pq Vector. The fol lowing is an outline for the implementation of the class member functions: i Use the size operation in the vector class to implement the priorityqueue functions size and emptyii The push operation inserts a new element at the back of the vector by using pushback The elements in the vector are not ordered in any way. Assign the boolean data member recomputeMaxIndex the value true, since the new value may be the largest in the priority queue. Part iii describes this variable. pq Vector before push pq Vector after pushiii The functions top and pop must compute the largest of the pqVector.size el ements. Do this computation with the privatemember function findMaxIndex which assigns to the data member maxIndex the index of the maximum element. The functions top and pop both need the maximum value in the vec lare a boolean data member recomputeMaxIndex, and initialize it to false. The functions top and pop use recomputeMaxIndex to avoid unnecessary calls to findMaxIndexiv The function top returns the maximum value in pqVector. The function top first checks to see whether the priority queue is empty. In that case, throw the exception underflowError. A second check looks at recomputeMaxIndex. If it is true, call findMaxIndex assign its return value to maxIndex, and set re computeMaxIndex to false. In either case, top returns the element pqVec tormaxIndexv If recomputeMaxIndex is true, the function pop finds the maximum element by calling findMaxIndex The function must remove the highest priority item from the vector, which vacates a position. Sliding the tail of the sequence toward the front of the vector is inefficient. Implement the removal process by locating the last element in the vector pqVectorback and copying its value into the vacated position. Remove the last element by using pqVector.popback and set the value of recomputeMaxIndex to true, indicating that a subsequent call to a top or pop requires a new maximum. The figures show the sequence of values in pqVector as push and pop operations ex ecute for the vpriorityqueue object pq int arr; vpriorityqueue int pg; int i; for i; i ; i pgpusharri;pq Vector cout pgtop; pgpop; output: pq Vector cout pqtop; pgpop; output: pq Vector pqpush; Pq Vector pqpush; PqVector cout pqtop; pgpop; output: pqVector output: cout pgtop; pgpop; pq Vector Implement the class vpriorityqueue, whose class declaration follows. Place the class in the header file "vpqueue.h and test the class by running Program CLASS vpriorityqueue Declaration "vpqueue.h template class vpriorityqueue public: vpriorityqueue; default constructor. create an empty priority queue void pushconst T& item; insert item into the priority queue. Postcondition: the priority queue has one more element void pop; remove the highest priority maximum item from the priority queue. Precondition: the priority queue is not empty. if it is empty, the function throws the underflowError exceptionT& top; return the highest priority maximum item in the priority queue Precondition: the priority queue is not empty. if it is empty, the function throws the underflowError exception const T& top const; constant version bool empty const; is the priority queue empty? int size const; return the size of the priority queue private: vector pqVector; vector that implements the priority queueint findMaxIndex const; find the index of the maximum value in pqVector int maxIndex; index of the maximum value bool recomputeMaxIndex; do we need to compute the index of the maximum element?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started