Reputation: 4596
Hello I have a double linked list set up, and i have a search working for it and all that stuff, i just want to delete from it too.
For my search i have:
public void firstNameSearch(String name)
{
Node u = header;
while (u != null && u.list() != name )
{
System.out.println("Searching List...");
u = u.getNext();
}
if (u.list() == name)
{
// what do I need to put here to delete it
}
}
I have looked through over post on stack overflow, but the ones i found were in C, so weren't a great help, I understand the concept on how to make it delete the node, just can't get it functional.
Thank you in advance.
Upvotes: 1
Views: 7467
Reputation: 17893
Its a very basic operation.I assume there is method as set/getPrevious() as its a double linked list.
[previous]<==>[u]<==>[next]
Deleting an element in a doubly linked list would be simply changing the references pointers of previous and next node.
if (u.list() == name)
{
Node pre = u.getPrevious();
Node next= u.getNext();
//Connect next node and previous node
if(pre != null){
next.setPrevious(pre);
}else{
header=next;
}
//Connect previous node and next node
if(next != null){
pre.setNext(next);
}else{
pre.setNext(null);
}
}
Upvotes: 1