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 to delete all items in a doubly linked list that match a certain value?

Delete all nodes linked list

How to delete doubly linked list data and return it?

Swap nodes in doubly linked list

Delete all nodes in a linked list (in c)

delete all duplicated nodes in unsorted linked list

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

how to delete node from doubly-linked list

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

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

Swap nodes (with pointers) on doubly circular linked list

Swap two nodes in a doubly linked list

Doubly Linked List Class with Nodes C++

Doubly linked list - Delete the list from memory

Linked List: How to Sort Doubly Linked List?

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

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

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

Delete a prime number in a doubly linked list

Java - Delete Node From Doubly Linked List

Doubly linked list delete last C

Delete in circular doubly linked list connected with head

Doubly Linked List - cannot in delete the first Node

delete node in doubly linked list (Data Structures)

Cant delete node from doubly linked list

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

Matrix of doubly linked nodes

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

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