C++ Write a template version of a class that implements a priority queue. To sum
ID: 3695562 • Letter: C
Question
C++
Write a template version of a class that implements a priority queue. To summarize, a priority queue is essentially a list of items that is always ordered by priority. Each item that is added to the list requires an associated priority value. For this problem, make the priority an integer where 0 is the highest priority and larger values are lower in priority. Removing an item from the queue removes the item with the highest priority.
The add function of the priority queue should take a generic type and then an integer priority. In the following example, the generic type is a char and we have added three items to the queue:
q.add(‘X’, 10);
q.add(‘Y’, 1);
q.add(‘Z’, 3);
The remove function should return and remove from the priority queue the item that has the highest priority. Given the example above, we would expect the following:
cout << q.remove(); //Outputs Y (priority 1)
cout << q.remove(); //Returns Z (priority 3)
cout << q.remove(); //Returns X (priority 10)
Test your queue on data with priorities in various orders (for example, ascending, descending, mixed). You can implement the priority queue by storing the items using a list(s) of your choice (for example, vector array, linked list, or GenericList) and then performing a linear search for the item with the lowest integer value in the remove function. In future courses you may study a data structure called a heap that affords a more efficient way to implement a priority queue.
Explanation / Answer
Ans;
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
int priority;
string data;
node*next;
node()
{
data = "";
priority = -1;
next=NULL;
}
node(string d, int p, node*temp)
{
data = d;
priority = p;
next=temp;
}
};
class queue
{
private:
node*head;
node*tail;
public:
queue()
{
head=NULL;
tail=NULL;
}
bool add(string data, int priority)
{
node*temp=new node();
if(temp==NULL) // if system memory is full return false
{
return false;
}
else // if system memory is not full then enque
{
if(head==NULL)
{
temp->data=data;
temp->priority = priority;
temp->next=NULL;
head=temp;
}
else if(head->next==NULL)
{
if(head->priority>priority)
{
temp->data=data;
temp->priority = priority;
temp->next=head;
head=temp;
}
else if(head->priority<=priority)
{
temp->data=data;
temp->priority = priority;
temp->next=NULL;
temp->next=head->next;
head->next=temp;
}
}
else
{
node*current=head;
if(head->priority>priority)
{
temp->data=data;
temp->priority=priority;
temp->next=NULL;
temp->next=head;
head=temp;
}
else
{
while(current->next!=NULL)
{
if(current->next->priority>priority)
{
break;
}
current=current->next;
}
temp->data=data;
temp->priority=priority;
temp->next=current->next;
current->next=temp;
}
}
return true;
}
}
string remove()
{
string data;
node*current=head;
if(current==NULL)
{
return "";
}
else
{
data=current->data;
head=current->next;
delete current;
return data;
}
}
bool isEmpty()
{
if(head==NULL)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
queue q;
cout<<"priority mixed: ";
q.add("x", 10);
q.add("y",1);
q.add("z", 3);
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<" ascending order ";
q.add("x", 1);
q.add("y",2);
q.add("z", 3);
q.add("w", 4);
q.add("v", 5);
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<" descending order ";
q.add("x", 5);
q.add("y", 4);
q.add("z", 3);
q.add("w", 2);
q.add("v", 1);
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<q.remove()<<" ";
cout<<" priority queue to search a in queue(example) ";
q.add("x", 5);
q.add("y", 4);
q.add("a", 3);
q.add("w", 2);
q.add("v", 1);
string a = "a";
int isfound = 0;
while(!q.isEmpty())
{
if(a == q.remove())
{
cout<<"a is in the queue ";
isfound = 1;
break;
}
}
if(!isfound)
cout<<"a not in the queue ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.