简体   繁体   English

具有优先级队列的c ++链表

[英]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. 我有7个优先级,从1个最重要到7个最不重要。

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

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: 首先,为了使代码更易于理解,为node提供适当的参数化构造函数既简单又有用:

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. 值得注意的是,该算法将在列表的该优先级部分的开头插入优先级相近的新到达信息。 Ie 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. 祝你好运。

The comparison in your while loop is wrong. while循环中的比较是错误的。 When inserting C newnode->priority == 3 and temp(B)->priority == 1 . 当插入C newnode->priority == 3temp(B)->priority == 1 Thus the while loop is never entered. 因此,永远不会进入while循环。

Also, the temp = temp->next inside the while loop should be outside (after) the if statement. 另外,while循环内的temp = temp->next应该位于if语句之外(之后)。 Otherwise this will be an infinite loop. 否则,这将是一个无限循环。

Assuming you are correcting these: you will always insert the new element after temp. 假设您要纠正这些问题:您将始终 temp 之后插入新元素。 Be aware of this in your fix of your comparisons. 在进行比较时应注意这一点。 You are likely to add comparisons with temp->next->priority as well. 您可能还会添加带有temp->next->priority比较。

I agree with Joachim in the comments: step through the code with a debugger. 我在评论中同意Joachim的观点:使用调试器逐步调试代码。 Then you can see the values of the variables and which comparisons produce which results. 然后,您可以查看变量的值以及哪些比较产生哪些结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM