简体   繁体   English

Java双链表–两个相邻节点相等

[英]Java doubly linked list — equality of two adjacent nodes

A public method twoTogether() for the class MyList returns True, if and only if the list has two adjacent elements that are equal. 当且仅当列表具有两个相等的相邻元素时,类MyList的公共方法twoTogether()才会返回True。 You can assume that no list element (data) is null. 您可以假定没有列表元素(数据)为空。 Here are some examples: the list [a,b,c,d], would return false when calling this method. 下面是一些示例:列表[a,b,c,d]在调用此方法时将返回false。 But a list [a,b,b,c] or [a,b,c,d,e,f,f]. 但是列表[a,b,b,c]或[a,b,c,d,e,f,f]。 The method returns true. 该方法返回true。 Write the public method twoTogether. 共同编写公共方法twoTogether。 You can use the List interface references (fields: data, prev, next)(head,tail)(size) etc. Here is the code I have written: 您可以使用List接口引用(字段:数据,上一个,下一个)(头,尾)(大小)等。这是我编写的代码:

 public boolean twoTogether(){
      currentNode = head;
      while (currentNode.hasNext()){ 
            if (currentNode.data != currentNode.next.data){ 
                  currentNode = currentNode.next;
            }
            else if (currentNode.data == currentNode.next.data){
                 return True;
            }
      }
      return False;
  }

Would this code correctly traverse a list testing for equality between two nodes? 这段代码会正确地遍历两个节点之间是否相等的列表测试吗? Am I implementing hasNext() correctly also? 我也正确实现hasNext()吗? I was having a hard time figuring out how not to terminate the loop, or return true for the nested if, then false for the else statement. 我很难弄清楚如何不终止循环,或者对于嵌套的if返回true,对于else语句返回false。

You're right, the loop termination isn't exactly right. 您是对的,循环终止并不完全正确。 The first two branches within the while loop doesn't belong. while循环中的前两个分支不属于。 One of the first two branches will always fire, as != and == are complements. 前两个分支之一将始终触发,因为!=和==是补码。 The return false statement belongs outside of the while loop, indicating the entire list has been traversed and no equal adjacent nodes. return false语句属于while循环之外,指示遍历了整个列表,并且没有相等的相邻节点。

public boolean twoTogether(){
    currentNode = head;
    while (currentNode.hasNext()){ 
        if (currentNode.data != currentNode.next.data){ 
            currentNode = currentNode.next;
        }
        else if (currentNode.data == currentNode.next.data){
            return True;
        }
    }
    return False;
}

the use of hasNext() is perfect! hasNext()的使用非常完美! never want to over-traverse a list... hope this helps! 从不希望遍历列表...希望对您有所帮助!

The basic idea is as follows: 基本思想如下:

  • Step one, check if the list is empty. 第一步,检查列表是否为空。 If that's the case then, by definition, there can be no duplicates, so return false and stop. 如果是这种情况,那么根据定义,就不可能有重复项,因此返回false并停止。

    This prevents you from trying to evaluate the following element when the current one does not exist). 这样可以防止您在当前元素不存在时尝试评估以下元素。

    Otherwise, start with the current element being the first in the list. 否则,请以当前元素作为列表中的第一个元素开始。

  • Step two, you need to carry on as long as there is at least one more following the current element (since you want to check current against next). 第二步,只要当前元素之后至少还有一个,就需要继续进行操作(因为要对照下一个检查当前值)。

    If there is no following element then you've finished the list and no duplicates were found, so immediately return false (hence stopping). 如果没有后续元素,则您已经完成了列表,没有找到重复项,因此请立即返回false (因此停止)。

  • Step three, you now know there is both current and next, so actually check current against next. 第三步,您现在知道当前和下一个,因此实际检查下一个的当前值。

    If their data is identical, you've found a duplicate and you can immediately return true (hence stopping). 如果它们的数据相同,那么您将找到一个重复项,并且可以立即返回true (因此停止)。

  • Step four, there's no duplicate at this point but there may be one further on, so advance to the next element and go back to step two above for rechecking whether you've reached the end. 第四步,有在一点没有重复,但有可能是一个进一步的,所以前进到下一个元素,并返回到第二步以上再次检查您是否已经走到了尽头。

The pseudo-code for this would be (passing in head as the argument): 为此的伪代码为(将head作为参数传递):

def twoTogether (node):
    if node == null:                     # empty means no dupes possible
        return false

    while node.next != null:             # loop through list
        if node.data == node.next.data:  # check current against next
            return true
        node = node.next                 # advance to next

    return false                         # no dupes before list end

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

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