0

I was given an integer called key as the parameter. I need to know how to delete a node at position key. Ndata is from my Node class.

    public void deleteNode(int key)
    {
       Node<E> temp = head; 
       while(temp.Ndata != key)//gives me error as not compatible types.    
       {
            temp = temp.next;
       }
       if(temp == head)
            head = temp.next;
       else
            temp.prev.next = temp.next;
       if(temp == tail)
            tail == temp.prev;
       else
            temp.prev.next = temp.next;
     }
4
  • What type is node.Ndata? Why isn't it an int? Commented Apr 14, 2015 at 1:19
  • its type T because I am using generics Commented Apr 14, 2015 at 1:26
  • It's also not clear if key is supposed to be the node position of the element to delete, or the node value - your question says key is the position, but then your code seems to look for node value (Ndata). To add to the confusion, one of the two answers so far assumes value and the other is assuming position... Commented Apr 14, 2015 at 3:12
  • key is the element that should be deleted . if key = 5 then the 5th element needs to be deleted. Commented Apr 14, 2015 at 23:39

2 Answers 2

1

Assuming that you need to find the node which is storing the value key, you need to do either:

Node<Integer> temp = head;

or:

public void deleteNode(E key)

This ensures type compatibility for your generic Node class.

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

1 Comment

I did the first but head is type Node<E> and it does not work if I cast it to Node<Integer> head.
1

As you mentioned that, you need to delete a node at position key. Here is how the code should be:

public void deleteNode (int key) {
    // those are base cases
    if (key >= length)
        key = length - 1;
    elseif (key < length)
        key = 0;

    if (head == null) // if list is empty
        return;

    if (key == 0){ // it means you have to remove head
        head = head.getNext(); //assuming you have getters and setters
        if (head == null)
            tail = null;
    } else {
        Node<E> temp = head;
        // iterate to that position ie. key
        for (int index = 0; index < key; index++){
            temp = temp.getNext();
        }
        // this is most important part
        temp.getNext().setPrev(temp.getPrev());
        temp.getPrev().setNext(temp.getNext());
    }
    length--; //since you deleted element the list is now 1 less
}

Hope this helps you.

5 Comments

This helped a lot but to make sure I got it I need to create an int variable called length in my class. Then keep track of it through all my insert methods.
yes, I made an assumption that you will be tracking all your inserts and deletes
When I ran it like this it only deleted the first node every time no matter what I do.
Logically, the code is correct. The head will get deleted only when your key == 0. It will be good if you use a debugger to check if the nodes are updating correctly. That will be helpful to identify the trivial mistake that might be happening.
I have a similar problem, do you think you can help me out. I've been searching everywhere on the internet, through my books and I'm getting nowhere.

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.