nn970510 2016-10-07 15:09 采纳率: 100%
浏览 944
已采纳

Java菜鸟问题,求大神们耐心解答

第一次在CDSN上提问,不知道有没有大神可以帮我看看:
这是我们这个星期的作业要求:
The Sorted List ADT
Implement the SortedList class. The SortedList class extends
the List class. Both can be seen here. Your assignment is to
implement (recursively) all of the abstract methods of the List class.
They are:
insert (recursive)
iterator
remove (recursive)
retrieve (recursive)
search (recursive)
You must also implement an Iterator inner class for the
SortedList class. You must submit a modified SortedList.java
file with your source code. Do not submit and do not modify
the List.java file.

然后我试着写了把retrieve,search和iterator方法都写出来了,唯有insert和remove(我想这两个应该差不多)写不出来,这是我试的代码,被报错,求大神们指点
public void insert(E data){
insert(head, data);

private insert(Node curr, data){
Node temp = new Node (data);
if (curr==null || data.compareTo(curr.data)<0){
temp.next=curr;
curr=temp;
break;
}
else{
continue;
}

return insert(curr.next, data);
}
return;
}

  • 写回答

7条回答 默认 最新

  • yinbucheng 2016-10-08 02:00
    关注

    如果我是你我会如下
    public void insert(Node pre,Node curr,T data){
    if(pre = null&&curr==null){//这表示开始没有一个节点
    curr = new Node(data);//其实这里还需要让 head = curr.这里设置java内存应用指向问题
    return;
    }

       if(curr.data.compareTo(data)>0){//这表示满足插入条件
            Node temp = new Node(data);
              if(pre!=null){
                        pre.next = temp;
                             temp.next = curr;
                      }else{
                          temp.next = curr;
                        }
        }
        if(curr.next!=null)//这表示它还没到最后
        insert(curr,curr.next,data);
        else{
        Node temp = new Node(data);
         temp.next = null;
        curr.next = temp;
    
        }
    

    }

    我知道你是想curr永远为头节点,但是你写的代码就不是你传入参数可以这眼 Node temp = head;
    insert(null,temp,data)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效