Group Group Group Group Group Group Group Group Group

Removing a node after a particular node fails after COW fix

#1

In the linked list section, I think I’ve found bug for the “remove(after:)” func after we implement the “copyNodes()” function.

Before we add copyNodes(), the function works adequately, and we have “After removing at index 1: 1 -> 3” printed out as expected. But, after adding copyNodes() at the beginning of the remove(after:) function, the printout is “After removing at index 1: 1 -> 2 -> 3”

Then, in “example(of: “removing a node after a particular node”)”, with the copyNodes() at the beginning of the remove(after:) function, I assign list to a new list2 var prior to performing the remove operation. list2 prints out the desired outcome after the operation and list prints out as if nothing was removed!?

#2

@kelvin_lau Can you please help with this when you get a chance? Thank you - much appreciated! :]

#3

Hey Paul, thank you for bringing this up. This is indeed a bug.

The root of the problem is this line here:

let node = list.node(at: index - 1)!

node is a reference to a node of list.

However, once you call a mutating method, copyNodes creates a brand new set of nodes for list. The node value is now referencing an old node that is no longer part of list.

I’ll have to think about how I can fix this. But thank you for flagging this!

#4

Hi Kevin!

Good, can’t wait to see the fix.