-
Notifications
You must be signed in to change notification settings - Fork 0
/
10866.java
101 lines (89 loc) · 1.8 KB
/
10866.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;
class DequeClass {
Deque<Integer> deque;
public DequeClass() {
deque = new LinkedList<>();
}
public void push_front(int x) {
deque.addFirst(x);
}
public void push_back(int x) {
deque.addLast(x);
}
public int pop_front() {
if(size()==0) {
return -1;
} else {
return deque.pop();
}
}
public int pop_back() {
if(size() == 0) {
return -1;
} else {
return deque.removeLast();
}
}
public int size() {
return deque.size();
}
public int empty() {
if(deque.isEmpty()==true) {
return 1;
} else {
return 0;
}
}
public int front() {
if(size()==0) {
return -1;
} else {
return deque.getFirst();
}
}
public int back() {
if(size() == 0) {
return -1;
} else {
return deque.getLast();
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
DequeClass dec = new DequeClass();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for(int i=0; i<n; i++) {
String [] s = br.readLine().split(" ");
if(s[0].equals("push_front")) {
dec.push_front(Integer.parseInt(s[1]));
}
if(s[0].equals("push_back")) {
dec.push_back(Integer.parseInt(s[1]));
}
if(s[0].equals("front")) {
System.out.println(dec.front());
}
if(s[0].equals("back")) {
System.out.println(dec.back());
}
if(s[0].equals("pop_front")) {
System.out.println(dec.pop_front());
}
if(s[0].equals("pop_back")) {
System.out.println(dec.pop_back());
}
if(s[0].equals("empty")) {
System.out.println(dec.empty());
}
if(s[0].equals("size")) {
System.out.println(dec.size());
}
}
}
}