Skip to content

Commit f26dd41

Browse files
author
billbarbour
committed
2 parents 26cb9d1 + 2afaf01 commit f26dd41

File tree

14 files changed

+563
-516
lines changed

14 files changed

+563
-516
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ script:
2626
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
2727
# - xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
2828
# - xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
29-
# - xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
29+
- xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
3030
# - xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
3131
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
3232
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests

Linked List/LinkedList.playground/Contents.swift

Lines changed: 27 additions & 20 deletions
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,36 @@ 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-
190+
extension LinkedList {
191+
convenience init(array: Array<T>) {
192+
self.init()
193+
194+
for element in array {
195+
self.append(element)
196+
}
197+
}
198+
}
192199

193200
let list = LinkedList<String>()
194201
list.isEmpty // true
195202
list.first // nil
196203
list.last // nil
197204

198-
list.append(value: "Hello")
205+
list.append("Hello")
199206
list.isEmpty
200207
list.first!.value // "Hello"
201208
list.last!.value // "Hello"
202209
list.count // 1
203210

204-
list.append(value: "World")
211+
list.append("World")
205212
list.first!.value // "Hello"
206213
list.last!.value // "World"
207214
list.count // 2
@@ -211,24 +218,24 @@ list.first!.next!.value // "World"
211218
list.last!.previous!.value // "Hello"
212219
list.last!.next // nil
213220

214-
list.nodeAt(0)!.value // "Hello"
215-
list.nodeAt(1)!.value // "World"
216-
list.nodeAt(2) // nil
221+
list.node(atIndex: 0)!.value // "Hello"
222+
list.node(atIndex: 1)!.value // "World"
223+
list.node(atIndex: 2) // nil
217224

218225
list[0] // "Hello"
219226
list[1] // "World"
220227
//list[2] // crash!
221228

222-
list.insert(value: "Swift", atIndex: 1)
229+
list.insert("Swift", atIndex: 1)
223230
list[0]
224231
list[1]
225232
list[2]
226233
print(list)
227234

228235
list.reverse() // [World, Swift, Hello]
229236

230-
list.nodeAt(0)!.value = "Universe"
231-
list.nodeAt(1)!.value = "Swifty"
237+
list.node(atIndex: 0)!.value = "Universe"
238+
list.node(atIndex: 1)!.value = "Swifty"
232239
let m = list.map { s in s.characters.count }
233240
m // [8, 6, 5]
234241
let f = list.filter { s in s.characters.count > 5 }
@@ -237,7 +244,7 @@ f // [Universe, Swifty]
237244
//list.removeAll()
238245
//list.isEmpty
239246

240-
list.remove(node: list.first!) // "Hello"
247+
list.remove(node: list.first!) // "Hello"
241248
list.count // 2
242249
list[0] // "Swift"
243250
list[1] // "World"
@@ -246,5 +253,5 @@ list.removeLast() // "World"
246253
list.count // 1
247254
list[0] // "Swift"
248255

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

