我對java和我想實現從雙鏈表中刪除方法,但我掙扎,不知道如何推進。該方法刪除存儲在列表中給定節點的數據。我已經讀過,我需要說明被刪除元素是開始還是結束的情況,但我不知道如何去做。總的來說,我不確定這是否是正確的做法。我的代碼/進度在下面發佈。如果有任何可以幫助,它將不勝感激。謝謝從雙向鏈表中刪除
P.S.我有一個開始和類內的端基準和一個大小的參考
public type removeAtTheIndex(int index)
{
type theData = null;
Node <type> current= start;
Node temp= new Node();
if (index >= 0 && index < size && start !=null)
{
for (int i=0; i < index && current.getNext()!= null; i++)
{
current=current.getNext();
}
if (current != null)
{
if (current == start)
{
}
else if (current == end)
{
}
else
{
theData= current.getData();
temp= current.getPrev();
temp.setNext(current.getNext());
current.getNext().setPrev(temp);
current.setData(null);
size--;
}
}
return theData;
}
爲什麼不使用[LinkedList](http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html)? – Kai
@ user714965我想通過創建我自己的方式來學習,因此我可以進一步研究。不過謝謝你的建議。你有沒有機會知道我的代碼有什麼問題?謝謝 –
當提供代碼時,它應該是可編譯的。你的代碼有什麼問題?您可以查看LinkedList的源代碼以獲得更好的理解。 – Kai