问题描述:

I have tired coding the normal priority. Now i would want to add in 1 or 2 more conditions into my code.

Here's my current normal priority code:

void queue::addToQueueList(int newPriority, double newFare, int custID)

{

node* newnode= new node;

newnode->priority= newPriority;

newnode->fare = newFare;

newnode->cusID = custID;

newnode->next= NULL;

if (front == NULL || newnode->priority < front->priority)

{

newnode->next = front;

front = newnode;

}

else

{

node* q = front;

node* p;

while (q->next != NULL && q->next->priority <= newnode->priority)

{

q=q->next;

}

newnode->next = q->next;

q->next = newnode;

}

}

how can i add in addition condition example if found priority same, compare the fare. the highest of the fare will be priority.

Thanks

网友答案:

If I have correctly understood you need the following

#include <utility>

//...

void queue::addToQueueList(int newPriority, double newFare, int custID)
{
    node* newnode= new node;
    newnode->priority= newPriority;
    newnode->fare = newFare;
    newnode->cusID = custID;
    newnode->next= NULL;

    auto p = std::make_pair( newPriority, newFare );

    if (front == NULL || p < std::make_pair( front->priority, front->fare ) )
    {

                newnode->next = front;

                front = newnode;

    }
    else
    {

                node* q = front;
                while (q->next != NULL && 
                       std::make_pair( q->next->priority, q->next->fare ) <= p)
        {   

            q=q->next;
        }


            newnode->next = q->next;
            q->next = newnode;



    }
}

Also it seems that statement

        node* p;

may be removed from the function.

网友答案:

This is what i did:

void queue::addToQueueList(int newPriority, double newFare, int custID)
{
    node* newnode= new node;
    newnode->priority= newPriority;
    newnode->fare = newFare;
    newnode->cusID = custID;
    newnode->next= NULL;

    if (front == NULL || newnode->priority < front->priority)
    {

                newnode->next = front;

                front = newnode;

    }
    else
    {

                node* q = front;
        node* p;
                while (q->next != NULL && q->next->priority <= newnode->priority)
        {   
            p=q;
            q=q->next;
        }

            bool chk = compare(q,newnode);
        if(chk)
        {
            p->next = newnode;
            newnode->next = q;

        }
        else
        {
            newnode->next = q->next;
            q->next = newnode;
        }       


    }
}
bool queue::compare(node* const& n1, node* const& n2)
{
    bool check = false;
    if(n1->priority == n2->priority)
    {
        if(n2->fare > n1->fare)
        {
            check = true;
        }
    }
    return check;
}
相关阅读:
Top