Linked List/LinkedList.swift

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
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 init() {}
17+
18+
public var isEmpty: Bool {
2219
return head == nil
2320
}
24-
25-
open var first: Node? {
21+
22+
public var first: Node? {
2623
return head
2724
}
28-
29-
open var last: Node? {
25+
26+
public var last: Node? {
3027
if var node = head {
3128
while case let next? = node.next {
3229
node = next
@@ -36,8 +33,8 @@ open class LinkedList<T> {
3633
return nil
3734
}
3835
}
39-
40-
open var count: Int {
36+
37+
public var count: Int {
4138
if var node = head {
4239
var c = 1
4340
while case let next? = node.next {
@@ -49,8 +46,8 @@ open class LinkedList<T> {
4946
return 0
5047
}
5148
}
52-
53-
open func nodeAt(_ index: Int) -> Node? {
49+
50+
public func node(atIndex index: Int) -> Node? {
5451
if index >= 0 {
5552
var node = head
5653
var i = index
@@ -62,14 +59,14 @@ open class LinkedList<T> {
6259
}
6360
return nil
6461
}
65-
66-
open subscript(index: Int) -> T {
67-
let node = nodeAt(index)
62+
63+
public subscript(index: Int) -> T {
64+
let node = self.node(atIndex: index)
6865
assert(node != nil)
6966
return node!.value
7067
}
71-
72-
open func append(_ value: T) {
68+
69+
public func append(_ value: T) {
7370
let newNode = Node(value: value)
7471
if let lastNode = last {
7572
newNode.previous = lastNode
@@ -78,67 +75,67 @@ open class LinkedList<T> {
7875
head = newNode
7976
}
8077
}
81-
82-
fileprivate func nodesBeforeAndAfter(_ index: Int) -> (Node?, Node?) {
78+
79+
private func nodesBeforeAndAfter(index: Int) -> (Node?, Node?) {
8380
assert(index >= 0)
84-
81+
8582
var i = index
8683
var next = head
8784
var prev: Node?
88-
85+
8986
while next != nil && i > 0 {
9087
i -= 1
9188
prev = next
9289
next = next!.next
9390
}
9491
assert(i == 0) // if > 0, then specified index was too large
95-
92+
9693
return (prev, next)
9794
}
98-
99-
open func insert(_ value: T, atIndex index: Int) {
100-
let (prev, next) = nodesBeforeAndAfter(index)
101-
95+
96+
public func insert(_ value: T, atIndex index: Int) {
97+
let (prev, next) = nodesBeforeAndAfter(index: index)
98+
10299
let newNode = Node(value: value)
103100
newNode.previous = prev
104101
newNode.next = next
105102
prev?.next = newNode
106103
next?.previous = newNode
107-
104+
108105
if prev == nil {
109106
head = newNode
110107
}
111108
}
112-
113-
open func removeAll() {
109+
110+
public func removeAll() {
114111
head = nil
115112
}
116-
117-
open func remove(_ node: Node) -> T {
113+
114+
public func remove(node: Node) -> T {
118115
let prev = node.previous
119116
let next = node.next
120-
117+
121118
if let prev = prev {
122119
prev.next = next
123120
} else {
124121
head = next
125122
}
126123
next?.previous = prev
127-
124+
128125
node.previous = nil
129126
node.next = nil
130127
return node.value
131128
}
132-
133-
open func removeLast() -> T {
129+
130+
public func removeLast() -> T {
134131
assert(!isEmpty)
135-
return remove(last!)
132+
return remove(node: last!)
136133
}
137-
138-
open func removeAt(_ index: Int) -> T {
139-
let node = nodeAt(index)
134+
135+
public func remove(atIndex index: Int) -> T {
136+
let node = self.node(atIndex: index)
140137
assert(node != nil)
141-
return remove(node!)
138+
return remove(node: node!)
142139
}
143140
}
144141

@@ -167,17 +164,17 @@ extension LinkedList {
167164
}
168165

169166
extension LinkedList {
170-
public func map<U>(_ transform: (T)-> U) -> LinkedList<U> {
167+
public func map<U>(transform: (T) -> U) -> LinkedList<U> {
171168
let result = LinkedList<U>()
172169
var node = head
173170
while node != nil {
174-
result.append(transform(node!.value))
171+
result.append(transform(node!.value))
175172
node = node!.next
176173
}
177174
return result
178175
}
179-
180-
public func filter(_ predicate: (T)-> Bool) -> LinkedList<T> {
176+
177+
public func filter(predicate: (T) -> Bool) -> LinkedList<T> {
181178
let result = LinkedList<T>()
182179
var node = head
183180
while node != nil {
@@ -189,3 +186,13 @@ extension LinkedList {
189186
return result
190187
}
191188
}
189+
190+
extension LinkedList {
191+
convenience init(array: Array<T>) {
192+
self.init()
193+
194+
for element in array {
195+
self.append(element)
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)