Priority Queue C + + implementation and application

Source: Internet
Author: User

#include <iostream>
#include <stdlib.h>
using namespace Std;
Number of priority queue array elements
const int maxpqsize = 50;
Template<class t>
Class pqueue{
Private
int count;
T Pqlist[maxpqsize];
Public
Pqueue (void);
void Pqinsert (const t& Item);
T pqdelete (void);
void Clearpq (void);
Detection Priority Queue Status action
int pqempty (void) const;
int pqfull (void) const;
int pqlength (void) const;

};


Template <class t>
Pqueue<t>::P queue () {
Count = 0;
}
Template<class t>
int pqueue<t>::P qempty () const{
return count==0;
}
Template<class t>
void Pqueue<t>::P qinsert (const t& Item) {

if (count = = maxpqsize) {
cerr<< "The queue has overflowed!" <<endl;

}
Place elements at the end of the queue and make count+1
Pqlist[count] = Item;
count++;
}
Place the tail element at the maximum priority element count--
Template<class t>
T pqueue<t>::P Qdelete () {
T min;
int i,minindex = 0;
if (Count > 0) {
Find the lowest value in the Pqlist the very subscript
Min = pqlist[0];//assumes that the first element is the minimum value
Sequential access elements Modify the minimum and subscript values
for (i = 1; i < count; i++)
if (Pqlist[i] < min) {
min = Pqlist[i];
Minindex = i;
}

Move the tail element into the smallest element and subtract count by one
Pqlist[minindex] = pqlist[count-1];
count--;
}else{
cerr<< "Deleting from an empty Pqueue" <<endl;
}
Return min;//returns the minimum value
}




Application

#include <iostream>
#include <iomanip>
#include <fstream>
#include "PQueue.h"
/* Run this program using the console Pauser or add your own getch, System ("pause") or input loop */
Define a record for a request order
Enum Staff{manager,supervisor,worker
};//Employee type Manager highest priority
struct Jobrequest
{
Staff Staffperson;
int Jobid;
int jobtime;
};//Working Status
int operator< (const jobrequest& a,const jobrequest& b) {
return A.staffperson < B.staffperson;
}

void Printjobinfo (Jobrequest PR) {
Switch (Pr.staffperson) {
Case Manager:
cout << "Manager";
Break
Case Supervisor:
cout<< "Supervisor";
Break
Case Worker:
cout<< "Worker";
Break
}
Cout<<pr.jobid << "" <<PR.jobTime<<endl;
}

void printjobsummary (int jobservicesuse[]) {
cout<< "\ntotal support usage\n";
cout<< "Maager" &LT;&LT;SETW (3)
&LT;&LT;JOBSERVICESUSE[0]&LT;&LT;SETW (3) <<endl;
cout<< "Supervisor" &LT;&LT;SETW (3) <<jobServicesUse[1]<<endl;
cout<< "Worker" &LT;&LT;SETW (3) <<jobServicesUse[2]<<endl;
}
int main (int argc, char** argv) {
Process no more than 50 service request orders
Pqueue<jobrequest> Jobpool;
Read the service request form from fin
Ifstream fin;
Hours of service for each type of employee
int jobservicesuse[3]={0,0,0};
Jobrequest PR;
Char ch;
Open the input file Job.dat quit the program if it fails
Fin.open ("Job.dat", ios::in);
if (!fin) {
cerr<< "Cannot open file Job.dat" <<endl;
}

Read each request from a file and insert it into the priority queue Jobpool
Each line starts with a character that indicates the employee category
while (Fin>>ch) {
Switch (CH) {
Case ' M ': Pr.staffperson = Manager;
Break
Case ' S ': Pr.staffperson = Supervisor;
Break
Case ' W ': Pr.staffperson = Worker;
Break
Default:break;
}
Read Jobid and Jobtime in PR
fin>>pr.jobid;
fin>>pr.jobtime;
To insert a PR into the priority queue
cout<<pr.jobid<< "* * *" <<endl;
Jobpool. Pqinsert (PR);
}

Remove the service from the priority queue and output information about the service
cout<< "Category Job ID job Time" <<endl;
while (!jobpool. Pqempty ()) {
PR = Jobpool. Pqdelete ();
Printjobinfo (PR);
Cumulative service time for each type of employee
Jobservicesuse[int (Pr.staffperson)]+=pr.jobtime;
}
Printjobsummary (Jobservicesuse);
return 0;
}
























































































Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Priority Queue C + + implementation and application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.