Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- LinkedList<Integer> deque = new LinkedList<>();
- int size = 0;
- String command;
- while (true) {
- command = sc.nextLine();
- if (command.contains("push_front")) {
- deque.addFirst(Integer.parseInt(command.substring(11)));
- size++;
- System.out.println("ok");
- }
- if (command.contains("push_back")) {
- deque.addLast(Integer.parseInt(command.substring(10)));
- size++;
- System.out.println("ok");
- }
- if (command.equals("pop_front")) {
- if (size <= 0) {
- System.out.println("error");
- } else {
- System.out.println(deque.removeFirst());
- size--;
- }
- }
- if (command.equals("pop_back")) {
- if (size <= 0) {
- System.out.println("error");
- } else {
- System.out.println(deque.removeLast());
- size--;
- }
- }
- if (command.equals("front")) {
- if (size <= 0) {
- System.out.println("error");
- } else {
- System.out.println(deque.getFirst());
- }
- }
- if (command.equals("back")) {
- if (size <= 0) {
- System.out.println("error");
- } else {
- System.out.println(deque.getLast());
- }
- }
- if (command.equals("size")) {
- System.out.println(size);
- }
- if (command.equals("clear")) {
- if (size == 0) {
- System.out.println("ok");
- } else {
- for (int i = 0; i < size; i++) {
- deque.removeFirst();
- }
- size = 0;
- System.out.println("ok");
- }
- }
- if (command.contains("exit")) {
- System.out.println("bye");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment