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条)

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接