+ 3
How do I make a double linked list is Swift?
Swift uses Automatic Reference Counting, or ARC, for garbage collection. It works by allocating an object, keeping a count of the number of references to that object, and deallocating that object when the number of references to it equals zero. This poses a problem with a doubly linked list, since a doubly linked list makes objects that refer to each other. Objects that refer to each other is itself a problem, because the reference count never equals zero, even if it's inaccessible, thus causing a memory leak. (See https://code.sololearn.com/c1mw3uhQ4Gme/?ref=app) Any ideas?
1 Answer
+ 4
a!.Next = nil
b!.Previous = nil
a = nil
b = nil
You need to remove the references to each other first.
You should create a DoublyLinkedList class that uses 2 nodes, head and tail. Your DoublyLinkedList class should be used to add and remove the nodes handling the assignment and nil of the additional elements etc. You'd then use a DoublyLinkedList object with its methods to add and remove nodes to the DLL.
https://medium.com/flawless-app-stories/doubly-linked-lists-swift-4-ae3cf8a5b975