-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay77.java
More file actions
55 lines (48 loc) · 1.38 KB
/
Day77.java
File metadata and controls
55 lines (48 loc) · 1.38 KB
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
import java.util.*;
public class Day77 {
private int[] queue;
private int front;
private int rear;
private int capacity;
public QueueUsingArray(int capacity) {
this.capacity = capacity;
this.queue = new int[capacity];
this.front = 0;
this.rear = -1;
}
public void enqueue(int element) {
if (rear == capacity - 1) {
System.out.println("Queue Overflow");
return;
}
queue[++rear] = element;
}
public int dequeue() {
if (front > rear) {
System.out.println("Queue Underflow");
return -1;
}
int element = queue[front++];
return element;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
QueueUsingArray queue = new QueueUsingArray(queries);
List<Integer> result = new ArrayList<>();
while (queries-- > 0) {
int type = scanner.nextInt();
if (type == 1) {
int element = scanner.nextInt();
queue.enqueue(element);
} else if (type == 2) {
int dequeued = queue.dequeue();
result.add(dequeued);
}
}
for (int val : result) {
System.out.println(val);
}
scanner.close();
}
}