Priority queue C ++ array implementation, priority queue c Array

Source: Internet
Author: User

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;
}

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.