Skip to content

Instantly share code, notes, and snippets.

@BrianLitwin
Last active May 27, 2018 01:17
Show Gist options
  • Save BrianLitwin/ac46948ee2a0c2e9e5805906d9a378a8 to your computer and use it in GitHub Desktop.
Save BrianLitwin/ac46948ee2a0c2e9e5805906d9a378a8 to your computer and use it in GitHub Desktop.
//First-in first-out
//concession-stand line
//i.e. enqueue(4), enqueue(43), enqueue(3), enqueue(2)
//-> dequeue() returns 4
//append items and take items from [0] or .first to get next item
//or: add items to beginning at index 0 and rettrieve items from the end
var numbers = [1,2,3]
func enqueue(n: Int) {
numbers.append(n)
}
func dequeue() -> Int? {
guard let first = numbers.first else { return nil }
numbers.remove(at: 0)
return first
}
//shorthand: append() and popFirst()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment