Skip to content

Instantly share code, notes, and snippets.

@spurscho
Last active January 22, 2020 01:11
Show Gist options
  • Save spurscho/a9f06e15433a8e889311544d36e75042 to your computer and use it in GitHub Desktop.
Save spurscho/a9f06e15433a8e889311544d36e75042 to your computer and use it in GitHub Desktop.
Queue_by_array
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