Priority queue
#include <iostream>
using namespace Std;
Class Queue
{
Private
struct node//Create a node information, including data and priority
{
int data;
int level;
Node ():d ATA (), Level (-1) {}
Node (int data_, int level_):d ata (Data_), level (LEVEL_) {}
};
Node Datas[5];
int front; Team first
int back; End of Team
int cont; Number of valid
Public
Queue (): Front (), Back (), Cont () {}
void push (int data, int level)//press-in data
{
if (full ())//Fully inspected
{
cout << "full" << Endl;
Cin.get ();
Exit (1);
}
int temp = Front;
for (int i = 0; i < cont; i++)//Validation priority
{
if (Datas[temp%5].level < level)//Put high priority nodes in front
{
Node T1 = datas[temp% 5];
Datas[temp% 5] = {data, level};
for (int j = i; J < Cont; J + +)
{
Node t2 = datas[(temp + 1)% 5];
temp++;
Datas[temp% 5] = T1;
T1 = T2;
}
back++; The tail of the team moves backwards.
cont++; Effective number plus One
Return
}
temp++;
}
datas[back++% 5] = {data, level};//if the current number has the lowest priority, put it at the end of the queue
cont++;
}
int pop ()//Popup data
{
if (empty ())
{
Cerr << "Empty" << Endl;
Cin.get ();
Exit (1);
}
cont--;
return datas[front++% 5].data;
}
int Get_front ()//Get team head data
{
return datas[front].data;
}
int Get_back ()//Get team Tail data
{
return datas[back].data;
}
void clear ()//Empty queue
{
Front = Cont = back = 0;
}
BOOL Full ()//fully sentenced
{
Return cont > 4;
}
bool Empty ()//Empty
{
return cont = = 0;
}
int size ()
{
Return cont;
}
};
int main ()
{
Cin.get ();
return 0;
}
Priority Queue C + + array implementation