Abinash Dash
Abinash Dash

Reputation: 75

Deletion Method of Circular Doubly Linked List

This is the deletion method of a circular doubly linked list.

    // Deletion of node from CDLL
    void deleteNode(int location) {
        if (head == null) {
            System.out.println("CDLL doesn't exist!");
            return;
        } else if (location == 0) {
            if (size == 1) {
                head.next = null;
                head.prev = null;
                head = null;
                tail = null;
                size--;
                return;
            } else {
                head = head.next;
                head.prev = tail;
                tail.next = head;
                size--;
            }
        }
    }

My question is -> -This part of the code deletes the first node of the linked list. -In the "else if" condition (where "location == 0") -Inside it where "if (size == 1)" -This means that we are deleting the first node when there is only one node in the linkedlist. -If we only write this much ->

if (size == 1) {
                head = null;
                tail = null;
                size--;
                return;
                }

-Will the first node still be collected by the garbage collector? -I think it should be collected by the garbage collector because there is nothing pointing to that one node anymore, the head and tail are null. -Please answer.

Upvotes: 1

Views: 389

Answers (1)

tquadrat
tquadrat

Reputation: 4034

Yes, it will be garbage collected even when the line

head.next = null;
head.prev = null;

are omitted.

Whether the attributes next and prev are different from null depends from the definition for your CDLL, they could be also set to head. Refer to the method that adds new entries to answer this question if you are interested.

But no matter which values the two attributes have, it does not matter for the garbage collector.

Upvotes: 1

Related Questions