/
QueueUsingStacks.swift
55 lines (49 loc) · 1.13 KB
/
QueueUsingStacks.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
//
// QueueUsingStacks.swift
//
import Foundation
var stack1 = [Int](); var stack2 = [Int]()
func queue(_ q: Int, _ e: Int?){
switch q {
case 1:
stack1.append(e!)
break
case 2:
if !stack2.isEmpty{
stack2.popLast()
}
else{
while !stack1.isEmpty{
stack2.append(stack1.popLast()!)
}
stack2.popLast()
}
break
case 3:
if !stack2.isEmpty{
print(stack2.last!)
}
else{
while !stack1.isEmpty{
stack2.append(stack1.popLast()!)
}
print(stack2.last!)
}
break
default:
print("default")
break
}
}
guard let t = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
for tItr in 1...t {
guard let expression = readLine() else { fatalError("Bad input") }
let arr = expression.split(separator: " ")
if arr.count == 2 {
queue(Int(arr[0])!, Int(arr[1])!)
}
else{
queue(Int(arr[0])!, 0)
}
}