Created
July 30, 2019 06:37
-
-
Save tifoaudii/7c51cf80d446e065d89167fdb3239cd5 to your computer and use it in GitHub Desktop.
Create a linkedlist class that consist of more than one node objects
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 Linkedlist<Value> { | |
public var head: Node<Value>? | |
public var tail: Node<Value>? | |
public var isEmpty: Bool { | |
return head == nil | |
} | |
init() {} | |
public mutating func push(_ value: Value) { | |
head = Node(value: value, next: head) | |
if tail == nil { | |
tail = head | |
} | |
} | |
} | |
extension Linkedlist: CustomStringConvertible { | |
public var description: String { | |
guard let head = head else { | |
return "empty data" | |
} | |
return "\(head)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment