-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.swift
More file actions
43 lines (32 loc) · 848 Bytes
/
Queue.swift
File metadata and controls
43 lines (32 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// Queue.swift
//
//
// Created by Douglas MacbookPro on 9/16/17.
//
// Credit: https://www.raywenderlich.com/148141/swift-algorithm-club-swift-queue-data-structure
import Foundation
public struct Queue<T> {
fileprivate var list = LinkedList<T>()
public mutating func add(_ element: T) {
list.append(element)
}
public mutating func remove() -> T? {
guard !list.isEmpty, let element = list.first else { return nil }
list.remove(element)
return element.value
}
public func peek() -> T? {
return list.first?.value
}
public var isEmpty: Bool {
return list.isEmpty
}
}
extension Queue: CustomStringConvertible {
// 2
public var description: String {
// 3
return list.description
}
}