Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.