곽로그
[백준 10866] 덱 본문
반응형
import java.util.ArrayList;
import java.util.Scanner;
class Dequeue {
ArrayList<Integer> dequeue;
int front = 0;
int back=0;
public Dequeue() {
dequeue = new ArrayList<Integer>();
}
public void push_front(int X) {
dequeue.add(front, X);
back++;
}
public void push_back(int X) {
dequeue.add(back, X);
back++;
}
public int pop_front() {
if (dequeue.size() < 1) {
return -1;
} else {
back--;
return dequeue.remove(front);
}
}
public int pop_back() {
if (dequeue.size() < 1) {
return -1;
} else {
return dequeue.remove(--back);
}
}
public int size() {
return this.dequeue.size();
}
public int empty() {
if (dequeue.size() < 1) {
return 1;
} else {
return 0;
}
}
public int front() {
if (dequeue.size() < 1) {
return -1;
} else {
return dequeue.get(front);
}
}
public int back() {
if (dequeue.size() < 1) {
return -1;
} else {
return dequeue.get(back-1);
}
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
Dequeue dequeue = new Dequeue();
for (int i = 0; i < N; i++) {
String command = in.next();
if (command.equals("push_front")) {
int X = in.nextInt();
dequeue.push_front(X);
} else if (command.equals("push_back")) {
int X = in.nextInt();
dequeue.push_back(X);
} else if (command.equals("pop_front")) {
System.out.println(dequeue.pop_front());
} else if (command.equals("pop_back")) {
System.out.println(dequeue.pop_back());
} else if (command.equals("size")) {
System.out.println(dequeue.size());
} else if (command.equals("empty")) {
System.out.println(dequeue.empty());
} else if (command.equals("front")) {
System.out.println(dequeue.front());
} else if (command.equals("back")) {
System.out.println(dequeue.back());
} else {
System.out.println("나가리");
}
}
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준 10799] 쇠막대기 (0) | 2020.01.19 |
---|---|
[백준 1021] 회전하는 큐 (0) | 2020.01.12 |
[백준1966] 프린터 큐 (0) | 2020.01.12 |
[백준 10845] 큐 (0) | 2020.01.04 |
[백준 9012] 괄호 (0) | 2019.12.29 |
Comments