Skip to content

Commit 8d03fea

Browse files
committed
Updated Linked List playground to swift 3
1 parent 116fb5d commit 8d03fea

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class LinkedListNode<T> {
1313
public class LinkedList<T> {
1414
public typealias Node = LinkedListNode<T>
1515

16-
private var head: Node?
16+
fileprivate var head: Node?
1717

1818
public var isEmpty: Bool {
1919
return head == nil
@@ -61,7 +61,7 @@ public class LinkedList<T> {
6161
}
6262

6363
public subscript(index: Int) -> T {
64-
let node = nodeAtIndex(index)
64+
let node = nodeAtIndex(index: index)
6565
assert(node != nil)
6666
return node!.value
6767
}
@@ -94,7 +94,7 @@ public class LinkedList<T> {
9494
}
9595

9696
public func insert(value: T, atIndex index: Int) {
97-
let (prev, next) = nodesBeforeAndAfter(index)
97+
let (prev, next) = nodesBeforeAndAfter(index: index)
9898

9999
let newNode = Node(value: value)
100100
newNode.previous = prev
@@ -129,13 +129,13 @@ public class LinkedList<T> {
129129

130130
public func removeLast() -> T {
131131
assert(!isEmpty)
132-
return removeNode(last!)
132+
return removeNode(node: last!)
133133
}
134134

135135
public func removeAtIndex(index: Int) -> T {
136-
let node = nodeAtIndex(index)
136+
let node = nodeAtIndex(index: index)
137137
assert(node != nil)
138-
return removeNode(node!)
138+
return removeNode(node: node!)
139139
}
140140
}
141141

@@ -164,22 +164,22 @@ extension LinkedList {
164164
}
165165

166166
extension LinkedList {
167-
public func map<U>(transform: T -> U) -> LinkedList<U> {
167+
public func map<U>(transform: (T) -> U) -> LinkedList<U> {
168168
let result = LinkedList<U>()
169169
var node = head
170170
while node != nil {
171-
result.append(transform(node!.value))
171+
result.append(value: transform(node!.value))
172172
node = node!.next
173173
}
174174
return result
175175
}
176176

177-
public func filter(predicate: T -> Bool) -> LinkedList<T> {
177+
public func filter(predicate: (T) -> Bool) -> LinkedList<T> {
178178
let result = LinkedList<T>()
179179
var node = head
180180
while node != nil {
181181
if predicate(node!.value) {
182-
result.append(node!.value)
182+
result.append(value: node!.value)
183183
}
184184
node = node!.next
185185
}
@@ -195,13 +195,13 @@ list.isEmpty // true
195195
list.first // nil
196196
list.last // nil
197197

198-
list.append("Hello")
198+
list.append(value: "Hello")
199199
list.isEmpty
200200
list.first!.value // "Hello"
201201
list.last!.value // "Hello"
202202
list.count // 1
203203

204-
list.append("World")
204+
list.append(value: "World")
205205
list.first!.value // "Hello"
206206
list.last!.value // "World"
207207
list.count // 2
@@ -211,24 +211,24 @@ list.first!.next!.value // "World"
211211
list.last!.previous!.value // "Hello"
212212
list.last!.next // nil
213213

214-
list.nodeAtIndex(0)!.value // "Hello"
215-
list.nodeAtIndex(1)!.value // "World"
216-
list.nodeAtIndex(2) // nil
214+
list.nodeAtIndex(index: 0)!.value // "Hello"
215+
list.nodeAtIndex(index: 1)!.value // "World"
216+
list.nodeAtIndex(index: 2) // nil
217217

218218
list[0] // "Hello"
219219
list[1] // "World"
220220
//list[2] // crash!
221221

222-
list.insert("Swift", atIndex: 1)
222+
list.insert(value: "Swift", atIndex: 1)
223223
list[0]
224224
list[1]
225225
list[2]
226226
print(list)
227227

228228
list.reverse() // [World, Swift, Hello]
229229

230-
list.nodeAtIndex(0)!.value = "Universe"
231-
list.nodeAtIndex(1)!.value = "Swifty"
230+
list.nodeAtIndex(index: 0)!.value = "Universe"
231+
list.nodeAtIndex(index: 1)!.value = "Swifty"
232232
let m = list.map { s in s.characters.count }
233233
m // [8, 6, 5]
234234
let f = list.filter { s in s.characters.count > 5 }
@@ -237,7 +237,7 @@ f // [Universe, Swifty]
237237
//list.removeAll()
238238
//list.isEmpty
239239

240-
list.removeNode(list.first!) // "Hello"
240+
list.removeNode(node: list.first!) // "Hello"
241241
list.count // 2
242242
list[0] // "Swift"
243243
list[1] // "World"
@@ -246,5 +246,5 @@ list.removeLast() // "World"
246246
list.count // 1
247247
list[0] // "Swift"
248248

249-
list.removeAtIndex(0) // "Swift"
249+
list.removeAtIndex(index: 0) // "Swift"
250250
list.count // 0

0 commit comments

Comments
 (0)