Skip to content

Commit 5b8c263

Browse files
authored
Merge pull request kodecocodes#212 from tuliot/master
Update Linked List to Swift 3
2 parents b4f2188 + 9857da6 commit 5b8c263

File tree

5 files changed

+79
-76
lines changed

5 files changed

+79
-76
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 23 additions & 23 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
@@ -47,7 +47,7 @@ public class LinkedList<T> {
4747
}
4848
}
4949

50-
public func nodeAtIndex(index: Int) -> Node? {
50+
public func nodeAt(_ index: Int) -> Node? {
5151
if index >= 0 {
5252
var node = head
5353
var i = index
@@ -61,7 +61,7 @@ public class LinkedList<T> {
6161
}
6262

6363
public subscript(index: Int) -> T {
64-
let node = nodeAtIndex(index)
64+
let node = nodeAt(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
@@ -111,7 +111,7 @@ public class LinkedList<T> {
111111
head = nil
112112
}
113113

114-
public func removeNode(node: Node) -> T {
114+
public func remove(node: Node) -> T {
115115
let prev = node.previous
116116
let next = node.next
117117

@@ -129,13 +129,13 @@ public class LinkedList<T> {
129129

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

135-
public func removeAtIndex(index: Int) -> T {
136-
let node = nodeAtIndex(index)
135+
public func removeAt(_ index: Int) -> T {
136+
let node = nodeAt(index)
137137
assert(node != nil)
138-
return removeNode(node!)
138+
return remove(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.nodeAt(0)!.value // "Hello"
215+
list.nodeAt(1)!.value // "World"
216+
list.nodeAt(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.nodeAt(0)!.value = "Universe"
231+
list.nodeAt(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.remove(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.removeAt(0) // "Swift"
250250
list.count // 0

Linked List/LinkedList.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Most operations on the linked list have complexity O(n).
55
*/
6-
public class LinkedListNode<T> {
6+
open class LinkedListNode<T> {
77
var value: T
88
var next: LinkedListNode?
99
weak var previous: LinkedListNode?
@@ -13,20 +13,20 @@ public class LinkedListNode<T> {
1313
}
1414
}
1515

16-
public class LinkedList<T> {
16+
open class LinkedList<T> {
1717
public typealias Node = LinkedListNode<T>
1818

19-
private var head: Node?
19+
fileprivate var head: Node?
2020

21-
public var isEmpty: Bool {
21+
open var isEmpty: Bool {
2222
return head == nil
2323
}
2424

25-
public var first: Node? {
25+
open var first: Node? {
2626
return head
2727
}
2828

29-
public var last: Node? {
29+
open var last: Node? {
3030
if var node = head {
3131
while case let next? = node.next {
3232
node = next
@@ -37,7 +37,7 @@ public class LinkedList<T> {
3737
}
3838
}
3939

40-
public var count: Int {
40+
open var count: Int {
4141
if var node = head {
4242
var c = 1
4343
while case let next? = node.next {
@@ -50,7 +50,7 @@ public class LinkedList<T> {
5050
}
5151
}
5252

53-
public func nodeAtIndex(index: Int) -> Node? {
53+
open func nodeAt(_ index: Int) -> Node? {
5454
if index >= 0 {
5555
var node = head
5656
var i = index
@@ -63,13 +63,13 @@ public class LinkedList<T> {
6363
return nil
6464
}
6565

66-
public subscript(index: Int) -> T {
67-
let node = nodeAtIndex(index)
66+
open subscript(index: Int) -> T {
67+
let node = nodeAt(index)
6868
assert(node != nil)
6969
return node!.value
7070
}
7171

72-
public func append(value: T) {
72+
open func append(_ value: T) {
7373
let newNode = Node(value: value)
7474
if let lastNode = last {
7575
newNode.previous = lastNode
@@ -79,7 +79,7 @@ public class LinkedList<T> {
7979
}
8080
}
8181

82-
private func nodesBeforeAndAfter(index: Int) -> (Node?, Node?) {
82+
fileprivate func nodesBeforeAndAfter(_ index: Int) -> (Node?, Node?) {
8383
assert(index >= 0)
8484

8585
var i = index
@@ -96,7 +96,7 @@ public class LinkedList<T> {
9696
return (prev, next)
9797
}
9898

99-
public func insert(value: T, atIndex index: Int) {
99+
open func insert(_ value: T, atIndex index: Int) {
100100
let (prev, next) = nodesBeforeAndAfter(index)
101101

102102
let newNode = Node(value: value)
@@ -110,11 +110,11 @@ public class LinkedList<T> {
110110
}
111111
}
112112

113-
public func removeAll() {
113+
open func removeAll() {
114114
head = nil
115115
}
116116

117-
public func removeNode(node: Node) -> T {
117+
open func remove(_ node: Node) -> T {
118118
let prev = node.previous
119119
let next = node.next
120120

@@ -130,15 +130,15 @@ public class LinkedList<T> {
130130
return node.value
131131
}
132132

133-
public func removeLast() -> T {
133+
open func removeLast() -> T {
134134
assert(!isEmpty)
135-
return removeNode(last!)
135+
return remove(last!)
136136
}
137137

138-
public func removeAtIndex(index: Int) -> T {
139-
let node = nodeAtIndex(index)
138+
open func removeAt(_ index: Int) -> T {
139+
let node = nodeAt(index)
140140
assert(node != nil)
141-
return removeNode(node!)
141+
return remove(node!)
142142
}
143143
}
144144

@@ -167,17 +167,17 @@ extension LinkedList {
167167
}
168168

169169
extension LinkedList {
170-
public func map<U>(transform: T -> U) -> LinkedList<U> {
170+
public func map<U>(_ transform: (T)-> U) -> LinkedList<U> {
171171
let result = LinkedList<U>()
172172
var node = head
173173
while node != nil {
174-
result.append(transform(node!.value))
174+
result.append(transform(node!.value))
175175
node = node!.next
176176
}
177177
return result
178178
}
179179

180-
public func filter(predicate: T -> Bool) -> LinkedList<T> {
180+
public func filter(_ predicate: (T)-> Bool) -> LinkedList<T> {
181181
let result = LinkedList<T>()
182182
var node = head
183183
while node != nil {

Linked List/README.markdown

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ It loops through the list in the same manner but this time increments a counter
216216
What if we wanted to find the node at a specific index in the list? With an array we can just write `array[index]` and it's an **O(1)** operation. It's a bit more involved with linked lists, but again the code follows a similar pattern:
217217

218218
```swift
219-
public func nodeAtIndex(index: Int) -> Node? {
219+
public func nodeAt(_ index: Int) -> Node? {
220220
if index >= 0 {
221221
var node = head
222222
var i = index
@@ -235,16 +235,16 @@ The loop looks a little different but it does the same thing: it starts at `head
235235
Try it out:
236236

237237
```swift
238-
list.nodeAtIndex(0)!.value // "Hello"
239-
list.nodeAtIndex(1)!.value // "World"
240-
list.nodeAtIndex(2) // nil
238+
list.nodeAt(0)!.value // "Hello"
239+
list.nodeAt(1)!.value // "World"
240+
list.nodeAt(2) // nil
241241
```
242242

243243
For fun we can implement a `subscript` method too:
244244

245245
```swift
246246
public subscript(index: Int) -> T {
247-
let node = nodeAtIndex(index)
247+
let node = nodeAt(index)
248248
assert(node != nil)
249249
return node!.value
250250
}
@@ -367,7 +367,7 @@ If you had a tail pointer, you'd set it to `nil` here too.
367367
Next we'll add some functions that let you remove individual nodes. If you already have a reference to the node, then using `removeNode()` is the most optimal because you don't need to iterate through the list to find the node first.
368368

369369
```swift
370-
public func removeNode(node: Node) -> T {
370+
public func remove(node: Node) -> T {
371371
let prev = node.previous
372372
let next = node.next
373373

@@ -391,24 +391,24 @@ Don't forget the `head` pointer! If this was the first node in the list then `he
391391
Try it out:
392392

393393
```swift
394-
list.removeNode(list.first!) // "Hello"
394+
list.remove(list.first!) // "Hello"
395395
list.count // 2
396396
list[0] // "Swift"
397397
list[1] // "World"
398398
```
399399

400-
If you don't have a reference to the node, you can use `removeLast()` or `removeAtIndex()`:
400+
If you don't have a reference to the node, you can use `removeLast()` or `removeAt()`:
401401

402402
```swift
403403
public func removeLast() -> T {
404404
assert(!isEmpty)
405-
return removeNode(last!)
405+
return remove(node: last!)
406406
}
407407

408-
public func removeAtIndex(index: Int) -> T {
409-
let node = nodeAtIndex(index)
408+
public func removeAt(_ index: Int) -> T {
409+
let node = nodeAt(index)
410410
assert(node != nil)
411-
return removeNode(node!)
411+
return remove(node: node!)
412412
}
413413
```
414414

@@ -419,7 +419,7 @@ list.removeLast() // "World"
419419
list.count // 1
420420
list[0] // "Swift"
421421

422-
list.removeAtIndex(0) // "Swift"
422+
list.removeAt(0) // "Swift"
423423
list.count // 0
424424
```
425425

0 commit comments

Comments
 (0)