Question: C++ Code Help: Problem: Give a C++ implementation of a priority queue based on an unsorted list. My code is throwing up tons of errors...

C++ Code Help:

Problem: Give a C++ implementation of a priority queue based on an unsorted list.

My code is throwing up tons of errors...

#include "stdafx.h"

#include

#include

#include

#include

#include

#include

using namespace std;

//declare a class called PriorityQueue

template

class PriorityQueue

{

public:

//returns the size of PriorityQueue

int size() const;

//returns a Boolean value if the PriorityQueue is

//empty

bool isEmpty() const;

//inserts an element into the PriorityQueue

void insert(const E& e);

// returns a reference of the PriorityQueue minimum

//element

E& min();

// removes an element from the PriorityQueue

void removeMin();

//Create a predefined list object of type E

list elements;

//create an object to the Comparator class C

C isLess;

};

//Define the member function size() is of constant type and returns the size of the elements

//present in the PriorityQueue.Internally, it returns the size of the list.

//definition size() method

template

int PriorityQueue::size() const

{

return elements.size();

}

//Define the member function isEmpty() is of constant type.It returns true if the list in the

//PriorityQueues size is 0 else returns false.

//definition isEmpty() method

template

bool PriorityQueue::isEmpty() const

{

return (size() == 0);

}

//Define the member function insert()that contains a constant reference object of type E.Declare

//an iterator for the list of type E to iterate the elements present in the list.

//definition insert() method

template

void PriorityQueue::insert(const E& e)

{

typename list::iterator p;

p = elements.begin();

while (p != elements.end() && !isLess(e, *p))

elements.insert(p, e);

}

//The definitions of member functions min() and removeMin() are same as specified in the text

//book code reference 8.10.

//definition min() method

template

E& PriorityQueue::min()

{

return elements.front();

}

//definition removeMin() method

template

void PriorityQueue::removeMin()

{

elements.pop_front();

}

//Define the class LeftRight which defines the overload operator() function of type constant that

//returns a Boolean value.The function takes two reference objects of type E.

//The function returns true if the value of p is less than the value of q, else returns false.

//definition LeftRight comparator class

template

class LeftRight

{

public:

bool operator()(const E& p, const E& q) const

{

return (p < q);

}

};

//Define the main function that reads values into list that are entered by the user.Print the values

//present in the list and insert the values into the PriorityQueue simultaneously.Print the values

//present in the PriorityQueue.

//definition of main function

int main()

{

//create a list of type int

list listValues;

//create an iterator for list

list::iterator iterate;

//declare the required variables

int sizeofList, value;

//declare an PriorityQueue object

PriorityQueue> priorQueue;

//prompt the user to insert the number of elements to

//be inserted into the list

cout << "Enter the size of list: ";

cin >> sizeofList;

//Prompt the user to insert the values until the

//iterate variable reaches the sizeofList

for (int i = 0; i

{

cout << "Enter element " << (i + 1) << " value: ";

cin >> value;

listValues.push_back(value);

}

cout<

//print the elements present in the list and at the

//same time insert elements into the PriorityQueue

cout << "Elements present in the list are: "<

for (iterate = listValues.begin();

iterate != listValues.end(); iterate++)

{

cout << *iterate << " ";

priorQueue.insert(*iterate);

}

cout<

cout<

//Print the elements present in the PriorityQueue

cout << "Elements present in the PriorityQueue are "

<< ": " <

while (!priorQueue.isEmpty())

{

int value = priorQueue.min();

cout << value << " ";

priorQueue.removeMin();

}

cout<

system("pause");

return 0;

}

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!