Writing an equals() method for linked list object

血红的双手。 提交于 2020-01-17 04:27:11

问题


I'm having trouble with an equals(Object other) method within a class I'm building called LString. The class contains an object called LString that builds strings out of linked lists, and a few other methods. I'm working on two methods, compareTo() and equals().

The class is tested by running another file, called LStringTest, which outputs this error message:

Running compareTo and equals tests (18 tests)
Starting tests: .E.........E...E.E
Time: 0.426

There were 4 failures:
1) t22aTestEquals[0](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
2) t22aTestEquals[5](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
3) t22aTestEquals[7](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
4) t22aTestEquals[8](LStringTest$LStringCompareToTest)
java.lang.Exception: test timed out after 100 milliseconds
        at LString.equals(LString.java:104)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more

Test Failed! (4 of 18 tests failed.)

Test failures: abandoning other phases.

I believe I have compareTo() and equals() correct, and I can't find the source of the mistake.

Here is my code:

import java.io.*;
import java.util.*;

public class LString    {

     node   front;
     int size;

    //Creating a node class
     private    class   node {
          char data;
          node next;

          public    node (){
          }

          public    node    (char   newData){
                this.data = newData;
          }

          public    node    (char   newData,    node newNext){
                this.data = newData;
                this.next = newNext;
          }


     }
    //Constructors
     public LString(){
          this.size =   0;
          this.front =  null;
     }
     public LString(String original)    {
          this.size = original.length();
          if (original.length() > 0){

              this.front =  new node(original.charAt(0));
              node curr = this.front;

              for   (int i =1; i <  original.length(); i++) {
                    curr.next = new node(original.charAt(i));
                    curr = curr.next;
              }
          }



     }

   // Length method, returns the length of LString
     public int length()    {
        return this.size;
    }
    // compareTo method, compares this LString to anotherLString, returns 0 if equal,
   // -1 if lexicogrpahically less, and 1 if lexicographically greater
    public int compareTo(LString anotherLString)    {
      int len1 = length();
      int len2 = anotherLString.length();
      int lim = Math.min(len1, len2);

      node cn1 = front;
      node cn2 = anotherLString.front;

      int k = 0;
      while (k < lim) {
         char c1 = cn1.data;
         char c2 = cn2.data;
         if (c1 != c2) {
            return c1-c2;
         }
         k++;
         cn1 = cn1.next;
         cn2 = cn2.next;
      }
      return len1 - len2;
    }
   // a boolean equals method that returns true if LString and other are the same, false if not
    public boolean  equals(Object other)    {
      if (this == other) {
         return true;
      }
        if  (other instanceof LString) {
         LString otherLString   = (LString)other;
         int n = length();
         if (n == otherLString.length()) {
            node n1 = front;
            node n2 = otherLString.front;
            while (n1 != null) {
               if (n1.data != n2.data) {
                  return false;
               }
            }
            return true;
         }
      }

      return false;
   }
}

Thanks for any advice, trying to learn java.


回答1:


Your equals method contains an infinite loop. Your while-loop checks to see if n1 != null, but nowhere in the loop do you change the value of n1:

    while (n1 != null) {
        if (n1.data != n2.data) {
            return false;
        }
    }

You need to advance the nodes down the list after you check each value:

while (n1 != null) {
    if (n1.data != n2.data) {
        return false;
    }
    n1 = n1.next;
    n2 = n2.next;
}


来源:https://stackoverflow.com/questions/28844026/writing-an-equals-method-for-linked-list-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!