Complete the class Chari that implements a Chari-like queue that CS 225 uses for
ID: 3861977 • Letter: C
Question
Complete the class Chari that implements a Chari-like queue that CS 225 uses for office hours. This function must have at least three member functions: get Status, returns the string: "Empty", if the Chari queue is empty "Light", if the Chari queue has 3 or fewer students in it "Moderate", if the Chari queue has between 4 and 6 (inclusive) students in it "Heavy", if the Chari queue has more than 6 students in it add To Queue, adds a name (string) to the Chari queue pop From Queue, removes the front person from the Chari queue, returning their name You will need to add additional private members to maintain your data structure. Additionally, no testing code is provided -- you should write test cases to ensure your function works as expected.Explanation / Answer
A)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct node
{
int priority;
int info;
struct node *link;
};
class Priority_Queue
{
private:
node *front;
public:
Priority_Queue()
{
front = NULL;
}
void insert(int item, int priority)
{
node *tmp, *q;
tmp = new node;
tmp->info = item;
tmp->priority = priority;
if (front == NULL || priority < front->priority)
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while (q->link != NULL && q->link->priority <= priority)
q=q->link;
tmp->link = q->link;
q->link = tmp;
}
}
void del()
{
node *tmp;
if(front == NULL)
cout<<"Queue Underflow ";
else
{
tmp = front;
cout<<"Deleted item is: "<<tmp->info<<endl;
front = front->link;
free(tmp);
}
}
void display()
{
node *ptr;
ptr = front;
if (front == NULL)
cout<<"Queue is empty ";
else
{ cout<<"Queue is : ";
cout<<"Priority Item ";
while(ptr != NULL)
{
cout<<ptr->priority<<" "<<ptr->info<<endl;
ptr = ptr->link;
}
}
}
};
int main()
{
int choice, item, priority;
Priority_Queue pq;
do
{
cout<<"1.Insert ";
cout<<"2.Delete ";
cout<<"3.Display ";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the item value to be added in the queue : ";
cin>>item;
cout<<"Enter its priority : ";
cin>>priority;
pq.insert(item, priority);
break;
case 2:
pq.del();
break;
case 3:
pq.display();
break;
default :
cout<<"Wrong choice ";
}
}
while(choice != 3);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.