Skip to content

Commit 0fa2623

Browse files
author
Peter Ivanics
committed
Migrate Linked List to Swift 3.0
1 parent bf9ba31 commit 0fa2623

File tree

3 files changed

+80
-88
lines changed

3 files changed

+80
-88
lines changed

Linked List/LinkedList.playground/Contents.swift

+18-21
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class LinkedList<T> {
4747
}
4848
}
4949

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

6363
public subscript(index: Int) -> T {
64-
let node = nodeAt(index)
64+
let node = self.node(atIndex: index)
6565
assert(node != nil)
6666
return node!.value
6767
}
6868

69-
public func append(value: T) {
69+
public func append(_ value: T) {
7070
let newNode = Node(value: value)
7171
if let lastNode = last {
7272
newNode.previous = lastNode
@@ -93,7 +93,7 @@ public class LinkedList<T> {
9393
return (prev, next)
9494
}
9595

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

9999
let newNode = Node(value: value)
@@ -132,8 +132,8 @@ public class LinkedList<T> {
132132
return remove(node: last!)
133133
}
134134

135-
public func removeAt(_ index: Int) -> T {
136-
let node = nodeAt(index)
135+
public func remove(atIndex index: Int) -> T {
136+
let node = self.node(atIndex: index)
137137
assert(node != nil)
138138
return remove(node: node!)
139139
}
@@ -168,7 +168,7 @@ extension LinkedList {
168168
let result = LinkedList<U>()
169169
var node = head
170170
while node != nil {
171-
result.append(value: transform(node!.value))
171+
result.append(transform(node!.value))
172172
node = node!.next
173173
}
174174
return result
@@ -179,29 +179,26 @@ extension LinkedList {
179179
var node = head
180180
while node != nil {
181181
if predicate(node!.value) {
182-
result.append(value: node!.value)
182+
result.append(node!.value)
183183
}
184184
node = node!.next
185185
}
186186
return result
187187
}
188188
}
189189

190-
191-
192-
193190
let list = LinkedList<String>()
194191
list.isEmpty // true
195192
list.first // nil
196193
list.last // nil
197194

198-
list.append(value: "Hello")
195+
list.append("Hello")
199196
list.isEmpty
200197
list.first!.value // "Hello"
201198
list.last!.value // "Hello"
202199
list.count // 1
203200

204-
list.append(value: "World")
201+
list.append("World")
205202
list.first!.value // "Hello"
206203
list.last!.value // "World"
207204
list.count // 2
@@ -211,24 +208,24 @@ list.first!.next!.value // "World"
211208
list.last!.previous!.value // "Hello"
212209
list.last!.next // nil
213210

214-
list.nodeAt(0)!.value // "Hello"
215-
list.nodeAt(1)!.value // "World"
216-
list.nodeAt(2) // nil
211+
list.node(atIndex: 0)!.value // "Hello"
212+
list.node(atIndex: 1)!.value // "World"
213+
list.node(atIndex: 2) // nil
217214

218215
list[0] // "Hello"
219216
list[1] // "World"
220217
//list[2] // crash!
221218

222-
list.insert(value: "Swift", atIndex: 1)
219+
list.insert("Swift", atIndex: 1)
223220
list[0]
224221
list[1]
225222
list[2]
226223
print(list)
227224

228225
list.reverse() // [World, Swift, Hello]
229226

230-
list.nodeAt(0)!.value = "Universe"
231-
list.nodeAt(1)!.value = "Swifty"
227+
list.node(atIndex: 0)!.value = "Universe"
228+
list.node(atIndex: 1)!.value = "Swifty"
232229
let m = list.map { s in s.characters.count }
233230
m // [8, 6, 5]
234231
let f = list.filter { s in s.characters.count > 5 }
@@ -237,7 +234,7 @@ f // [Universe, Swifty]
237234
//list.removeAll()
238235
//list.isEmpty
239236

240-
list.remove(node: list.first!) // "Hello"
237+
list.remove(node: list.first!) // "Hello"
241238
list.count // 2
242239
list[0] // "Swift"
243240
list[1] // "World"
@@ -246,5 +243,5 @@ list.removeLast() // "World"
246243
list.count // 1
247244
list[0] // "Swift"
248245

249-
list.removeAt(0) // "Swift"
246+
list.remove(atIndex: 0) // "Swift"
250247
list.count // 0

Linked List/LinkedList.swift

+46-51
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
/*
2-
Doubly-linked list
3-
4-
Most operations on the linked list have complexity O(n).
5-
*/
6-
open class LinkedListNode<T> {
1+
public class LinkedListNode<T> {
72
var value: T
83
var next: LinkedListNode?
94
weak var previous: LinkedListNode?
10-
5+
116
public init(value: T) {
127
self.value = value
138
}
149
}
1510

16-
open class LinkedList<T> {
11+
public class LinkedList<T> {
1712
public typealias Node = LinkedListNode<T>
18-
13+
1914
fileprivate var head: Node?
20-
21-
open var isEmpty: Bool {
15+
16+
public var isEmpty: Bool {
2217
return head == nil
2318
}
24-
25-
open var first: Node? {
19+
20+
public var first: Node? {
2621
return head
2722
}
28-
29-
open var last: Node? {
23+
24+
public var last: Node? {
3025
if var node = head {
3126
while case let next? = node.next {
3227
node = next
@@ -36,8 +31,8 @@ open class LinkedList<T> {
3631
return nil
3732
}
3833
}
39-
40-
open var count: Int {
34+
35+
public var count: Int {
4136
if var node = head {
4237
var c = 1
4338
while case let next? = node.next {
@@ -49,8 +44,8 @@ open class LinkedList<T> {
4944
return 0
5045
}
5146
}
52-
53-
open func nodeAt(_ index: Int) -> Node? {
47+
48+
public func node(atIndex index: Int) -> Node? {
5449
if index >= 0 {
5550
var node = head
5651
var i = index
@@ -62,14 +57,14 @@ open class LinkedList<T> {
6257
}
6358
return nil
6459
}
65-
66-
open subscript(index: Int) -> T {
67-
let node = nodeAt(index)
60+
61+
public subscript(index: Int) -> T {
62+
let node = self.node(atIndex: index)
6863
assert(node != nil)
6964
return node!.value
7065
}
71-
72-
open func append(_ value: T) {
66+
67+
public func append(_ value: T) {
7368
let newNode = Node(value: value)
7469
if let lastNode = last {
7570
newNode.previous = lastNode
@@ -78,67 +73,67 @@ open class LinkedList<T> {
7873
head = newNode
7974
}
8075
}
81-
82-
fileprivate func nodesBeforeAndAfter(_ index: Int) -> (Node?, Node?) {
76+
77+
private func nodesBeforeAndAfter(index: Int) -> (Node?, Node?) {
8378
assert(index >= 0)
84-
79+
8580
var i = index
8681
var next = head
8782
var prev: Node?
88-
83+
8984
while next != nil && i > 0 {
9085
i -= 1
9186
prev = next
9287
next = next!.next
9388
}
9489
assert(i == 0) // if > 0, then specified index was too large
95-
90+
9691
return (prev, next)
9792
}
98-
99-
open func insert(_ value: T, atIndex index: Int) {
100-
let (prev, next) = nodesBeforeAndAfter(index)
101-
93+
94+
public func insert(_ value: T, atIndex index: Int) {
95+
let (prev, next) = nodesBeforeAndAfter(index: index)
96+
10297
let newNode = Node(value: value)
10398
newNode.previous = prev
10499
newNode.next = next
105100
prev?.next = newNode
106101
next?.previous = newNode
107-
102+
108103
if prev == nil {
109104
head = newNode
110105
}
111106
}
112-
113-
open func removeAll() {
107+
108+
public func removeAll() {
114109
head = nil
115110
}
116-
117-
open func remove(_ node: Node) -> T {
111+
112+
public func remove(node: Node) -> T {
118113
let prev = node.previous
119114
let next = node.next
120-
115+
121116
if let prev = prev {
122117
prev.next = next
123118
} else {
124119
head = next
125120
}
126121
next?.previous = prev
127-
122+
128123
node.previous = nil
129124
node.next = nil
130125
return node.value
131126
}
132-
133-
open func removeLast() -> T {
127+
128+
public func removeLast() -> T {
134129
assert(!isEmpty)
135-
return remove(last!)
130+
return remove(node: last!)
136131
}
137-
138-
open func removeAt(_ index: Int) -> T {
139-
let node = nodeAt(index)
132+
133+
public func remove(atIndex index: Int) -> T {
134+
let node = self.node(atIndex: index)
140135
assert(node != nil)
141-
return remove(node!)
136+
return remove(node: node!)
142137
}
143138
}
144139

@@ -167,17 +162,17 @@ extension LinkedList {
167162
}
168163

169164
extension LinkedList {
170-
public func map<U>(_ transform: (T)-> U) -> LinkedList<U> {
165+
public func map<U>(transform: (T) -> U) -> LinkedList<U> {
171166
let result = LinkedList<U>()
172167
var node = head
173168
while node != nil {
174-
result.append(transform(node!.value))
169+
result.append(transform(node!.value))
175170
node = node!.next
176171
}
177172
return result
178173
}
179-
180-
public func filter(_ predicate: (T)-> Bool) -> LinkedList<T> {
174+
175+
public func filter(predicate: (T) -> Bool) -> LinkedList<T> {
181176
let result = LinkedList<T>()
182177
var node = head
183178
while node != nil {

0 commit comments

Comments
 (0)