How do I make a double linked list is Swift? | SoloLearn: Learn to code for FREE!


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 Any ideas?

5/28/2020 4:53:08 AM


1 Answer

New Answer


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.