Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEEDS TO BE A C++ program!!! implement a priority queue of maximum length 25. Us

ID: 3683302 • Letter: N

Question

NEEDS TO BE A C++ program!!!

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.

**Please add comments if possible, thanks!

Explanation / Answer

Programme:

// Example programme for simple priority queue using linked lists.
// Priority depends on identity number. Small identity number has greater priority.
// If identity numbers are equal. Then FIFO rules are used.

#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;

struct DAT
{
int id;
char fullname[50];
double salary;
};

struct NODE
{
DAT data;
Node *N;
Node *P;
Node(const int i , const char *f, const double s )
{
data.id = i;
strcpy(data.fullname,f);
data.salary = s;
N = NULL;
P = NULL;
}

};

class PQueueLinkedList
{
private:
NODE *front;
NODE *back;
public:
PQueueLinkedList(){front = NULL;back = NULL;}
~PQueueLinkedList(){destroyList();}
void enqueue(NODE *);
NODE* dequeue();
void destroyList();
};

void PQueueLinkedList::enqueue(NODE *n)
{
if(front == NULL)//queue has one node.
{
front = n;
back = n;
}
else//queue has more than one node.
{
NODE* temp = front;
if( n->data.id > temp->data.id)//New node id's is greater than all others.
{
front->P = n;
n->N = front;
front = n;
}
else
{
//Search for the position for the new node.
while( n->data.id < temp->data.id)
{
if(temp->N == NULL)
break;
temp = temp->N;
}
//New node id's smallest than all others
if(temp->N == NULL && n->data.id < temp->data.id)
{
back->N = n;
n->P = back;
back = n;
}

else//New node id's is in the medium range.
{
temp->P->N = n;
n->P = temp->P;
n->N = temp;
temp->P = n;
}
}

}
}

NODE* PQueueLinkedList::dequeue()
{
NODE *temp;
if( back == NULL )//no nodes
return NULL;
else if(back->P == NULL)//there is only one node
{
NODE * temp2 = back;
temp = temp2;
front = NULL;
back = NULL;
delete temp2;
return temp;
}
else//there are more than one node
{
NODE * temp2 = back;
temp = temp2;
back = back->P;
back->N = NULL;
delete temp2;
return temp;
}
}

void PQueueLinkedList::destroyList()
{
while(front != NULL)
{
NODE *temp = front;
front = front->N;
delete temp;
}
}

void disp(NODE *m)
{
if( m == NULL )
{
cout << " Queue is Empty!!!" << endl;
}
else
{
cout << " Id No. : " << m->data.id;
cout << " Full Name : " << m->data.fullname;
cout << " Salary : " << setprecision(15) << m->data.salary << endl;
}
}

int main()
{
PQueueLinkedList *Queue = new PQueueLinkedList();

NODE No1( 101, "Aaaaa Nnnnnnn Mmmmm", 123456.4758 );
NODE No2( 102, "Bbbbb Ddddd Ssssss", 765432.9488 );
NODE No3( 103, "wwww nnnnn www eeee", 366667.3456 );
NODE No4( 104, "Bsrew hytre dfresw", 9876544.0432 );

Queue->enqueue(&No4);
Queue->enqueue(&No3);
Queue->enqueue(&No1);
Queue->enqueue(&No2);

disp(Queue->dequeue());

disp(Queue->dequeue());

disp(Queue->dequeue());

disp(Queue->dequeue());

disp(Queue->dequeue());

delete Queue;
return 0;
}

Output:

Id No. : 101
Full Name : Aaaaa Nnnnnnn Mmmmm
Salary : 123456.4758

Id No. : 102
Full Name : Bbbbb Ddddd Ssssss
Salary : 765432.9488

Id No. : 103
Full Name : wwww nnnnn www eeee
Salary : 366667.3456

Id No. : 104
Full Name : Bsrew hytre dfresw
Salary : 9876544.0432

Queue is Empty!!!

Process returned 0 (0x0) execution time : 0.015 s
Press any key to continue.