Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

Rationalize the denominator 1 9 7 Rationalize the denominator. 1

Answered: 1 week ago

Question

Does it have at least one-inch margins?

Answered: 1 week ago

Question

Does it have correct contact information?

Answered: 1 week ago

Question

Does it exceed two pages in length?

Answered: 1 week ago