Steven Meow De Claude
Steven Meow De Claude

Reputation: 380

c++ Linked List with priority queue

I trying to code out the Linked List with priority queue and i encountered some problem.

I have about 7 priority from 1 the most to 7 the least important.

here's my current insert method.

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 (isempty())
    {
        front = back = newnode;
    }
    else
    {
        node* temp = front;
        if(newnode->priority < temp->priority)
        {
            newnode->next = front;
            front = newnode;
        }
        else
        {
            while(newnode->priority < temp->priority)
            {
                if(temp->next == NULL)
                {
                    break;
                    temp = temp->next;
                }
            }
            if(temp->next == NULL && newnode->priority < temp->priority)
            {
                back->next = newnode;
                back = newnode;
            }
            else
            {
                newnode->next = temp->next;
                temp->next = newnode;
            }
        }
    }
}

Invoked as:

qList->addToQueueList(2, 488.88, A);
qList->addToQueueList(1, 388.88, B);
qList->addToQueueList(3, 488.88, C);

Expected result should be :

B, A, C

THe result shows :

B, C, A

Upvotes: 0

Views: 2848

Answers (2)

WhozCraig
WhozCraig

Reputation: 66194

Your making this considerably harder than it needs to be. Ultimately you need to walk the list, find the insertion point, remember how you arrived at that insertion point, and wire both your fore and aft pointers appropriately. Also a priority queue has no reason to keep a "back" pointer, so I'm not sure why you have one.

There are a number of ways to do this. First, to make the code cleaner to understand, providing a proper parameterized constructor for node is both trivial and helpful:

struct node
{
    int priority;
    double fare;
    int cusID;
    node *next;

    node(int p, double f, int id, node *nxt = nullptr)
        : priority(p), fare(f), cusID(id), next(nxt)
    {
    }
};

One you have that, you can go down the road you were apparently trying to navigate, using a pointer-value list walking approach. To do that you need to maintain a previous pointer:

void queue::addToQueueList(int newPriority, double newFare, int custID)
{
    node* temp = front, *prev = NULL;
    while (temp && temp->priority < newPriority)
    {
        prev = temp;         // remember how we got here
        temp = temp->next;   // advance to next node
    }

    // create new node, linking to temp
    node *newnode = new node(newPriority, newFair, custID, temp);

    // link to previous node or assign as new head, whichever is needed
    if (prev != nullptr)
        prev->next = newnode;
    else
        head = newnode;

    // though there is no need for a back pointer in a priority queue
    //  you had one none-the-less, so....
    if (!temp)
        back = newnode;
}

it is worth noting that this algorithm will insert new arrivals with similar priority at the head of that priority section of the list. I.e. the newest arrivals for a given priority are always at the forefront of that priority's position in the queue. If you want the oldest arrivals of a given priority to be "ahead" of their brethren, you simply need to change this:

while (temp && temp->priority < newPriority)

to this:

while (temp && temp->priority <= newPriority)  // note < is now <=

Best of luck.

Upvotes: 2

Knowleech
Knowleech

Reputation: 1055

The comparison in your while loop is wrong. When inserting C newnode->priority == 3 and temp(B)->priority == 1. Thus the while loop is never entered.

Also, the temp = temp->next inside the while loop should be outside (after) the if statement. Otherwise this will be an infinite loop.

Assuming you are correcting these: you will always insert the new element after temp. Be aware of this in your fix of your comparisons. You are likely to add comparisons with temp->next->priority as well.

I agree with Joachim in the comments: step through the code with a debugger. Then you can see the values of the variables and which comparisons produce which results.

Upvotes: 0

Related Questions