【问题标题】:LinkedLists, building a toString method for custom linked list classLinkedLists,构建自定义链表类的toString方法
【发布时间】:2015-05-02 07:22:29
【问题描述】:

我正在努力在我的名为 LString 的链表类中完成这个 toString() 方法

该类创建一个 LString 对象,该对象使用链表而不是数组来模仿 String 和 StringBuilder。它从链表中创建字符串。

这是该方法的代码:

public String toString(){
    StringBuilder result    = new   StringBuilder();


    node curr = front;
    while   (curr   !=  null){
        result.append(curr.data);
        curr = curr.next;

    }


    return result.toString();
}

我已经尝试了几种不同的方法,我想我已经接近解决了。但我无法从此错误消息继续前进:

Running constructor, length, toString tests (10 tests)
Starting tests: .......E.E
Time: 0.009

There were 2 failures:
1) t11aLStringOfStringToString[1](LStringTest$LStringOfStringTest)
org.junit.ComparisonFailure: LString("ab").toString() is wrong. expected:<[a]b> but was:<[]b>
        at org.junit.Assert.assertEquals(Assert.java:115)
        at LStringTest$LStringOfStringTest.t11aLStringOfStringToString(LStringTest.java:221)
        ... 10 more
2) t11aLStringOfStringToString[2](LStringTest$LStringOfStringTest)
org.junit.ComparisonFailure: LString("This is a long string.").toString() is wrong. expected:<[This is a long string].> but was:<[].>
        at org.junit.Assert.assertEquals(Assert.java:115)
        at LStringTest$LStringOfStringTest.t11aLStringOfStringToString(LStringTest.java:221)
        ... 10 more

Test Failed! (2 of 10 tests failed.)

Test failures: abandoning other phases.

LString 类使用另一个类 LStringTest.java 进行各种测试。此错误消息来自运行 LStringTest.java,但我正在处理的方法在 LString 中。它是LString("ab").toString() is wrong 位。

在某些情况下,这是我班的其余部分:

public class LString    {

     // 2. Fields
     node   front;
     //node tail;
     int size;

     // 1. Node class

    private class node {

        char data;
        node next;

        //constructors
        //1. default
        public node (){
        }

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

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


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

      for (int i =0; i < original.length(); i++) {

         this.front = new node(original.charAt(i));
      }
      this.size = original.length();

   }

    //  4.  Methods
   public int length() {
      return this.size;
   }
   public int compareTo(LString anotherLString) {
      return 0;
   }
   public boolean equals(Object other) {
      if (other == null || !(other instanceof LString)) {
         return false;
      }
      else {
         LString otherLString = (LString)other;
         return true;
      }
   }
   public char charAt(int index) {
      return 'a';
   }
   public void setCharAt(int index, char ch) {
      ch = 'a';
   }
   public LString substring(int start, int end) {
      return null;
   }
   public LString replace(int start, int end, LString lStr) {
      return null;
   }

    //append
    public void append(char data){

        this.size++;

        if  (front == null){
             front =    new node(data);
             return;
        }

        node curr = front;
        while   (curr.next != null){
             curr   = curr.next;
        }

        curr.next = new node(data);

    }

    //prepend
    public void prepend (char data){
        /*node temp = new   node(data);
        temp.next = front;
        front   = temp;*/

        front   = new   node(data, front);
        size++;
    }

    //delete
    public void delete(int index){
    //assume    that index is valid
        if  (index == 0){
             front =    front.next;
        } else {
             node   curr = front;
             for (int i = 0; i <    index   - 1; i++){
                curr = curr.next;
             }
             curr.next = curr.next.next;
        }
        size--;

    }

    //toString
    public String toString(){
        StringBuilder result    = new   StringBuilder();
        //result.append('[');

        node curr = front;
        while   (curr   !=  null){

         //result.append('[');
            result.append(curr.data);
         curr = curr.next;
         //result.append(']');
            //if (curr.next != null){

            //}

        }

        //result.append(']');
        return result.toString();
    }

    //add   (at an index)
    public void add(int index,  char data){
         if (index == 0){
              front = new node(data, front);
         }  else {
              node curr = front;
              for   (int i =    0;  i < index - 1;  i++){
                    curr = curr.next;
              }
              curr.next = new   node(data, curr.next);
         }
     }
}

【问题讨论】:

  • 您是否调试过这个程序并逐行检查了必要的部分?真的很简单..
  • toString 只是此代码中的一个问题。你的 LString 构造函数也坏了

标签: java linked-list tostring


【解决方案1】:

在 LString 构造函数中,您只需在每次循环时重新分配一个新字符到前面。当它完成构造 LString 时,它将只有原始字符串中最后一个字符的一个前端节点。

您可以在循环之外分配前端,并制作一个临时的“当前”节点来移动并分配字符。像这样的:

//This will assign the first char to the front
this.front = new node(original.charAt(0);

//Create a temporary current node
node curr = this.front;

//Since you already have the first node set up you can start i at 1
for (int i = 1; original.length(); i++) {

   //Assign the char to the next node
   curr.next = new node(original.charAt(i));

   //Change current to the next node, this way it wont just rewrite the same node
   curr = curr.next;

}

据我所知,这是主要问题,所以 toString 方法本身没有问题。

希望这会有所帮助,祝你好运。

【讨论】:

    【解决方案2】:

    我看到的最大问题是您的 LString 构造函数在为新字符串生成节点时使用了错误的节点构造函数。结果,链表节点实际上并没有相互链接。基本上你需要改变

    this.front = new node(original.charAt(i));
    

    this.front = new node(original.charAt(i), this.front);
    

    当然,您仍然会将this.front 指向最后一个字符,而不是第一个字符,因此您可能需要更改解析输入的顺序。另一个可能更好的选择是在构造函数中使用您自己的append 方法以避免代码重复。但是,按照现在的实现方式,您最终会得到一个 O(n^2) 算法,因为每个附加都会遍历整个列表。

    很明显,LString 的大部分其余部分也未实现(charAt 总是返回 'a'...),所以这不是您唯一的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-29
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      相关资源
      最近更新 更多