Last active
April 22, 2017 10:45
-
-
Save aratkevich/44c49b6dbc3e7b67d5cfae97fa3840db to your computer and use it in GitHub Desktop.
List in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node<T> { | |
var value: T | |
weak var parent: Node? | |
var children: [Node] = [] | |
init(value: T) { | |
self.value = value | |
} | |
func add(child: Node) { | |
children.append(child) | |
child.parent = self | |
} | |
} | |
extension Node where T: Equatable { | |
func search(value: T) -> Node? { | |
if value == self.value { | |
return self | |
} | |
for child in children { | |
if let found = child.search(value: value) { | |
return found | |
} | |
} | |
return nil | |
} | |
func search(value: T) -> Node? { | |
if value == self.value { | |
return self | |
} | |
for child in children { | |
if let found = child.search(value: value) { | |
return found | |
} | |
} | |
return nil | |
} | |
} | |
// Linked List Data Structure | |
public class Node<T> { | |
var value: T | |
var next: Node<T>? | |
weak var previous: Node<T>? | |
init(value: T) { | |
self.value = value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment