从链表中删除和添加项目(Java)

从链表中删除和添加项目(Java),java,linked-list,singly-linked-list,Java,Linked List,Singly Linked List,当我尝试测试用例时,我编写的代码不起作用,但我不明白为什么。我的代码在这两个方法中。如果您需要其余的类文件,可以在这里找到它们。 注意:我编辑了节点类并创建了set和get方法。这可能会给我带来错误,还是我的解决方案错了 /** * squish() takes this list and, wherever two or more consecutive items are * equals(), it removes duplicate nodes so that only one co

当我尝试测试用例时,我编写的代码不起作用,但我不明白为什么。我的代码在这两个方法中。如果您需要其余的类文件,可以在这里找到它们。 注意:我编辑了节点类并创建了set和get方法。这可能会给我带来错误,还是我的解决方案错了

/**
*  squish() takes this list and, wherever two or more consecutive items are
*  equals(), it removes duplicate nodes so that only one consecutive copy
*  remains.  Hence, no two consecutive items in this list are equals() upon
*  completion of the procedure.
*
*  After squish() executes, the list may well be shorter than when squish()
*  began.  No extra items are added to make up for those removed.
*
*  For example, if the input list is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ], the
*  output list is [ 0 1 0 3 1 0 ].
*
*  IMPORTANT:  Be sure you use the equals() method, and not the "=="
*  operator, to compare items.
**/

public void squish() {
// Fill in your solution here.  (Ours is eleven lines long.)
  SListNode previous = head;
  SListNode next;
  SListNode root = head;
  int x = 1;

  for (int counter = 0; counter < size; counter++)
  {
      next = previous.getNext();
      if (!previous.equals(next))
      {
          root.setNext(next);
          root = next;
          x++;
      }
      previous = previous.getNext();

  }
  size = x;
}

/**
*  twin() takes this list and doubles its length by replacing each node
*  with two consecutive nodes referencing the same item.
*
*  For example, if the input list is [ 3 7 4 2 2 ], the
*  output list is [ 3 3 7 7 4 4 2 2 2 2 ].
*
*  IMPORTANT:  Do not try to make new copies of the items themselves.
*  Make new SListNodes, but just copy the references to the items.
**/

public void twin() {
 // Fill in your solution here.  (Ours is seven lines long.)
 SListNode previous = head;
 SListNode next;

 for (int counter = 0; counter < size; counter++)
 {
     next = previous.getNext();
     previous.setNext(previous);
     previous = previous.getNext();
     previous.setNext(next);
 }
 size = 2*size;
}
/**
*squish()获取此列表,并在有两个或多个连续项的位置
*equals(),它将删除重复的节点,以便只保留一个连续副本
*仍然存在。因此,此列表中没有两个连续项在
*完成程序。
*
*执行squish()后,列表可能会比squish()时短
*开始。不添加额外的项目来弥补删除的项目。
*
*例如,如果输入列表为[0 0 0 1 0 0 3 3 3 1 1 0],则
*输出列表为[0 1 0 3 1 0]。
*
*重要提示:请确保使用equals()方法,而不是“==”
*运算符,以比较项目。
**/
公共空间挤压(){
//在这里填写你的答案。(我们的答案是十一行。)
SListNode previous=头部;
滑动节点下一步;
滑动节点根=头;
int x=1;
用于(int计数器=0;计数器<大小;计数器++)
{
next=previous.getNext();
如果(!previous.equals(next))
{
root.setNext(下一个);
根=下一个;
x++;
}
previous=previous.getNext();
}
尺寸=x;
}
/**
*twin()接受此列表并通过替换每个节点将其长度加倍
*两个连续的节点引用同一项。
*
*例如,如果输入列表为[3 7 4 2],则
*输出列表为[3 3 7 4 2 2]。
*
*重要提示:不要尝试制作项目本身的新副本。
*创建新的SListNodes,但只需将引用复制到项目。
**/
公共图书馆{
//在这里填写你的答案。(我们的答案是七行。)
SListNode previous=头部;
滑动节点下一步;
用于(int计数器=0;计数器<大小;计数器++)
{
next=previous.getNext();
上一个。设置下一个(上一个);
previous=previous.getNext();
上一个。设置下一个(下一个);
}
尺寸=2*尺寸;
}

我认为这可能是列表长度上的一个问题,完成后无法更新。测试用例可能会在您忘记执行此操作时查找并打印错误消息!(没有饼干!)


虽然我不确定,但这似乎是最合理的解释(看看测试文件,看看他们是如何明确说明他们检查了所有不变量的)。

谢谢!我不想要一个解决方案。这不是为了上学,但我只是在练习,如果你给了我解决办法,我练习就没有意义了。经过再三考虑,我想我发布得太匆忙了。逻辑应该可以工作-我正在检查类文件,看看这是否是由其他原因造成的。我添加的集合和get看起来像是public SListNode getNext(){return next;},public void setNext(SListNode inNext){next=inNext;},这看起来并不奇怪。嗯,你能把你得到的输出分享给一个示例输入吗?啊!我刚刚意识到-我想可能有一个清单大小检查完成后,你没有更新!我更新了我的答案。不,我只是想在业余时间学点东西。。。谢谢你是一个很大的帮助,这页上的2个方法中的代码是错误的。其余的都不是。我已经提过了。还有为什么你是个混蛋。这是学年的结束。如果是的话,它不会被编号为家庭作业3。我不是想作弊或诸如此类的事情。实际上,是我在业余时间试图学习一些东西。在这里,为你自己寻找。这门课程对每个人都是在线的,我想现在有不同的老师