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
public class SinglyLinkedList<Element> { | |
private var head: SinglyLinkedListNode<Element>? | |
private var tail: SinglyLinkedListNode<Element>? | |
public var isEmpty: Bool { | |
return head == nil | |
} | |
public var count: Int { | |
var node = head |
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
public class DoublyLinkedList<Element> { | |
private var head: DoublyLinkedListNode<Element>? | |
private var tail: DoublyLinkedListNode<Element>? | |
public var first: DoublyLinkedListNode<Element>? { | |
return head | |
} | |
public var last: DoublyLinkedListNode<Element>? { | |
return tail |
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
struct Stack<Element> { | |
fileprivate var array = [Element]() | |
public var isEmpty: Bool { | |
return array.isEmpty | |
} | |
public var count: Int { | |
return array.count | |
} |
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
struct StackWithLinkedList<Element> { | |
let stack = DoublyLinkedList<Element>() | |
mutating func push(_ element: Element) { | |
stack.append(element) | |
} | |
mutating func pop() -> DoublyLinkedListNode<Element>? { | |
return stack.remove(node: stack.tail!) | |
} |
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
public struct Queue<Element> { | |
fileprivate let queue = SinglyLinkedList<Element>() | |
var isEmpty: Bool { | |
return queue.isEmpty | |
} | |
var count: Int { | |
return queue.count | |
} |
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
struct HashTable<Key: Hashable, Value> { | |
private typealias Element = (key: Key, value: Value) | |
private typealias List = SinglyLinkedList<Element> | |
private var table: [List] | |
private(set) public var count = 0 | |
var isEmpty: Bool { | |
return count == 0 |
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
struct HashSet<Element: Hashable> { | |
private var set: HashTableOnlyKeys<Element> | |
var isEmpty: Bool { | |
return set.isEmpty | |
} | |
var count: Int { | |
return set.count | |
} |
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
public indirect enum BinaryTree { | |
case node( | |
leftChild: BinaryTree, | |
rightChild: BinaryTree, | |
value: String | |
) | |
case empty | |
} |
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
extension BinaryTree { | |
public func traverseInOrder() { | |
if case let .node(left, right, value) = self { | |
left.traverseInOrder() | |
print(value + " ", terminator: "") | |
right.traverseInOrder() | |
} | |
} | |
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
func iterativeFib(_ n: Int) -> Int { | |
var n1 = 1, n2 = 1, answer = 0, temp = 0 | |
for _ in 2..<n { | |
answer = n1 + n2 | |
temp = n2 | |
n2 = answer | |
n1 = temp | |
} | |
return answer | |
} |
OlderNewer