Created
May 17, 2018 21:14
-
-
Save fitomad/3714b34de5cd469473aafb87e6c4efc4 to your computer and use it in GitHub Desktop.
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
import Foundation | |
public struct Queue<Element> | |
{ | |
private var storage: [Element] | |
public init() | |
{ | |
self.storage = [Element]() | |
} | |
public mutating func enqueue(_ element: Element) -> Void | |
{ | |
self.storage.append(element) | |
} | |
public mutating func dequeue() -> Element? | |
{ | |
return self.storage.removeFirst() | |
} | |
public func whosFirst() -> Element? | |
{ | |
return self.storage.first | |
} | |
} | |
// | |
// TEST | |
// | |
var queue = Queue<Int>() | |
queue.enqueue(1) | |
queue.enqueue(2) | |
queue.enqueue(3) | |
queue.enqueue(4) | |
queue.enqueue(5) | |
print(queue) | |
if let first = queue.whosFirst() | |
{ | |
print("First: \(first)") | |
} | |
print(queue) | |
if let sacar = queue.dequeue() | |
{ | |
print("Sacar: \(sacar)") | |
} | |
print(queue) | |
queue.enqueue(1) | |
print(queue) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment