How to delete all nodes in doubly linked list?

muthermutton

I'm curious as to what is the correct way of deleting all nodes in a doubly linked list.

Here's my linked list structure:

type ListNode struct {
    Data branch
    Next *ListNode
    Prev *ListNode
}

type doublyLinkedList struct {
    Head *ListNode
    Tail *ListNode
    Size int
}

Will it work if I just point the Head & Tail nodes to Nil?

func deleteAllNodes(dl *doublyLinkedList) {
    dl.Head = nil
    dl.Tail = nil
    dl.Size = 0
}

If so, what happens to all the nodes? Does it get garbage collected?

Alexander

In a reference counted environment (Arc in Rust, shared_ptr in C++, Swift, etc.), this could leak.

The nodes may have references among themselves, but there's no other references to them. In graph theory terms, the nodes being "deleted" form a component of the object graph, which is now a disconnected graph.

Any environment with a tracing garbage collector (including Go) can handle this, no problem.

First, the GC will detect all connected components of the memory graph (those objects which are referenced from root references, like global variables, local variables, etc.). This is called the "mark" phase. Then, it will delete all disconnected components in a second "sweep" phase. https://en.wikipedia.org/wiki/Tracing_garbage_collection#Na%C3%AFve_mark-and-sweep

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you delete all the nodes on the right of a linked list when a value greater than 'x' is encountered?

I'm unable to delete all nodes from a linked list

Doubly Linked List - cannot in delete the first Node

Delete all nodes linked list

Doubly linked list - Delete the list from memory

How to delete the list when linked list nodes are constructed with "new"?

How to delete doubly linked list data and return it?

How can I add a node at the beginning of a doubly linked list in-between two dummy nodes?

Doubly linked list delete last C

How to delete all items in a doubly linked list that match a certain value?

how to delete node from doubly-linked list

Delete in circular doubly linked list connected with head

delete node in doubly linked list (Data Structures)

Why adding a node to a doubly linked list deletes all nodes except first node?

Java - Delete Node From Doubly Linked List

Linked List: How to Sort Doubly Linked List?

Matrix of doubly linked nodes

How to search name and delete node in doubly linked list in C?

Swap two nodes in a doubly linked list

Cant delete node from doubly linked list

Delete a prime number in a doubly linked list

Swap nodes (with pointers) on doubly circular linked list

delete all duplicated nodes in unsorted linked list

Swap nodes in doubly linked list

How to delete a specific value from a doubly linked list?

is there a method to delete the element at the end of a doubly linked list?

How to sort a doubly linked list with the nodes instead of the node's values

Doubly Linked List Class with Nodes C++

Delete all nodes in a linked list (in c)