From bb198fc73322d8c740d5c48f5fea9c97614eaa5c Mon Sep 17 00:00:00 2001 From: yanqizhao Date: Thu, 28 Apr 2022 19:19:32 +0800 Subject: [PATCH 1/3] fix: fix the missing assignment of tail in copyNode(returningCopyOf:) --- .../projects/finished/LinkedList.playground/Contents.swift | 4 ++++ .../finished/LinkedList.playground/Sources/LinkedList.swift | 1 + 2 files changed, 5 insertions(+) diff --git a/06-linked-list/projects/finished/LinkedList.playground/Contents.swift b/06-linked-list/projects/finished/LinkedList.playground/Contents.swift index d41e150..5569c42 100644 --- a/06-linked-list/projects/finished/LinkedList.playground/Contents.swift +++ b/06-linked-list/projects/finished/LinkedList.playground/Contents.swift @@ -129,4 +129,8 @@ example(of: "linked list cow") { list2.remove(after: node) } print("List2: \(list2)") + + print("Append node on list2") + list2.append(4) + print("List2: \(list2)") } diff --git a/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift b/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift index ab44d82..4122fb6 100644 --- a/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift +++ b/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift @@ -135,6 +135,7 @@ public struct LinkedList { newNode = newNode!.next oldNode = nextOldNode } + tail = newNode return nodeCopy } From aed019616cdf70f82e7a681e25c55f09ed7aed19 Mon Sep 17 00:00:00 2001 From: yanqizhao Date: Thu, 28 Apr 2022 19:53:39 +0800 Subject: [PATCH 2/3] fix: add the missing copyNodes(returningCopyOf:) in insert(_:, after:) --- .../finished/LinkedList.playground/Contents.swift | 9 ++++++++- .../LinkedList.playground/Sources/LinkedList.swift | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/06-linked-list/projects/finished/LinkedList.playground/Contents.swift b/06-linked-list/projects/finished/LinkedList.playground/Contents.swift index 5569c42..fce6265 100644 --- a/06-linked-list/projects/finished/LinkedList.playground/Contents.swift +++ b/06-linked-list/projects/finished/LinkedList.playground/Contents.swift @@ -38,7 +38,7 @@ example(of: "inserting at a particular index") { print("Before inserting: \(list)") var middleNode = list.node(at: 1)! for _ in 1...4 { - middleNode = list.insert(-1, after: middleNode) + middleNode = list.insert(-1, after: middleNode) ?? Node(value: 0) } print("After inserting: \(list)") } @@ -133,4 +133,11 @@ example(of: "linked list cow") { print("Append node on list2") list2.append(4) print("List2: \(list2)") + + print("Insert node on list2") + if let node2 = list2.node(at: 0) { + list2.insert(5, after: node2) + } + print("List2: \(list2)") + } diff --git a/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift b/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift index 4122fb6..2c9439d 100644 --- a/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift +++ b/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift @@ -41,8 +41,8 @@ public struct LinkedList { } @discardableResult - public mutating func insert(_ value: Value, after node: Node) -> Node { - copyNodes() + public mutating func insert(_ value: Value, after node: Node) -> Node? { + guard let node = copyNodes(returningCopyOf: node) else { return nil } guard tail !== node else { append(value) return tail! From 3191c0998e447bb3c7e55dcdcac969d3ccba2a53 Mon Sep 17 00:00:00 2001 From: yanqizhao Date: Thu, 28 Apr 2022 20:02:42 +0800 Subject: [PATCH 3/3] fix: fix the wrong return value of copyNodes(returningCopyOf:) when isKnownUniquelyReferenced is true --- .../projects/finished/LinkedList.playground/Contents.swift | 5 +++++ .../finished/LinkedList.playground/Sources/LinkedList.swift | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/06-linked-list/projects/finished/LinkedList.playground/Contents.swift b/06-linked-list/projects/finished/LinkedList.playground/Contents.swift index fce6265..82817f6 100644 --- a/06-linked-list/projects/finished/LinkedList.playground/Contents.swift +++ b/06-linked-list/projects/finished/LinkedList.playground/Contents.swift @@ -140,4 +140,9 @@ example(of: "linked list cow") { } print("List2: \(list2)") + print("Removing middle node on list2") + if let node3 = list2.node(at: 1) { + list2.remove(after: node3) + } + print("List2: \(list2)") } diff --git a/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift b/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift index 2c9439d..0e49f2b 100644 --- a/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift +++ b/06-linked-list/projects/finished/LinkedList.playground/Sources/LinkedList.swift @@ -117,7 +117,7 @@ public struct LinkedList { private mutating func copyNodes(returningCopyOf node: Node?) -> Node? { guard !isKnownUniquelyReferenced(&head) else { - return nil + return node } guard var oldNode = head else { return nil