Question: 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 28. This exercise uses a vector object to implement a priority queue class vpriority_queue. 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 priority-queue functions size() and empty().(ii) The push() operation inserts a new element at the back of the vector by using push_back(). 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(4)3521 pq Vector after push(4)35214(iii) The functions top() and pop() must compute the largest of the pqVector.size() el- ements. Do this computation with the private-member 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 findMaxIndex().(iv) 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 tor[maxIndex].(v) 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 (pqVector.back()) and copying its value into the vacated position. Remove the last element by using pqVector.pop_back(), 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 vpriority_queue object pq. int arr[5]={6,2,3,5,3}; ,3 vpriority_queue int> pg; int i; for (i=0; i <5; i++) pg.push(arr[i]);pq Vector 62354 cout << pg.top()<<""; pg.pop(); // output: 6 pq Vector 4235 cout << pq.top()<<""; pg.pop(); // output: 5 pq Vector 42.3 pq.push(10); Pq Vector 42310 pq.push(1); PqVector 42.3101 cout << pq.top()<<""; pg.pop(); // output: 10 pqVector 4.231// output: 4 cout << pg.top()<<""; pg.pop(); pq Vector 12.3 Implement the class vpriority_queue, whose class declaration follows. Place the class in the header file "vpqueue.h, and test the class by running Program 8-4. CLASS vpriority_queue Declaration "vpqueue.h" template class vpriority_queue { public: vpriority_queue(); // default constructor. create an empty priority queue void push(const 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; 1/ do we need to compute the index of the maximum element?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!