插入代码似乎在最后一次插入之前工作得很好,它没有按顺序添加它,而是将它放在列表的末尾。
public void insert(Comparable item)
{
if ( this.first == null || item.compareTo(this.first.data) <= 0)
{
addFirst(item);
}
else if( item.compareTo(this.first.data) > 0 )
{
addLast(item);
}
else
{
Node oldFirst = this.first;
this.first = this.first.next;
insert(item);
this.first = oldFirst;
}
}
这是它产生的输出......
6 Item(s)
5
16
21
22
45
23
remove 方法在删除项目后停止编译,我不知道为什么。
public Comparable remove(Comparable item)
{
if( this.first == null )
{
return null;
}
Node oldFirst = this.first;
if( this.first.next == null && this.first.data.equals(item) )
{
Comparable found = this.first.data;
this.first = null;
return found;
}
this.first = this.first.next;
if( this.first.data.equals(item) )
{
Comparable found = this.first.data;
oldFirst.next = this.first.next;
this.first = oldFirst;
return found;
}
Comparable foundIt = remove(item);
return foundIt;
}
这是 remove 方法的输出....
at List.remove(List.java:164)
Removed: 21. List has: 4 Item(s)
at List.remove(List.java:164)
16
at List.remove(List.java:164)
22
45
at TestRecursion.main(TestRecursion.java:87)