簡體   English   中英

優先隊列排序不正確

[英]Priority Queue not sorting in right order

因此,我試圖建立一個包含2個數字(0和2)的優先級隊列。 優先級0的值將根據到達時間添加到隊列的末尾。 優先級2將在優先級0數據包之前插入,但不能在已經排隊的優先級數據包之前插入。 請不要使用std :: priority_queue。 使用鏈接列表也是一個要求。有人可以指出我在做什么錯嗎? 樣本包(優先考慮):

55555555555555D507E34B17887100A0FF1822198100 0 100000663727970746fC704DD7B

 pQueue100::pQueue100(){
    front = NULL;
}

void pQueue100::insert(string packet, int priority)
{
    node2 *temp, *q;
    temp = new node2;
    temp ->info= packet;
    temp ->priority = priority;
    if (front == NULL ||priority <front->priority)
    {
        temp->link =front;
        front = temp;
    }else 
    {
        q = front;
        while (q->link != NULL && q->link->priority<=priority)
            q=q->link;
        temp->link = q->link;
        q->link=temp;
    }

    //display();
}//insert

void pQueue100::display()
{
    node2 *ptr;
    ptr = front;
    //node2 *temp;
    //temp = front;

    if (front ==NULL)
        cout << "Queue is empty" <<endl;
    else 
    {
        while (ptr != NULL)
        {
            ptr->priority;
            //temp->info;
            cout << ptr->priority << "|" ;
            cout << ptr->info <<endl;
            ptr = ptr->link;

        }
    }
}

如果你們想看看數據包來自哪里:

void thr1::selectPQueue(string packet){ //reads in packets from FIFO Queue and sorts it based on VLAN Tag and priority
    pQueue100 p100;
//  cout <<packet <<endl;//print packets for testing

    if (packet.substr(45,3) == "100"){ //if the packet matches VLAN 100....

        if (packet.substr(44,1) == "0"){
            priorityCode = 0;
        }else if (packet.substr(44,1) == "2"){
            notif = "!!!!!!!!!Priority Packet Packet Found: <..." + packet.substr(16,11) + "...> !!!!!!!!!";
            priorityCode = 2;
            packetCopy = packet;
            //thr2.interupptHandler(packetCopy, notif);
        }else
            priorityCode = priorityCode;

        p100.insert(packet, priorityCode);


    }//end VLAN 100

    else if (packet.substr(45,3) =="101") //if the packet matches VLAN 101
    {


        if (packet.substr(44,1) == "0"){
            priorityCode = 0;
        }else if (packet.substr(44,1) == "2"){
            notif = "!!!!!!!!!Priority Packet Packet Found: <..." + packet.substr(16,11) + "...> !!!!!!!!!";
            priorityCode = 2;
            packetCopy = packet;
            //thr2.interupptHandler(packetCopy, notif);

        }else
            priorityCode = priorityCode;
        //pQ101().recieveInterrupts(packet, priority,);

    }//end VLAN 101

    //p100.display();

反轉使用的比較運算符很簡單。

void pQueue100::insert(string packet, int priority)
{
    node2 *temp, *q;
    temp = new node2;
    temp ->info= packet;
    temp ->priority = priority;
    if (front == NULL ||priority > front->priority)
    {
        temp->link =front;
        front = temp;
    }else 
    {
        q = front;
        while (q->link != NULL && q->link->priority >= priority)
            q=q->link;
        temp->link = q->link;
        q->link=temp;
    }
}

驅動程序代碼:

int main() 
{
    pQueue100 q;
    q.insert("alpha", 0);
    q.insert("beta", 2);
    q.insert("gamma", 0);
    q.insert("delta", 2);
    q.insert("epsilon", 0);
    q.insert("zeta", 2);
    // correct display sequence should be:
    // beta, delta, zeta, alpha, gamma, epsilon
    q.display();
}

輸出:

2|beta
2|delta
2|zeta
0|alpha
0|gamma
0|epsilon

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM