Menu

[Solved] C Please Thanks Implement Priority Queue Heap Priorityqueuecpp Declarations File Priorityq Q37174261

In c++ please. Thanks

Implement priority queue as a heap inPriorityQueue.cpp. The declarations from the filePriorityQueue.h derive a class called PriorityQueue from the Heapclass.

  • Constructor, enqueue, and dequeue .
  • Test the implementation with test11pq.cpp and provide ascreen shot below

Using the program shell given in the file ossim.cpp asa basis, create a program that uses the Priority Queue ADT toimplement a task scheduler.

  • Add your implementation to ossim.cpp to finishthe task scheduler simulator
  • Use your program to simulate the flow of tasks throughthe priority queue and complete the table below
  • Fill in your answers to Question 1 and 2below.

———————————————————————————————–

Heap.h

#ifndef HEAP_H
#define HEAP_H

#include <stdexcept>
#include <iostream>

using namespace std;

template < typename KeyType=int >
class Greater {
public:
bool operator()(const KeyType &a, const KeyType &b) const {return a > b; }
};

template < typename DataType, typename KeyType=int, typenameComparator=Greater >
class Heap
{
public:

static const int DEFAULT_MAX_HEAP_SIZE = 10; // Default maximumheap size

// Constructor
Heap ( int maxNumber = DEFAULT_MAX_HEAP_SIZE ); // Defaultconstructor + basic constr
Heap ( const Heap& other );       //Copy constructor
Heap& operator= ( const Heap& other ); // Overloadedassignment operator

// Destructor
~Heap ();

// Heap manipulation operations
void insert ( const DataType &newDataItem ) // Insert a dataitem
throw ( logic_error );
DataType remove () throw ( logic_error ); // Remove max priorityelement
void clear (); // Clear heap

// Heap status operations
bool isEmpty () const; // Heap is empty
bool isFull () const; // Heap is full

// Output the heap structure — used in testing/debugging
void showStructure () const;

// Programming exercise #3 operation
void writeLevels () const; // Output in level order

private:

// Recursive helper of the showStructure() function
void showSubtree ( int index, int level ) const;

// Data members
int maxSize, // Maximum number of elements in the heap
size; // Actual number of elements in the heap
DataType *dataItems; // Array containing the heap elements

Comparator comparator;
};

#endif   //#ifndef HEAP_H

———————————————————————————————–

ossim.cpp

#include <iostream>
#include <cstdlib>
#include “PriorityQueue.cpp”

using namespace std;

//——————————————————————–
//
// Declaration for the task data struct
//

struct TaskData
{
int getPriority () const
{ return priority; } // Returns the priority. Needed by theheap.

int priority, // Task’s priority
arrived; // Time when task was enqueued

};

//——————————————————————–

int main ()
{
PriorityQueue > taskPQ; // Priority queue of tasks
TaskData task; // Task
int simLength, // Length of simulation (minutes)
minute, // Current minute
numPtyLevels, // Number of priority levels
numArrivals, // Number of new tasks arriving
j; // Loop counter

// Seed the random number generator
srand( (unsigned int) time( NULL ) );

cout << endl << “Enter the number of priority levels: “;
cin >> numPtyLevels;

cout << “Enter the length of time to run the simulator :”;
cin >> simLength;

for ( minute = 0 ; minute < simLength ; minute++ )
{
// Dequeue the first task in the queue (if any).
   // Your code here

// Determine the number of new tasks and add them to
// the queue.
   // Your code here

}

return 0;
}

———————————————————————————————–

Priorityqueue.cpp

#ifndef PRIORITYQUEUE_CPP
#define PRIORITYQUEUE_CPP

using namespace std;

#include “PriorityQueue.h”

//——————————————————————–

template < typename DataType, typename KeyType, typenameComparator >
PriorityQueue:: PriorityQueue ( int maxNumber )

// Creates an empty priority queue.
{}

//——————————————————————–

template < typename DataType, typename KeyType, typenameComparator >
void PriorityQueue:: enqueue ( const DataType &newDataItem)

// Inserts newDataItem into a priority queue.

{
}

//——————————————————————–

template < typename DataType, typename KeyType, typenameComparator >
DataType PriorityQueue:: dequeue ()

// Removes the least recently added (front) data item from apriority
// queue and returns it.

{
}

———————————————————————————————–

Priorityqueue.h

#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H

#include <stdexcept>
#include <iostream>

using namespace std;

#include “Heap.cpp”

const int defMaxQueueSize = 10; // Default maximum queuesize

template < typename DataType, typename KeyType=int, typenameComparator=Greater >
class PriorityQueue : public Heap
{
public:

// Constructor
PriorityQueue ( int maxNumber = defMaxQueueSize );

// Queue manipulation operations
void enqueue ( const DataType &newDataItem ); // Enqueue dataelement
DataType dequeue (); // Dequeue data element
};

#endif

———————————————————————————————–

test11pq.cpp

#include <iostream>
#include <cctype>
#include “PriorityQueue.cpp”

using namespace std;

void printHelp();

//——————————————————————–
//
// Declaration for the priority queue data item class
//

class TestData
{
public:

void setPriority ( int newPriority )
{ priority = newPriority; } // Set the priority

int getPriority () const
{ return priority; } // Returns the priority

private:

int priority; // Priority for the data item
};

//——————————————————————–

int main()
{
PriorityQueue testQueue(8); // Test priority queue
TestData testData; // Queue data item
int inputPty; // User input priority
char cmd; // Input command

printHelp();

do
{
testQueue.showStructure(); // Output queue

cout << endl << “Command (H for help): “; // Readcommand
cin >> cmd;
   cmd = toupper( cmd );          // Normalize to uppercase
if ( ( cmd == ‘+’ ) || ( cmd == ‘>’ ) )
cin >> inputPty;

switch ( cmd )
{
case ‘+’ : // enqueue
testData.setPriority(inputPty);
cout << “Enqueue : pty = ” <<testData.getPriority()
<< endl;
testQueue.enqueue(testData);
break;

case ‘-‘ : // dequeue
testData = testQueue.dequeue();
cout << “Dequeued : pty = ” <<testData.getPriority()
<< endl;
break;

case ‘C’ :
cout << “Clear the queue” << endl;
testQueue.clear();
break;

case ‘E’ : // isEmpty
if ( testQueue.isEmpty() )
cout << “Queue is empty” << endl;
else
cout << “Queue is NOT empty” << endl;
break;

case ‘F’ : // isFull
if ( testQueue.isFull() )
cout << “Queue is full” << endl;
else
cout << “Queue is NOT full” << endl;
break;

case ‘H’ : // Print out help menu
   printHelp();
break;

case ‘Q’ : // Quit test program
break;

default : // Invalid command
cout << “Invalid command” << endl;
}
}
while ( cmd != ‘Q’ );

return 0;
}

void printHelp() {
cout << endl << “Commands:” << endl;
cout << ” +x : Enqueue data item with priority x” <<endl;
cout << ” – : Dequeue data item” << endl;
cout << ” C : Clear the queue” << endl;
cout << ” E : Empty queue?” << endl;
cout << ” F : Full queue?” << endl;
cout << ” H : Print this help message” << endl;
cout << ” Q : Quit the test program” << endl;
cout << endl;
}

Expert Answer


Answer to In c++ please. Thanks Implement priority queue as a heap in PriorityQueue.cpp. The declarations from the file PriorityQu… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *