Priority queue C ++ array implementation, priority queue c Array
// 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 (): data (), level (-1 ){}
Node (int data _, int level _): data (data _), level (level _){}
};
Node datas [5];
Int front; // head of the team
Int back; // team end
Int cont; // valid number
Public:
Queue (): front (), back (), cont (){}
Void push (int data, int level) // press data
{
If (full () // full check
{
Cout <"full" <endl;
Cin. get ();
Exit (1 );
}
Int temp = front;
For (int I = 0; I <cont; I ++) // verify the priority
{
If (datas [temp % 5]. level <level) // place the node with higher priority 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 ++; // move one back at the end of the team
Cont ++; // Add one valid number
Return;
}
Temp ++;
}
Datas [back ++ % 5] = {data, level}; // if the current number has the lowest priority level, put it at the end of the team
Cont ++;
}
Int pop () // pop up data
{
If (empty ())
{
Cerr <"null" <endl;
Cin. get ();
Exit (1 );
}
Cont --;
Return datas [front ++ % 5]. data;
}
Int get_front () // get the first data of the team
{
Return datas [front]. data;
}
Int get_back () // get the data at the end of the team
{
Return datas [back]. data;
}
Void clear () // clear the queue
{
Front = cont = back = 0;
}
Bool full () // determine whether the condition is full
{
Return cont> 4;
}
Bool empty () // empty
{
Return cont = 0;
}
Int size ()
{
Return cont;
}
};
Int main ()
{
Cin. get ();
Return 0;
}