-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path232_queue_using_stacks.swift
65 lines (57 loc) · 1.16 KB
/
232_queue_using_stacks.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class MyQueue {
var head: Int?
var tail: Int?
var array: [Int]
var count: Int
init() {
head = nil
tail = nil
array = []
count = 0
}
func push(_ x: Int) {
if count == 0 {
head = x
}
tail = x
array.append(x)
count += 1
}
func pop() -> Int {
if count > 1 {
var temp = array[0]
head = array[1]
array.removeFirst()
count -= 1
return temp
} else if count == 1 {
var temp = array[0]
array.removeFirst()
head = nil
count -= 1
return temp
}
count
return 0
}
func peek() -> Int {
if count != 0 {
return head!
}
return 0
}
func empty() -> Bool {
if array.count == 0 {
return true
}
return false
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* let obj = MyQueue()
* obj.push(x)
* let ret_2: Int = obj.pop()
* let ret_3: Int = obj.peek()
* let ret_4: Bool = obj.empty()
*/