-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue2.java
More file actions
82 lines (67 loc) · 1.95 KB
/
Copy pathQueue2.java
File metadata and controls
82 lines (67 loc) · 1.95 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.example.Algorithm.test;
import java.io.*;
import java.util.StringTokenizer;
public class Queue2 {
private static final int FIRST = 0;
static int[] queue;
static int front = 0;
static int back = 0;
static int size = 0;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
queue = new int[N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
switch (st.nextToken()) {
case "push": push(Integer.parseInt(st.nextToken())); break;
case "pop": pop(); break;
case "size": size(); break;
case "empty": empty(); break;
case "front": front(); break;
case "back": back(); break;
}
}
System.out.println(sb);
}
public static void push(int data) {
queue[back] = data;
back++;
size++;
}
public static void pop() {
if (size == 0) {
sb.append(-1).append("\n");
} else {
sb.append(queue[front]).append("\n");
size--;
front++;
}
}
public static void size() {
sb.append(size).append("\n");
}
public static void empty() {
if (size == 0) {
sb.append(1).append("\n");
} else {
sb.append(0).append("\n");
}
}
public static void front() {
if (size == 0) {
sb.append(-1).append("\n");
} else {
sb.append(queue[front]).append("\n");
}
}
public static void back() {
if (size == 0) {
sb.append(-1).append("\n");
} else {
sb.append(queue[back - 1]).append("\n");
}
}
}