Skip to content

Instantly share code, notes, and snippets.

@aratkevich
Last active April 22, 2017 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aratkevich/44c49b6dbc3e7b67d5cfae97fa3840db to your computer and use it in GitHub Desktop.
Save aratkevich/44c49b6dbc3e7b67d5cfae97fa3840db to your computer and use it in GitHub Desktop.
List in Swift
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