Skip to content

Instantly share code, notes, and snippets.

@davidinga
davidinga / SinglyLinkedList.swift
Last active May 30, 2019 17:12
Singly Linked List
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
@davidinga
davidinga / DoublyLinkedList.swift
Last active May 30, 2019 17:13
Doubly Linked List
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
@davidinga
davidinga / Stack.swift
Last active May 29, 2019 18:44
Stack with Array implementation
struct Stack<Element> {
fileprivate var array = [Element]()
public var isEmpty: Bool {
return array.isEmpty
}
public var count: Int {
return array.count
}
@davidinga
davidinga / StackWithLinkedList.swift
Created May 29, 2019 18:44
Stack with Linked List implementation
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!)
}
@davidinga
davidinga / Queue.swift
Created May 29, 2019 23:34
Queue with Linked List implementation
public struct Queue<Element> {
fileprivate let queue = SinglyLinkedList<Element>()
var isEmpty: Bool {
return queue.isEmpty
}
var count: Int {
return queue.count
}
@davidinga
davidinga / HashTable.swift
Last active June 17, 2019 20:15
Hash Table with Chaining. Chaining implemented with Linked List.
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
@davidinga
davidinga / HashSet.swift
Created June 5, 2019 22:04
Hash Set implemented with a Hash Table that only stores Keys.
struct HashSet<Element: Hashable> {
private var set: HashTableOnlyKeys<Element>
var isEmpty: Bool {
return set.isEmpty
}
var count: Int {
return set.count
}
@davidinga
davidinga / BinaryTree.swift
Last active June 7, 2019 00:01
Binary Tree
public indirect enum BinaryTree {
case node(
leftChild: BinaryTree,
rightChild: BinaryTree,
value: String
)
case empty
}
@davidinga
davidinga / BinaryTreeTraversal.swift
Created June 7, 2019 19:57
Traverses a binary tree data structure.
extension BinaryTree {
public func traverseInOrder() {
if case let .node(left, right, value) = self {
left.traverseInOrder()
print(value + " ", terminator: "")
right.traverseInOrder()
}
}
@davidinga
davidinga / IterativeNthFibonacci.swift
Last active June 8, 2019 20:36
Returns the nth number in the Fibonacci Sequence.
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
}