import Foundation struct LinkedList<Value> { var head : Node<Value>? var tail : Node<Value>? init() {} func isEmpty() -> Bool { return head == nil } mutating func push(_ value: Value) { head = Node(value: value, next: head) if tail == nil { tail = head } } mutating func append(_ value : Value) { } } class Node<Value> { var value :Value var next : Node? init(value:Value, next:Node? = nil ) { self.value = value self.next = next } } extension LinkedList : CustomStringConvertible { var description : String { guard let head = head else { return " Empty List" } return String(describing: head) } } extension Node : CustomStringConvertible { var description : String { guard let next = next else { return "\(value)" } return "\(value) -> " + String(describing : next) + " " } } var list = LinkedList<Int>(); list.push(1) list.push(2) list.push(3) list.push(5) print(list)
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more