插入代碼似乎正在完美工作,直到最後一次插入,而不是按順序添加它,而是將它放在列表的末尾。從定製列表中遞歸地插入和刪除
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)
什麼是堆棧跟蹤崩潰? –
你可以把完整的code.Will讓你知道這個問題呢? –
是否有可能看到你的整個班級? –