Skip to content

Commit 6863590

Browse files
committed
implemented the ExpressibleByArrayLiteral for the Linked List in a new extension instead of the class declaration for readability
1 parent 7a5700d commit 6863590

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ public class LinkedListNode<T> {
44
var value: T
55
var next: LinkedListNode?
66
weak var previous: LinkedListNode?
7-
7+
88
public init(value: T) {
99
self.value = value
1010
}
1111
}
1212

1313
public final class LinkedList<T> {
1414
public typealias Node = LinkedListNode<T>
15-
15+
1616
fileprivate var head: Node?
17-
17+
18+
public init() {}
19+
1820
public var isEmpty: Bool {
1921
return head == nil
2022
}
21-
23+
2224
public var first: Node? {
2325
return head
2426
}
25-
27+
2628
public var last: Node? {
2729
if var node = head {
2830
while case let next? = node.next {
@@ -33,7 +35,7 @@ public final class LinkedList<T> {
3335
return nil
3436
}
3537
}
36-
38+
3739
public var count: Int {
3840
if var node = head {
3941
var c = 1
@@ -46,7 +48,7 @@ public final class LinkedList<T> {
4648
return 0
4749
}
4850
}
49-
51+
5052
public func node(atIndex index: Int) -> Node? {
5153
if index >= 0 {
5254
var node = head
@@ -59,80 +61,88 @@ public final class LinkedList<T> {
5961
}
6062
return nil
6163
}
62-
64+
6365
public subscript(index: Int) -> T {
6466
let node = self.node(atIndex: index)
6567
assert(node != nil)
6668
return node!.value
6769
}
68-
70+
6971
public func append(_ value: T) {
7072
let newNode = Node(value: value)
73+
self.append(newNode)
74+
}
75+
76+
public func append(_ newNode: Node) {
7177
if let lastNode = last {
7278
newNode.previous = lastNode
7379
lastNode.next = newNode
7480
} else {
7581
head = newNode
7682
}
7783
}
78-
84+
7985
private func nodesBeforeAndAfter(index: Int) -> (Node?, Node?) {
8086
assert(index >= 0)
81-
87+
8288
var i = index
8389
var next = head
8490
var prev: Node?
85-
91+
8692
while next != nil && i > 0 {
8793
i -= 1
8894
prev = next
8995
next = next!.next
9096
}
9197
assert(i == 0) // if > 0, then specified index was too large
92-
98+
9399
return (prev, next)
94100
}
95-
101+
96102
public func insert(_ value: T, atIndex index: Int) {
97-
let (prev, next) = nodesBeforeAndAfter(index: index)
98-
99103
let newNode = Node(value: value)
104+
self.insert(newNode, atIndex: index)
105+
}
106+
107+
public func insert(_ newNode: Node, atIndex index: Int) {
108+
let (prev, next) = nodesBeforeAndAfter(index: index)
109+
100110
newNode.previous = prev
101111
newNode.next = next
102112
prev?.next = newNode
103113
next?.previous = newNode
104-
114+
105115
if prev == nil {
106116
head = newNode
107117
}
108118
}
109-
119+
110120
public func removeAll() {
111121
head = nil
112122
}
113-
114-
public func remove(node: Node) -> T {
123+
124+
@discardableResult public func remove(node: Node) -> T {
115125
let prev = node.previous
116126
let next = node.next
117-
127+
118128
if let prev = prev {
119129
prev.next = next
120130
} else {
121131
head = next
122132
}
123133
next?.previous = prev
124-
134+
125135
node.previous = nil
126136
node.next = nil
127137
return node.value
128138
}
129-
130-
public func removeLast() -> T {
139+
140+
@discardableResult public func removeLast() -> T {
131141
assert(!isEmpty)
132142
return remove(node: last!)
133143
}
134-
135-
public func remove(atIndex index: Int) -> T {
144+
145+
@discardableResult public func remove(atIndex index: Int) -> T {
136146
let node = self.node(atIndex: index)
137147
assert(node != nil)
138148
return remove(node: node!)
@@ -173,7 +183,7 @@ extension LinkedList {
173183
}
174184
return result
175185
}
176-
186+
177187
public func filter(predicate: (T) -> Bool) -> LinkedList<T> {
178188
let result = LinkedList<T>()
179189
var node = head
@@ -187,21 +197,23 @@ extension LinkedList {
187197
}
188198
}
189199

190-
extension LinkedList: ExpressibleByArrayLiteral {
200+
extension LinkedList {
191201
convenience init(array: Array<T>) {
192202
self.init()
193-
203+
194204
for element in array {
195205
self.append(element)
196206
}
197207
}
198-
208+
}
209+
210+
extension LinkedList: ExpressibleByArrayLiteral {
199211
public convenience init(arrayLiteral elements: T...) {
200-
self.init()
212+
self.init()
201213

202-
for element in elements {
203-
self.append(element)
204-
}
214+
for element in elements {
215+
self.append(element)
216+
}
205217
}
206218
}
207219

Linked List/LinkedList.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class LinkedListNode<T> {
88
}
99
}
1010

11-
public final class LinkedList<T>: ExpressibleByArrayLiteral {
11+
public final class LinkedList<T> {
1212
public typealias Node = LinkedListNode<T>
1313

1414
fileprivate var head: Node?
@@ -203,12 +203,14 @@ extension LinkedList {
203203
self.append(element)
204204
}
205205
}
206-
207-
public convenience init(arrayLiteral elements: T...) {
208-
self.init()
206+
}
207+
208+
extension LinkedList: ExpressibleByArrayLiteral {
209+
public convenience init(arrayLiteral elements: T...) {
210+
self.init()
209211

210-
for element in elements {
211-
self.append(element)
212-
}
212+
for element in elements {
213+
self.append(element)
213214
}
215+
}
214216
}

0 commit comments

Comments
 (0)