@@ -42,54 +42,60 @@ public final class LinkedList<T> {
4242
4343 /// Computed property to iterate through the linked list and return the last node in the list (if any)
4444 public var last : Node ? {
45- if var node = head {
46- while case let next? = node. next {
47- node = next
48- }
49- return node
50- } else {
45+ guard var node = head else {
5146 return nil
5247 }
48+
49+ while let next = node. next {
50+ node = next
51+ }
52+ return node
5353 }
5454
5555 /// Computed property to iterate through the linked list and return the total number of nodes
5656 public var count : Int {
57- if var node = head {
58- var c = 1
59- while case let next? = node. next {
60- node = next
61- c += 1
62- }
63- return c
64- } else {
57+ guard var node = head else {
6558 return 0
6659 }
60+
61+ var count = 1
62+ while let next = node. next {
63+ node = next
64+ count += 1
65+ }
66+ return count
6767 }
6868
6969 /// Function to return the node at a specific index. Crashes if index is out of bounds (0...self.count)
7070 ///
7171 /// - Parameter index: Integer value of the node's index to be returned
7272 /// - Returns: Optional LinkedListNode
73- public func node( atIndex index: Int ) -> Node ? {
74- if index >= 0 {
75- var node = head
76- var i = index
77- while node != nil {
78- if i == 0 { return node }
79- i -= 1
80- node = node!. next
81- }
73+ public func node( at index: Int ) -> Node {
74+ assert ( head != nil , " List is empty " )
75+ assert ( index >= 0 , " index must be greater than 0 " )
76+
77+ if index == 0 {
78+ return head!
79+ } else {
80+ var node = head!. next
81+ for _ in 1 ..< index {
82+ node = node? . next
83+ if node == nil {
84+ break
85+ }
86+ }
87+
88+ assert ( node != nil , " index is out of bounds. " )
89+ return node!
8290 }
83- return nil
8491 }
8592
8693 /// Subscript function to return the node at a specific index
8794 ///
8895 /// - Parameter index: Integer value of the requested value's index
8996 public subscript( index: Int ) -> T {
90- let node = self . node ( atIndex: index)
91- assert ( node != nil )
92- return node!. value
97+ let node = self . node ( at: index)
98+ return node. value
9399 }
94100
95101 /// Append a value to the end of the list
@@ -104,7 +110,7 @@ public final class LinkedList<T> {
104110 ///
105111 /// - Parameter node: The node containing the value to be appended
106112 public func append( _ node: Node ) {
107- let newNode = LinkedListNode ( value : node. value )
113+ let newNode = node
108114 if let lastNode = last {
109115 newNode. previous = lastNode
110116 lastNode. next = newNode
@@ -124,78 +130,57 @@ public final class LinkedList<T> {
124130 }
125131 }
126132
127- /// A private helper funciton to find the nodes before and after a specified index. Crashes if index is out of bounds (0...self.count)
128- ///
129- /// - Parameter index: Integer value of the index between the nodes.
130- /// - Returns: A tuple of 2 nodes before & after the specified index respectively.
131- private func nodesBeforeAndAfter( index: Int ) -> ( Node ? , Node ? ) {
132- assert ( index >= 0 )
133-
134- var i = index
135- var next = head
136- var prev : Node ?
137-
138- while next != nil && i > 0 {
139- i -= 1
140- prev = next
141- next = next!. next
142- }
143- assert ( i == 0 ) // if > 0, then specified index was too large
144-
145- return ( prev, next)
146- }
147-
148133 /// Insert a value at a specific index. Crashes if index is out of bounds (0...self.count)
149134 ///
150135 /// - Parameters:
151136 /// - value: The data value to be inserted
152137 /// - index: Integer value of the index to be insterted at
153- public func insert( _ value: T , atIndex index: Int ) {
138+ public func insert( _ value: T , at index: Int ) {
154139 let newNode = Node ( value: value)
155- self . insert ( newNode, atIndex : index)
140+ self . insert ( newNode, at : index)
156141 }
157142
158143 /// Insert a copy of a node at a specific index. Crashes if index is out of bounds (0...self.count)
159144 ///
160145 /// - Parameters:
161146 /// - node: The node containing the value to be inserted
162147 /// - index: Integer value of the index to be inserted at
163- public func insert( _ node: Node , atIndex index: Int ) {
164- let ( prev, next) = nodesBeforeAndAfter ( index: index)
165- let newNode = LinkedListNode ( value: node. value)
166- newNode. previous = prev
167- newNode. next = next
168- prev? . next = newNode
169- next? . previous = newNode
170-
171- if prev == nil {
148+ public func insert( _ newNode: Node , at index: Int ) {
149+ if index == 0 {
150+ newNode. next = head
151+ head? . previous = newNode
172152 head = newNode
153+ } else {
154+ let prev = node ( at: index- 1 )
155+ let next = prev. next
156+ newNode. previous = prev
157+ newNode. next = next
158+ next? . previous = newNode
159+ prev. next = newNode
173160 }
174161 }
175-
162+
176163 /// Insert a copy of a LinkedList at a specific index. Crashes if index is out of bounds (0...self.count)
177164 ///
178165 /// - Parameters:
179166 /// - list: The LinkedList to be copied and inserted
180167 /// - index: Integer value of the index to be inserted at
181- public func insert( _ list: LinkedList , atIndex index: Int ) {
168+ public func insert( _ list: LinkedList , at index: Int ) {
182169 if list. isEmpty { return }
183- var ( prev , next ) = nodesBeforeAndAfter ( index : index )
184- var nodeToCopy = list . head
185- var newNode : Node ?
186- while let node = nodeToCopy {
187- newNode = Node ( value : node . value )
188- newNode ? . previous = prev
189- if let previous = prev {
190- previous . next = newNode
191- } else {
192- self . head = newNode
193- }
194- nodeToCopy = nodeToCopy ? . next
195- prev = newNode
170+
171+ if index == 0 {
172+ list . last ? . next = head
173+ head = list . head
174+ } else {
175+ let prev = node ( at : index - 1 )
176+ let next = prev. next
177+
178+ prev . next = list . head
179+ list . head? . previous = prev
180+
181+ list . last ? . next = next
182+ next ? . previous = list . last ? . next
196183 }
197- prev? . next = next
198- next? . previous = prev
199184 }
200185
201186 /// Function to remove all nodes/value from the list
@@ -235,10 +220,9 @@ public final class LinkedList<T> {
235220 ///
236221 /// - Parameter index: Integer value of the index of the node to be removed
237222 /// - Returns: The data value contained in the deleted node
238- @discardableResult public func remove( atIndex index: Int ) -> T {
239- let node = self . node ( atIndex: index)
240- assert ( node != nil )
241- return remove ( node: node!)
223+ @discardableResult public func remove( at index: Int ) -> T {
224+ let node = self . node ( at: index)
225+ return remove ( node: node)
242226 }
243227}
244228
@@ -324,7 +308,7 @@ list.first // nil
324308list. last // nil
325309
326310list. append ( " Hello " )
327- list. isEmpty
311+ list. isEmpty // false
328312list. first!. value // "Hello"
329313list. last!. value // "Hello"
330314list. count // 1
@@ -339,9 +323,9 @@ list.first!.next!.value // "World"
339323list. last!. previous!. value // "Hello"
340324list. last!. next // nil
341325
342- list. node ( atIndex : 0 ) ! . value // "Hello"
343- list. node ( atIndex : 1 ) ! . value // "World"
344- list. node ( atIndex : 2 ) // nil
326+ list. node ( at : 0 ) . value // "Hello"
327+ list. node ( at : 1 ) . value // "World"
328+ // list.node(at : 2) // crash!
345329
346330list [ 0 ] // "Hello"
347331list [ 1 ] // "World"
@@ -354,35 +338,59 @@ list.append(list2) // [Hello, World, Goodbye, World]
354338list2. removeAll ( ) // [ ]
355339list2. isEmpty // true
356340list. removeLast ( ) // "World"
357- list. remove ( atIndex : 2 ) // "Goodbye"
341+ list. remove ( at : 2 ) // "Goodbye"
358342
359- list. insert ( " Swift " , atIndex : 1 )
343+ list. insert ( " Swift " , at : 1 )
360344list [ 0 ] // "Hello"
361345list [ 1 ] // "Swift"
362346list [ 2 ] // "World"
363347print ( list)
364348
365349list. reverse ( ) // [World, Swift, Hello]
366350
367- list. node ( atIndex : 0 ) ! . value = " Universe "
368- list. node ( atIndex : 1 ) ! . value = " Swifty "
369- let m = list. map { s in s. characters . count }
351+ list. node ( at : 0 ) . value = " Universe "
352+ list. node ( at : 1 ) . value = " Swifty "
353+ let m = list. map { s in s. count }
370354m // [8, 6, 5]
371- let f = list. filter { s in s. characters . count > 5 }
355+ let f = list. filter { s in s. count > 5 }
372356f // [Universe, Swifty]
373357
374358list. remove ( node: list. first!) // "Universe"
375359list. count // 2
376360list [ 0 ] // "Swifty"
377361list [ 1 ] // "Hello"
378362
363+ list. count // 2
379364list. removeLast ( ) // "Hello"
365+ list. head? . value
380366list. count // 1
381367list [ 0 ] // "Swifty"
382368
383- list. remove ( atIndex : 0 ) // "Swifty"
369+ list. remove ( at : 0 ) // "Swifty"
384370list. count // 0
385371
372+ let list3 = LinkedList < String > ( )
373+ list3. insert ( " 2 " , at: 0 ) // [2]
374+ list3. count // 1
375+ list3. insert ( " 4 " , at: 1 ) // [2,4]
376+ list3. count // 2
377+ list3. insert ( " 5 " , at: 2 ) // [2,4,5]
378+ list3. count // 3
379+ list3. insert ( " 3 " , at: 1 ) // [2,3,4,5]
380+ list3. insert ( " 1 " , at: 0 ) // [1,2,3,4,5]
381+
382+ let list4 = LinkedList < String > ( )
383+ list4. insert ( list3, at: 0 ) // [1,2,3,4,5]
384+ list4. count // 5
385+
386+ let list5 = LinkedList < String > ( )
387+ list5. append ( " 0 " ) // [0]
388+ list5. insert ( " End " , at: 1 ) // [0,End]
389+ list5. count // 2
390+ list5. insert ( list4, at: 1 ) // [0,1,2,3,4,5,End]
391+ list5. count // 7
392+
393+
386394let linkedList : LinkedList < Int > = [ 1 , 2 , 3 , 4 ] // [1, 2, 3, 4]
387395linkedList. count // 4
388396linkedList [ 0 ] // 1
0 commit comments