简体   繁体   English

在双向链表Java中删除具有最小值的通用类型节点

[英]Removing a generic type node with the smallest value in a doubly linked list Java

This is the code I have for my getSmallest() method: 这是我的getSmallest()方法的代码:

public T getSmallest() throws EmptyListException
{
    if(isEmpty())
        throw new EmptyListException("List is empty");


    DLNode<T> current = front;
    DLNode<T> minNode = current;
    int minimum = current.getValue();

    while(current.getNext() != null)
    {
        if(minimum > current.getValue())
        {
            minNode = current;
            minimum = current.getValue();
        }

        current = current.getNext();    
    }

    return current.getData();
}

Each node has a String called dataItem and an integer called value associated with it. 每个节点都有一个名为dataItem的字符串和一个与之关联的称为值的整数。 I want to see which node has the smallest value and then return the dataItem. 我想查看哪个节点的值最小,然后返回dataItem。 The problem is that I'm stuck in the while loop and don't know why. 问题是我陷入了while循环中,不知道为什么。 How would I properly traverse the list so that I don't get stuck in the while loop and can compare the minimum values? 如何正确遍历列表,以免卡在while循环中并可以比较最小值?

As you've seen, you can't overload operators in Java, and > will only apply to numeric datatypes. 如您所见,您不能在Java中重载运算符,并且>仅适用于数字数据类型。

The generic solution for this is to have T extends Comparable<T> and use its compareTo method: 通用的解决方案是让T extends Comparable<T>并使用其compareTo方法:

DLNode<T> current = front;
DLNode<T> minNode = current;
T minimum = current.getValue();

while (current.getNext() != null) {
    if (minimum.compareTo(current.getValue()) > 0) {
        minNode = current;
        minimum = current.getValue();
    }

    current = current.getNext();    
}

return current.getData();

(alternatively, if T isn't a Comparable , you could supply a custom Comparator and use it in a similar fashion). (或者,如果T不是Comparable ,则可以提供自定义Comparator并以类似的方式使用它)。

The question is: why is the loop termination condition never reached? 问题是:为什么从未达到循环终止条件?

As a doubly-linked list, does your list connect the last element to the first element? 作为双向链接列表,您的列表是否将最后一个元素连接到第一个元素? Will getNext() ever answer null? getNext()回答null吗?

Also, there are problems in the loop as written. 同样,在编写的循环中也存在问题。 See the corrected code, below. 请参阅下面的更正代码。 The problem of loop termination is probably not fixed by this update. 此更新可能无法解决循环终止的问题。

public T getSmallest() throws EmptyListException {
    if ( isEmpty() ) {
        throw new EmptyListException("List is empty");
    }

    DLNode<T> currentNode = front;

    int minValue = currentNode.getValue();
    DLNode<T> minNode = currentNode;

    while ( (currentNode = currentNode.getNext()) != null ) {
        int nextValue = currentNode.getValue();
        if ( nextValue < minValue ) {
            minNode = currentNode;
            minValue = nextValue;
        }
    }

    return minNode.getData();
}

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

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