Last active
September 29, 2019 12:19
-
-
Save PetreVane/2b89f3fc4fb4d0ddf070398cbd3ea43e to your computer and use it in GitHub Desktop.
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
struct Queue<Element> { | |
private var elements: [Element] = [] | |
var description: String { | |
return "Your queue has \(elements.count) items: \(elements)" | |
} | |
mutating func enqueue(newElement: Element) { | |
elements.append(newElement) | |
} | |
mutating func dequeue() -> Element? { | |
guard !elements.isEmpty else { return nil } | |
return elements.remove(at: 0) | |
} | |
} | |
var intQueue = Queue<Int>() | |
intQueue.enqueue(newElement: 2) | |
intQueue.enqueue(newElement: 20) | |
intQueue.description | |
var stringQueue = Queue<String>() | |
stringQueue.enqueue(newElement: "Hello world") | |
stringQueue.enqueue(newElement: "How are you?") | |
stringQueue.description | |
stringQueue.dequeue() | |
stringQueue.description |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment