Last active
May 27, 2018 01:17
-
-
Save BrianLitwin/ac46948ee2a0c2e9e5805906d9a378a8 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
//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