Java LinkedList删除测试失败 public void delete(int索引){ 如果(索引计数-1){ 将新的ArrayIndexOutOfBoundsException()抛出; } 节点电流=头; 如果(索引==0){ current=current.getLink(); 计数--; 返回; } 对于(int i=0;iadios->ciao->see ya。但是你的实现是相反的。see ya->ciao->adios->bye->helloyes这是有意的谢谢,这是有道理的,你是第一个所以我会给你支票谢谢,请阅读关于你的添加例程的部分。它是reve正如你所发布的一样,rsed。是的,把它倒过来才是重点。好主意哦,抱歉,哈哈。我发布了代码,以便在你需要时按顺序添加。祝你好运@user3427042。欢迎你。但你的问题还没有结束。我对下一个for()循环有点怀疑,因为它在index=1时不起作用。for()循环循环被跳过。@anonymous在这种情况下应该跳过for循环。这也让我有一秒钟的时间。写例程imho只是一种奇怪的方式。@williammerrison我怀疑他在删除最后一个节点时会得到NullPointerException。@anonymous他不会。如果有超过1个节点,他的例程会得到最后一个节点之前的节点。t他的意思是,当删除系列中的最后一个节点时,下一个链接将永远不会为空,因为您实际上将操作倒数第二个节点。我在回答中重写了例程。这对我个人来说更容易阅读(见图,我写的)。@Williamorrison Yep,您是对的。您的代码肯定更容易阅读。 if (index == 0) { //we're deleting the head here. You need to reassign head variable. //current = current.getLink(); <-- no. this doesn't change the structure. head = head.getLink();//here we set head to head's next. Now structure is changed. count--; return; } list.add(1); list.add(2); list.add(3); public void add(X v) { Node<X> a = new Node<X>(); a.setValue(v); if(head==null) head = a; else { Node<X> tail = getTail(); tail.setLink(a); } count++; } private Node<X> getTail(){ if(head==null) return null; Node<X> current = head; while(current.getLink()!=null) current = current.getLink(); return current; } public void delete(int index) { if (index < 0 || index > count-1) { throw new ArrayIndexOutOfBoundsException(); } if (index == 0) { head = head.getLink(); }else{ Node<X> beforeDeletedNode = head; for (int i = 0; i < index-1; i++) { beforeDeletedNode = beforeDeletedNode.getLink(); } Node<X> toDelete = beforeDeletedNode.getLink(); beforeDeletedNode.setLink(toDelete.getLink()); } count--; }

Java LinkedList删除测试失败 public void delete(int索引){ 如果(索引计数-1){ 将新的ArrayIndexOutOfBoundsException()抛出; } 节点电流=头; 如果(索引==0){ current=current.getLink(); 计数--; 返回; } 对于(int i=0;iadios->ciao->see ya。但是你的实现是相反的。see ya->ciao->adios->bye->helloyes这是有意的谢谢,这是有道理的,你是第一个所以我会给你支票谢谢,请阅读关于你的添加例程的部分。它是reve正如你所发布的一样,rsed。是的,把它倒过来才是重点。好主意哦,抱歉,哈哈。我发布了代码,以便在你需要时按顺序添加。祝你好运@user3427042。欢迎你。但你的问题还没有结束。我对下一个for()循环有点怀疑,因为它在index=1时不起作用。for()循环循环被跳过。@anonymous在这种情况下应该跳过for循环。这也让我有一秒钟的时间。写例程imho只是一种奇怪的方式。@williammerrison我怀疑他在删除最后一个节点时会得到NullPointerException。@anonymous他不会。如果有超过1个节点,他的例程会得到最后一个节点之前的节点。t他的意思是,当删除系列中的最后一个节点时,下一个链接将永远不会为空,因为您实际上将操作倒数第二个节点。我在回答中重写了例程。这对我个人来说更容易阅读(见图,我写的)。@Williamorrison Yep,您是对的。您的代码肯定更容易阅读。 if (index == 0) { //we're deleting the head here. You need to reassign head variable. //current = current.getLink(); <-- no. this doesn't change the structure. head = head.getLink();//here we set head to head's next. Now structure is changed. count--; return; } list.add(1); list.add(2); list.add(3); public void add(X v) { Node<X> a = new Node<X>(); a.setValue(v); if(head==null) head = a; else { Node<X> tail = getTail(); tail.setLink(a); } count++; } private Node<X> getTail(){ if(head==null) return null; Node<X> current = head; while(current.getLink()!=null) current = current.getLink(); return current; } public void delete(int index) { if (index < 0 || index > count-1) { throw new ArrayIndexOutOfBoundsException(); } if (index == 0) { head = head.getLink(); }else{ Node<X> beforeDeletedNode = head; for (int i = 0; i < index-1; i++) { beforeDeletedNode = beforeDeletedNode.getLink(); } Node<X> toDelete = beforeDeletedNode.getLink(); beforeDeletedNode.setLink(toDelete.getLink()); } count--; },java,list,hyperlink,Java,List,Hyperlink,@测试 public void testdeleteThrough() { LList b=新的LList(); b、 加上(“你好”); b、 添加(“再见”); b、 添加(“再见”); b、 添加(“ciao”); b、 加上(“见你”); b、 删除(0); 资产质量(4,b.大小()); 资产质量(“ciao”,b.get(0)); 资产质量(“再见”,b.get(1)); 资产质量(“再见”,b.get(2)); assertEquals(“你好”,b.get(3)); publ

@测试
public void testdeleteThrough()
{
LList b=新的LList();
b、 加上(“你好”);
b、 添加(“再见”);
b、 添加(“再见”);
b、 添加(“ciao”);
b、 加上(“见你”);
b、 删除(0);
资产质量(4,b.大小());
资产质量(“ciao”,b.get(0));
资产质量(“再见”,b.get(1));
资产质量(“再见”,b.get(2));
assertEquals(“你好”,b.get(3));

public void add(X v)
{
节点a=新节点();
a、 设定值(v);
a、 setLink(头);
水头=a;
计数++;
}
当它说
assertEquals(“ciao”,b.get(0));
时,我说它应该是“ciao”,因为它删除了第一个节点,但错误显示为它期望“see ya”,当我认为“see ya”被删除时,因为我删除了
delete(0);
我遗漏了什么吗


编辑:我添加了add方法

您的delete方法在这里有问题

public void add(X v)
{
    Node<X> a = new Node<X>();
    a.setValue(v);
    a.setLink(head);
    head = a;
    count++;
}
它实际上并没有删除,因为“head”仍然引用同一个节点。请重试

if (index == 0)
{
    current = current.getLink();
    count--;
    return;
}

你的删除方法不正确。这是一个更正的版本,解释了你的错误

if (index == 0)
{
    head = current.getLink();
    count--;
    return;
}
实际包含的项目顺序为
[3,2,1]
而不是
[1,2,3]

我假设您不想要这个,然后为您编写一个新的add方法

list.add(1);
list.add(2);
list.add(3);
public void add(X v)
{
节点a=新节点();
a、 设定值(v);
if(head==null)
水头=a;
其他的
{
Node tail=getTail();
tail.setLink(a);
}
计数++;
}
私有节点getTail(){
if(head==null)
返回null;
节点电流=头;
while(current.getLink()!=null)
current=current.getLink();
回流;
}
这会将项目按顺序添加到列表中。但结果是,现在添加项目是O(n)。这在单链表中是不可避免的

编辑:

重写你的常规。我很无聊,原谅我

public void add(X v)
{
    Node<X> a = new Node<X>();
    a.setValue(v);
    if(head==null)
        head = a;
    else
    {
        Node<X> tail = getTail();
        tail.setLink(a);
    }
    count++;
}
private Node<X> getTail(){
    if(head==null)
        return null;
    Node<X> current = head;
    while(current.getLink()!=null)
        current = current.getLink();
    return current;
}
public void delete(int索引){
如果(索引<0 | |索引>计数-1){
将新的ArrayIndexOutOfBoundsException()抛出;
}
如果(索引==0){
head=head.getLink();
}否则{
节点beforeDeletedNode=头;
对于(int i=0;i
你确定你的LIST已经订购了吗?你的add方法没有添加到尾部。它实际上是在头部插入。不确定这是否是有意的,但当我看到“add()”时,我想它会添加到尾部,这是元素插入的顺序。所以hello->bye->adios->ciao->see ya。但是你的实现是相反的。see ya->ciao->adios->bye->helloyes这是有意的谢谢,这是有道理的,你是第一个所以我会给你支票谢谢,请阅读关于你的添加例程的部分。它是reve正如你所发布的一样,rsed。是的,把它倒过来才是重点。好主意哦,抱歉,哈哈。我发布了代码,以便在你需要时按顺序添加。祝你好运@user3427042。欢迎你。但你的问题还没有结束。我对下一个for()循环有点怀疑,因为它在index=1时不起作用。for()循环循环被跳过。@anonymous在这种情况下应该跳过
for
循环。这也让我有一秒钟的时间。写例程imho只是一种奇怪的方式。@williammerrison我怀疑他在删除最后一个节点时会得到NullPointerException。@anonymous他不会。如果有超过1个节点,他的例程会得到最后一个节点之前的节点。t他的意思是,当删除系列中的最后一个节点时,下一个链接将永远不会为空,因为您实际上将操作倒数第二个节点。我在回答中重写了例程。这对我个人来说更容易阅读(见图,我写的)。@Williamorrison Yep,您是对的。您的代码肯定更容易阅读。
    if (index == 0)
    {
        //we're deleting the head here. You need to reassign head variable.
        //current = current.getLink(); <-- no. this doesn't change the structure.
        head = head.getLink();//here we set head to head's next. Now structure is changed.
        count--;
        return;
    }
list.add(1);
list.add(2);
list.add(3);
public void add(X v)
{
    Node<X> a = new Node<X>();
    a.setValue(v);
    if(head==null)
        head = a;
    else
    {
        Node<X> tail = getTail();
        tail.setLink(a);
    }
    count++;
}
private Node<X> getTail(){
    if(head==null)
        return null;
    Node<X> current = head;
    while(current.getLink()!=null)
        current = current.getLink();
    return current;
}
public void delete(int index) {
    if (index < 0 || index > count-1) {
        throw new ArrayIndexOutOfBoundsException();
    }

    if (index == 0) {
        head = head.getLink();
    }else{
        Node<X> beforeDeletedNode = head;
        for (int i = 0; i < index-1; i++) {
            beforeDeletedNode = beforeDeletedNode.getLink();
        }
        Node<X> toDelete = beforeDeletedNode.getLink();
        beforeDeletedNode.setLink(toDelete.getLink());
    }
    count--;

}