简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM