MAKE SURE THIS IS procedural code C++ Implement a priority queue of maximum leng
ID: 3681825 • Letter: M
Question
MAKE SURE THIS IS procedural code C++
Implement a priority queue of maximum length 25. Use priorities ranging from 0 to 10 with the highest priority a 0. Prompt the user for a priority and a job name (a string), and allow the user to enter a -1 priority to quit entering jobs.
This exercise requires that you implement a function to insert a job anywhere in the queue, which must be implemented as a linked list. Do not remove any duplicates; that is, the same job can be entered multiple times with the same or different priorities.
After the user has quit entering priorities and jobs, print out the resulting queue from head to tail.
If you could add comments, explaining whats going on, it would be appreciated!
Explanation / Answer
/*
* C++ Program to Implement Priority Queue
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int priority;
string name;
struct node *link;
}*head;
/*
* Insert into Priority Queue
*/
void insert(string name, int priority){
node *tmp, *q;
tmp = new node; // creating new node
tmp->name = name; // assinging name
tmp->priority = priority; // assiging priority
if (head == NULL || priority < head->priority) // if there is no item in queue or current job has higest priority
{
tmp->link = head;
head = tmp;
}
else // else travese list till you get a node with lower priority
{
q = head;
while (q->link != NULL && q->link->priority <= priority)
q=q->link;
tmp->link = q->link;
q->link = tmp;
}
}
// this function is to delete all nodes from queue
void del()
{
node *tmp;
if(head == NULL)
cout<<"Queue Underflow ";
else
{
tmp = head;
cout<<"Deleted item is: "<<tmp->name<<endl;
head = head->link;
free(tmp);
}
}
/* This function is to display the content of queue
*
* */
void display()
{
node *ptr;
ptr = head;
if (head == NULL) // no item
cout<<"Queue is empty ";
else
{
cout<<"Queue is : ";
cout << setw(21) << left <<"Name "<<"Priority ";
while(ptr != NULL) // traverse till last
{
cout << setw(21) << left <<ptr->name<<" "<<ptr->priority<<endl;
ptr = ptr->link;
}
}
}
/*
* Main
*/
int main()
{
char choice;
int priority;
string name;
do
{
cout<<"Input the name of job to be added in the queue : ";
cin>>name;
cout<<"Enter its priority : ";
cin>>priority;
insert(name, priority);
cout<<"Do you want to add more job (y/n): ";
cin>>choice;
}while(choice =='y');
display();
return 0;
}
/*
Sample output:
Input the name of job to be added in the queue : office
Enter its priority : 4
Do you want to add more job (y/n): y
Input the name of job to be added in the queue : player
Enter its priority : 0
Do you want to add more job (y/n): y
Input the name of job to be added in the queue : texteditor
Enter its priority : 3
Do you want to add more job (y/n): y
Input the name of job to be added in the queue : pdfreader
Enter its priority : 8
Do you want to add more job (y/n): y
Input the name of job to be added in the queue : movieplayer
Enter its priority : 2
Do you want to add more job (y/n): y
Input the name of job to be added in the queue : eclipse
Enter its priority : 1
Do you want to add more job (y/n): n
Queue is :
Name Priority
player 0
eclipse 1
movieplayer 2
texteditor 3
office 4
pdfreader 8
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.