class Node { var value: T? = nil var next: Node? = nil var prev: Node? = nil init() { } init(value: T) { self.value = value } } class Deque { var count: Int = 0 var head: Node var tail: Node init() { self.head = Node() self.tail = head } func isEmpty() -> Bool { return self.count == 0 } func push(_ value: T) { let node = Node(value: value) if self.isEmpty() { self.head = node self.tail = node } else { node.next = self.head self.head.prev = node self.head = node } self.count += 1 } func unshift(_ value: T) { let node = Node(value: value) if self.isEmpty() { self.head = node self.tail = node } else { node.prev = self.tail self.tail.next = node self.tail = node } self.count += 1 } func pop() -> T? { if self.isEmpty() { return nil } else if self.count == 1 { let temp: Node = self.head self.count -= 1 return temp.value } else { let temp: Node = self.head self.head = self.head.next! self.count -= 1 return temp.value } } func shift() -> T? { if self.isEmpty() { return nil } else if self.count == 1 { let temp: Node = self.tail self.count -= 1 return temp.value } else { let temp: Node = self.tail self.tail = self.tail.prev! self.count -= 1 return temp.value } } }