I am to java and I am trying to implement a remove from double linked list method but I am struggling and not sure how to advance. The method removes data stored at the given node in the list. I have read that I need to account for cases where the the element being removed is the start or end but I am not sure how to go about. In general I am not sure if this is the right way to do it. My code/progress is posted below. If any could help, it would be appreciated. Thanks
P.S. I have a start and an end reference within the class and a size reference
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;
}
I have changed the
typetoType. Using lowercase for class names is not recommended in Java. I have added loads of comments in the hope that you will understand what is going on.Note that this is not tested code. You may find bugs in it but I am confident that the essence of process is there.