Priority Queue C + + array implementation

Source: Internet
Author: User

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

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.