简体   繁体   English

在列表java中找到最大项目

[英]find Max Item on list java

I need help with my homework我的家庭作业需要帮助

My question is: How to complete the method findMaxItem below to return a pointer to the largest element of the linked list given by the parameter ptr, or return null if the list is empty.我的问题是:如何完成下面的findMaxItem方法,返回一个指向参数ptr给出的链表最大元素的指针,或者如果链表为空则返回null。 The solution must be a recursive one.解决方案必须是递归的。

This my my answer but is not correct:这是我的答案,但不正确:

public static Node findMaxItem (Node ptr) {
    if (ptr == null) {
        return null;

    } else {
        if (ptr.next.data > ptr.data) {
            Node n = ptr.next;


            if (ptr.next.data >= n.data) {

                n = ptr.next;
                //ptr = n.next;
                findMaxItem(n);
            } else {

                ptr = ptr.next;
                findMaxItem(ptr);
            }
            ptr = n.next;
            findMaxItem(ptr);
            ptr = n;
        }
    //   ptr = ptr.next;
     //  findMaxItem(ptr);
    }
    return ptr;
}

Thanks for help everyone :)谢谢大家的帮助:)

You've at least got the first part right: if the list is empty, return null .至少第一部分是正确的:如果列表为空,则返回null This is the base case of the recursion.这是递归的基本情况。

    if (ptr == null) return null;

You're meant to use recursion for the rest of the cases.您打算在其余情况下使用递归。 But how?但是如何?

If the list is not empty, you can divide it into two parts: the first item, and the rest of the list.如果列表不为空,您可以将其分为两部分:第一项和列表的其余部分。 Would it help you "find the max item" in the rest of the list?它会帮助您在列表的其余部分“找到最大项目”吗?

    Node restOfTheList = ptr.next;
    Node maxOfRestOfTheList = findMax(restOfTheList);

Now you have the first item, referenced by ptr , and the item with the greatest value in the rest of the list.现在您有了ptr引用的第一个项目,以及列表其余部分中具有最大价值的项目。 If you compare the data in those, you can figure out which has the greatest value.如果你比较这些数据,你可以找出哪个具有最大的价值。 And which ever it is, that is the node with the largest item.无论是哪个,都是具有最大项目的节点。 Does that make sense?那有意义吗?

    if data of "ptr" is greater than data of "maxOfRestOfTheList": return ptr
    else: return maxOfRestOfTheList;

You'll also have to deal with the case where there is only one item in the list.您还必须处理列表中只有一项的情况。 In that situtation restOfTheList is empty.在这种情况下, restOfTheList是空的。

One possible solution is to create a method that receive the array with the values -1 and the last value.一种可能的解决方案是创建一个方法来接收具有值 -1 和最后一个值的数组。 Then check if the next last element is greater that the one you have or not, if it is, the new value is the current maximum remove it and call the same method by using the new array (with the removed value) and the current maximum.然后检查下一个最后一个元素是否大于您拥有的元素,如果是,则新值是当前最大值删除它并使用新数组(带有删除的值)和当前最大值调用相同的方法.

The base case will be when there is only one element in the array, take or compare it with the last maximum value you passed to the recuerdo be función and return the greater value of these two.基本情况是当数组中只有一个元素时,将它与传递给 recuerdo be función 的最后一个最大值进行比较或比较,并返回这两个值中的较大值。 This last will be the maximum in a recursive way.这最后将是递归方式的最大值。

I have no possibility to write the code right now but in meta-code it should be something like this我现在无法编写代码,但在元代码中它应该是这样的

private int finalMax
public void maxRecursive(arrayList<int>List, Int tempMax){
  //Base case
   Int newTempMax = tempMax
    If (tempMax >list.get(list.size-1)){
       newTempMax=list.get(list.size()-1)
    }
    If (list.size()>1){
      List.remove(list.size()-1)
      maxRecursive(list, newTemMax)
    }else{
       finalMax=newTempMax
    }
 }

As I said this is nothing more than pseudo-code that looks adapted to java, I cannot son anything better right now but AI think that you can get the Idea from here.正如我所说,这只不过是看起来适用于 Java 的伪代码,我现在不能说任何更好的东西,但 AI 认为你可以从这里获得 Idea。

Best!最好的事物!

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

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