Skip to content

Commit

Permalink
Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
suparna-khamaru authored Jun 11, 2020
1 parent e3709cb commit 1898863
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Queue.swift
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]

0 comments on commit 1898863

Please sign in to comment.