8000 Fixed a bug when appending or inserting a node · matsoftware/swift-algorithm-club@93cbde7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93cbde7

Browse files
committed
Fixed a bug when appending or inserting a node
1 parent 4d68679 commit 93cbde7

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public final class LinkedList<T> {
7373
self.append(newNode)
7474
}
7575

76-
public func append(_ newNode: Node) {
76+
public func append(_ node: Node) {
77+
let newNode = LinkedListNode(value: node.value)
7778
if let lastNode = last {
7879
newNode.previous = lastNode
7980
lastNode.next = newNode
@@ -104,9 +105,9 @@ public final class LinkedList<T> {
104105
self.insert(newNode, atIndex: index)
105106
}
106107

107-
public func insert(_ newNode: Node, atIndex index: Int) {
108+
public func insert(_ node: Node, atIndex index: Int) {
108109
let (prev, next) = nodesBeforeAndAfter(index: index)
109-
110+
let newNode = LinkedListNode(value: node.value)
110111
newNode.previous = prev
111112
newNode.next = next
112113
prev?.next = newNode
@@ -264,24 +265,24 @@ f // [Universe, Swifty]
264265
//list.removeAll()
265266
//list.isEmpty
266267

267-
list.remove(node: list.first!) // "Hello"
268+
list.remove(node: list.first!) // "Universe"
268269
list.count // 2
269-
list[0] // "Swift"
270-
list[1] // "World"
270+
list[0] // "Swifty"
271+
list[1] // "Hello"
271272

272-
list.removeLast() // "World"
273+
list.removeLast() // "Hello"
273274
list.count // 1
274-
list[0] // "Swift"
275+
list[0] // "Swifty"
275276

276-
list.remove(atIndex: 0) // "Swift"
277+
list.remove(atIndex: 0) // "Swifty"
277278
list.count // 0
278279

279280
let linkedList: LinkedList<Int> = [1, 2, 3, 4] // [1, 2, 3, 4]
280281
linkedList.count // 4
281282
linkedList[0] // 1
282283

283284
// Infer the type from the array
284-
let listArrayLiteral2: LinkedList = ["Swift", "Algorithm", "Club"]
285-
listArrayLiteral2.count // 3
286-
listArrayLiteral2[0] // "Swift"
287-
listArrayLiteral2.removeLast() // "Club"
285+
let listArrayLiteral2: LinkedList? = ["Swift", "Algorithm", "Club"]
286+
listArrayLiteral2?.count // 3
287+
listArrayLiteral2?[0] // "Swift"
288+
listArrayLiteral2?.removeLast() // "Club"

0 commit comments

Comments
 (0)
0