vendredi 30 décembre 2016

Deletion of a Middle Node from Doubly Linked List in java

I need to delete middle node from doubly linked list i think this code is correct but doesn't work in the deleting of middle node ? (the delete is Random)

public Item dequeue()
{
if (isEmpty())
    throw new java.util.NoSuchElementException(
            " cannot remove from an empty deque");
else {

    Node current = getRanPointer();
    Item it;

    if (current == First)
    {
         it = (Item) First.item;
        First = First.next;
        First.prev = null;
        N--;
    }
    else if (current == Last)
    {
         it = (Item) Last.item;
        Last = Last.prev;
        Last.next = null;
        N--;
    } 
    else
    {
        it = (Item) current.item;
        current.prev.next = current.next;
        current.next.prev = current.prev ;
        current = null; // deletes the pointer
        N--;
    }

    return (Item) it;
}

}




Aucun commentaire:

Enregistrer un commentaire