Last active
January 22, 2020 01:11
-
-
Save spurscho/a9f06e15433a8e889311544d36e75042 to your computer and use it in GitHub Desktop.
Queue_by_array
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
public struct Queue<T> { | |
internal var data = Array<T>() | |
public init() {} | |
public mutating func dequeue() -> T? { | |
return data.removeFirst() | |
} | |
public func peek() -> T? { | |
return data.first | |
} | |
public mutating func enqueue(element: T) { | |
data.append(element) | |
} | |
public mutating func clear() { | |
data.removeAll() | |
} | |
public var count: Int { | |
return data.count | |
} | |
public var capacity: Int { | |
get { | |
return data.capacity | |
} | |
set { | |
data.reserveCapacity(newValue) | |
} | |
} | |
public func isFull() -> Bool { | |
return count == data.capacity | |
} | |
public func isEmpty() -> Bool { | |
return data.isEmpty | |
} | |
} | |
extension Queue: CustomStringConvertible { | |
public var description: String { | |
return data.description | |
} | |
} | |
var myQueue = Queue<Int>() | |
for i in 1 ... 5 { | |
myQueue.enqueue(element: i) | |
} | |
print(myQueue.description) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment