Last active
July 27, 2020 05:13
-
-
Save muzahid59/c8d5649ef5989d18ce980b08f644b352 to your computer and use it in GitHub Desktop.
Simple Generic Linklist data structure implemented by Swift. Reverse list, apply filter, map are available here.
This file contains hidden or 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
| class Node<T: Equatable> { | |
| var value: T | |
| var next: Node<T>? | |
| init(_ value: T) { | |
| self.value = value | |
| } | |
| } | |
| extension Node { | |
| func map<U>(completion: (T) -> U?) -> Node<U>? { | |
| if let tranformedValue = completion(value) { | |
| return Node<U>(tranformedValue) | |
| } | |
| return nil | |
| } | |
| } | |
| struct List<T: Equatable> { | |
| private var head: Node<T>? | |
| mutating func addLast(_ value: T) { | |
| let newNode = Node(value) | |
| if head == nil { | |
| head = newNode | |
| } else { | |
| var iterator = head | |
| while iterator?.next != nil { | |
| iterator = iterator?.next | |
| } | |
| iterator?.next = newNode | |
| } | |
| } | |
| mutating func addFirst(_ value: T) { | |
| let newNode = Node(value) | |
| if head == nil { | |
| head = newNode | |
| } else { | |
| newNode.next = head | |
| head = newNode | |
| } | |
| } | |
| mutating func reverse() { | |
| var prev: Node<T>? | |
| var current = head | |
| var next: Node<T>? | |
| while current != nil { | |
| next = current?.next | |
| current?.next = prev | |
| prev = current | |
| current = next | |
| } | |
| head = prev | |
| } | |
| } | |
| /// Init with Array literal | |
| extension List { | |
| init(_ items: T...) { | |
| var newList = List<T>() | |
| for item in items { | |
| newList.addLast(item) | |
| } | |
| head = newList.head | |
| } | |
| } | |
| extension List { | |
| /// Applying map | |
| func map<U>(_ completion: (T) -> U?) -> List<U> { | |
| var newList = List<U>() | |
| var iterator = head | |
| while let value = iterator?.value, let newValue = completion(value) { | |
| newList.addLast(newValue) | |
| iterator = iterator?.next | |
| } | |
| return newList | |
| } | |
| /// Applying filter | |
| func filter(_ completion: (T) -> Bool) -> List<T> { | |
| var newList = List<T>() | |
| var iterator = head | |
| while let value = iterator?.value { | |
| if completion(value) { | |
| newList.addLast(value) | |
| } | |
| iterator = iterator?.next | |
| } | |
| return newList | |
| } | |
| } | |
| extension List { | |
| func printList() { | |
| var values: [T] = [] | |
| var iterator = head | |
| while let value = iterator?.value { | |
| values.append(value) | |
| iterator = iterator?.next | |
| } | |
| print(values) | |
| } | |
| } | |
| var list = List<Int>() | |
| list.addLast(2) | |
| list.addLast(3) | |
| list.addFirst(1) | |
| print("Int List:") | |
| list.printList() | |
| print("Revered:") | |
| list.reverse() | |
| list.printList() | |
| print("Transformed List:") | |
| let transformedList = list.map { $0*$0 } | |
| transformedList.printList() | |
| //Init List with array literal | |
| var charaterList = List<String>("a", "b", "c") | |
| print("Revered:") | |
| charaterList.reverse() | |
| charaterList.printList() | |
| print("Apply filter:") | |
| let filterdList = charaterList.filter { $0 == "a" } | |
| filterdList.printList() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment