从链接列表中删除和添加项目 (Java)
Removing and adding items from a linked list (Java)
当我尝试测试用例时,我编写的代码不起作用,但我不明白为什么。我的代码在这两种方法中。如果您需要 class 文件的其余部分,可以在此处 https://www.cs.berkeley.edu/~jrs/61b/hw/hw3/ 找到它们。
注意我编辑了节点 class 并创建了设置和获取方法。这会给我错误还是我的解决方案是错误的?
/**
* 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;
}
我认为这可能是列表长度在完成后没有得到更新的问题。测试用例可能会查找它并在您忘记这样做时打印出一条错误消息! (没有饼干!)
虽然我不确定,但这似乎是最合理的解释(查看测试文件并查看他们如何明确声明他们检查所有不变量)。
当我尝试测试用例时,我编写的代码不起作用,但我不明白为什么。我的代码在这两种方法中。如果您需要 class 文件的其余部分,可以在此处 https://www.cs.berkeley.edu/~jrs/61b/hw/hw3/ 找到它们。 注意我编辑了节点 class 并创建了设置和获取方法。这会给我错误还是我的解决方案是错误的?
/**
* 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;
}
我认为这可能是列表长度在完成后没有得到更新的问题。测试用例可能会查找它并在您忘记这样做时打印出一条错误消息! (没有饼干!)
虽然我不确定,但这似乎是最合理的解释(查看测试文件并查看他们如何明确声明他们检查所有不变量)。