1

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.

2
  • 3
    What exactly have you tried? What didn't work? Did you try debugging it in a debugger? This is much the same in C++ or C# as it is in Java. Have you looked at the code for LinkedList as its is a doubly linked list? c.f. LinkedList.unlink(Node) Commented Jan 10, 2012 at 15:02
  • 6
    The u.list() == name and u.list() != name almost certainly don't do what you want. Use String.equals() instead. Commented Jan 10, 2012 at 15:02

1 Answer 1

1

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); 
        }




    } 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.