Contains

To make this LinkedList a little more interesting, we will develop a contains method similar to the one that we developed for the BST:

static func contains(_ key: Element, list: LinkedList<Element>) -> Bool { 
switch list {
case .end:
return false
case .node(let data, let next):
if key == data {
return true
} else {
return contains(key, list: next)
}
}
}

This method recursively checks for a specific element in LinkedList and returns true if it finds the element:

print(LinkedList.contains(1, list: functionalLinkedList)) 

The result of this expression is going to be true.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset