Skip to content

Commit

Permalink
Feat:Add solution of #118
Browse files Browse the repository at this point in the history
  • Loading branch information
송다경 authored and 송다경 committed Jul 29, 2022
1 parent c72ae3c commit 44918d0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/BaekJoon/sa11k/10845/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.io.*;
import java.util.*;

class Main {

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
ArrayDeque<Integer> queue = new ArrayDeque<>();

for(int i = 0; i<N; i++){
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String command = st.nextToken();
if(command.equals("push")) queue.add(Integer.parseInt(st.nextToken()));
else if(command.equals("pop")){
if (queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.poll()).append("\n");
}
else if (command.equals("size")) sb.append(queue.size()).append("\n");
else if (command.equals("empty")){
if (queue.isEmpty()) sb.append(1).append("\n");
else sb.append(0).append("\n");
}
else if(command.equals("front")){
if (queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekFirst()).append("\n");
}
else if(command.equals("back")){
if (queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekLast()).append("\n");
}
}

System.out.println(sb);


br.close();
}
}
45 changes: 45 additions & 0 deletions src/BaekJoon/sa11k/10866/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.*;
import java.util.*;

class Main {

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
ArrayDeque<Integer> queue = new ArrayDeque<>();

for(int i = 0; i<N; i++){
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String command = st.nextToken();
if(command.equals("push_front")) queue.addFirst(Integer.parseInt(st.nextToken()));
else if(command.equals("push_back")) queue.addLast(Integer.parseInt(st.nextToken()));
else if(command.equals("pop_front")){
if (queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.pollFirst()).append("\n");
}
else if(command.equals("pop_back")){
if (queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.pollLast()).append("\n");
}
else if (command.equals("size")) sb.append(queue.size()).append("\n");
else if (command.equals("empty")){
if (queue.isEmpty()) sb.append(1).append("\n");
else sb.append(0).append("\n");
}
else if(command.equals("front")){
if (queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekFirst()).append("\n");
}
else if(command.equals("back")){
if (queue.isEmpty()) sb.append(-1).append("\n");
else sb.append(queue.peekLast()).append("\n");
}
}

System.out.println(sb);


br.close();
}
}

0 comments on commit 44918d0

Please sign in to comment.