在双向链表中查找最小元素不起作用 [英] Find the minimum element in a doubly linked list not working

查看:43
本文介绍了在双向链表中查找最小元素不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这段代码,以找到链表中一组数字的最小值。

I have written up this code, to find the minimum of a set of numbers within a linked list.

public DLNode<T> getMinimum() {
    if (isEmpty()) {
        return null;
    }
    DLNode<T> curr = head;
    DLNode<T> min = curr;
    T temporaryMinimum = head.getElement();
    while (curr.getElement() != null) {
        if ((Integer) temporaryMinimum < (Integer) curr.getElement()) {
            min = curr;
        }
        curr = curr.getNext();
    }
    return min;
}

我正在使用此代码块对其进行测试

I am testing it using this code block

public static void getMinElement(){
    int[] data = {2, 3, 5, 1, 6};
    //Add elements to the list from array data
    DoublyLinkedList<Integer> ll = new DoublyLinkedList<>();

    for (int i = 0; i < data.length; i++) {
        ll.AddLast(data[i]);
    }

    System.out.println("Size: " + ll.size);

    System.out.println("Minimum Element is: " + ll.getMinimum());

}

在main内这样调用:

called within main like so:

getMinElement();

它不会引发任何错误,但似乎会陷入无限循环或某种情况(判断

It does not throw up any errors, but it seems to go into an infinite loop or something (judging by how much CPU is used on my machine whenever I launch it).

我想指出,我的IDE(IntelliJ IDEA)不显示任何错误或警告。不受控制的循环或类似的东西。
在过去的几天里,我一直在对此进行推理,但没有任何运气,现在我已经失去了主意。

I would like to point out that my IDE (IntelliJ IDEA) does not show any errors or warnings of uncontrolled loops or stuff like that. I've been reasoning about it for the past few days, without any luck, and I'm now out of ideas.

任何帮助都很感激。

编辑:我的DLNode类是:

My DLNode Class is:

class DLNode<T> {
    DLNode<T> Element;
    T data;
    DLNode<T> next;
    DLNode<T> prev;

    DLNode(T data, DLNode<T> next, DLNode<T> prev) {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }

    T getElement() {
        return data;
    }
    public DLNode<T> getPrev() {
        return prev;
    }
    public void setPrev(DLNode<T> prev) {
        this.prev = prev;
    }
    public DLNode<T> getNext() {
        return next;
    }
    public void setNext(DLNode<T> next) {
        this.next = next;
    }
}


推荐答案


  1. 您的循环应检查 curr!= null 。否则,您最终可能会得到 NullPointerException

  1. Your loop should be checking if curr != null. Otherwise you will probably get NullPointerException eventually.

您的比较应该颠倒( curr.getElement()应该小于 temporaryMinimum 才能替换)。

Your comparison should be reversed (curr.getElement() should be smaller than temporaryMinimum in order to replace it).

当找到新的最小值时,应修改 temporaryMinimum

You should modify temporaryMinimum when you find a new minimum.

如果 DLNode 类应该是通用的,您不能将 T 转换为 Integer 。您可以要求 T扩展Comparable< T> ,这将允许您使用 compareTo 而不是> <

If your DLNode class is supposed to be generic, you can't cast T to Integer. You can require that T extends Comparable<T>, which would allow you to use compareTo instead of > or <.

哦,如果您仍然遇到无限循环,也许您的链表是循环的(即最后一个节点的getNext()返回第一个节点)。我无法确定是否没有看到您的 DoublyLinkedList 类。

Oh, and if you are still getting an infinite loop, perhaps your linked list is circular (i.e. getNext() of the last node returns the first node). I can't be sure without seeing your DoublyLinkedList class.

while (curr != null) {
    if (curr.getElement() != null) {
        if (temporaryMinimum.compareTo(curr.getElement()) > 0) {
            min = curr;
            temporaryMinimum = curr.getElement();
        } 
    }
    curr = curr.getNext();
}


这篇关于在双向链表中查找最小元素不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