zy Oklahoma State Uni x m/ /zybook/OKSTATECS2433Chan-Tinspring2017/chapterl16/se
ID: 3813411 • Letter: Z
Question
zy Oklahoma State Uni x m/ /zybook/OKSTATECS2433Chan-Tinspring2017/chapterl16/section/1 VERIFY EMAl This content is controlled by your instructor, and is not zyBooks content. Direct questions or concerns about this content to your instructor If you have amy lechn Cal issues with the zyLab submission system, use the "Trouble with lab? button at the bottom of the lab. Implement a Priority Queue for strings. A priority queue is similar to a regular queue except each item added to the queue also has an associated priority. For this problem, make the priority an integer where 0 is the highest priority and larger values are increasingly lower in priority The remove function should return and remove the item that has the highest priority. For example: q. add("X", 10); q add("Y", 1); q. add("Z", 3) cout q remove(); Returns Y cout q. remove(); Returns Z cout q remove(); Returns X You can implement the priority queue by performing a linear search in the remove0 function. Hint Take a look at the Linked List code example from 0330, which can downloaded from D2L Reusing the code example is acceptable and modifying it is acceptable. You are expected to use pointers. You will get a o if you use STL priorityqueue or any other already implemented priority queue data structure Important Make sure to use the template provided since the test cases depend on it submission 16.13.1: Homework9 (due Apr 8)Explanation / Answer
In priority queue class , under private section we can Add the following :
- Node *begn; // start node
- Node *curr; // current ending node
- Node *prev; //previous node
- int highest; // for highest priority
- int lowest; // for lowest priority
under public section we can modify add method like this :
PriorityQueue()
{
begn=null;
curr=null;
prev=null;
}
void add(string item,int priority)
{
Node n=new Node(item,priority);
if(priority>this.highest)
this.highest=priority;
else if(priority<this.lowest)
this.lowest=priority;
if(begn!=null){
prev->link=curr;
curr->link=n;
else{
begn=&n;
prev=&n;
curr=&n;
}
}
string remove()
{
prev->link=null;
return curr->data;
}
In the node class , under public section add the following :
Node();
Node(string data,int priority)
{
this.data=data;
this.priority=priority;
}
void setData(string data)
{
this.data=data;
}
void setPriority(int priority)
{
this.priority=priority;
}
int getPriority()
{
return this.priority;
}
string getData()
{
return this.data;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.