简体   繁体   English

如何在队列的链表实现中找到最小元素的前任?

[英]How to find a predecessor of a minimum element in a linked list implementation of a queue?

so i want to find a predecessor of a minimum element in a queue.所以我想在队列中找到最小元素的前身。 I have used a linked list implementation of a queue.我使用了队列的链表实现。 I have been able to find a minimum element in a queue, but i have no idea how to find a predecessor of a minimum element.我已经能够在队列中找到最小元素,但我不知道如何找到最小元素的前身。 For example lets say a queue consits of elements like: [5 3 1 4].例如,假设一个队列由以下元素组成:[5 3 1 4]。 I need to find the element 3, as it is the predecessor of the minimum element which in this case is 1. Thanks in advance for any help.我需要找到元素 3,因为它是最小元素的前身,在这种情况下是 1。在此先感谢您的帮助。

Here's what I have done so far:这是我到目前为止所做的:

template <typename InfoType>
void LQueue <InfoType>::MinElementP() {
    Node *min = head;
    Node *h = head;
        
    while (h != 0){
        if (min->info > h->info){
            min->info = h->info;
        }
        h = h->next;
    }
    
    cout << "Minimum element (the smallest element) in this queue is : " << min->info << "\n";
}

I got it working now, i initialized a new pointer that points to the predecessor, just before the h= h->next;我现在开始工作了,我初始化了一个指向前任的新指针,就在h= h->next;之前statement, so for example predecessor = h;声明,例如predecessor = h;

What type of variables have you created in node ?您在node中创建了什么类型的变量? If it has a key in ascending order to keep track of your nodes, then you can get the key of predecessor by subtracting one from key of node with minimum value .如果它有一个按升序排列的key来跟踪您的节点,那么您可以通过从key of node with minimum value的键中减去一个来获得前任的键。 Or you could create a double linked link list which points to previous element as well by making a pointer named previous in node type.或者,您可以通过在node类型中创建一个名为previous的指针来创建一个指向前一个元素的双链链接列表。

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

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