-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3709cb
commit 1898863
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import UIKit | ||
|
||
struct Queue<T> { | ||
|
||
var array: [T] = [] | ||
init() {} | ||
|
||
var isEmpty: Bool { | ||
return array.isEmpty | ||
} | ||
|
||
var peek: T? { | ||
return array.first | ||
} | ||
|
||
mutating func enqueue(_ element: T) -> Bool { | ||
array.append(element) | ||
return true | ||
} | ||
|
||
mutating func dequeue() -> T? { | ||
return isEmpty ? nil : array.removeFirst() | ||
} | ||
} | ||
|
||
extension Queue: CustomStringConvertible { | ||
var description: String { | ||
return String(describing: array) | ||
} | ||
} | ||
|
||
var queue = Queue<Int>() | ||
|
||
queue.enqueue(10) | ||
queue.enqueue(20) | ||
queue.enqueue(30) | ||
queue.enqueue(40) | ||
print(queue) //[10, 20, 30, 40] | ||
|
||
queue.dequeue() | ||
print(queue) //[20, 30, 40] |