-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathQueueArray.swift
42 lines (35 loc) · 861 Bytes
/
QueueArray.swift
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
//Stack
//by Sayed Mahmudul Alam
struct QueueArray {
private var queueSize: Int?
private var front = 0
private var rear = -1
private var array: Array<Int?>
init(queueSize: Int) {
self.queueSize = queueSize
array = Array(repeating: nil, count: queueSize)
}
mutating func enqueue(data: Int) {
if rear < queueSize! {
rear += 1
array[rear] = data
} else {
print("Queue is full")
}
}
mutating func dequeue() -> Int? {
let temp = array[front]
if rear >= 0 {
front += 1
return temp
} else {
return nil
}
}
mutating func peekFront() -> Int {
return array[front]!
}
mutating func isEmpty() -> Bool {
return queueSize == 0
}
}