Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.*;
- import java.util.*;
- public class Deque {
- static int head = 26000;
- static int tail = 26000;
- static int[] a = new int[100];
- static PrintWriter out;
- static void push_front(int n) {
- a[tail % 100] = n;
- tail++;
- out.println("ok");
- }
- static void push_back(int n) {
- head--;
- a[head % 100] = n;
- out.println("ok");
- }
- public static void pop_front() {
- if(tail- head == 0) {
- out.println("error");
- return;
- }
- tail--;
- out.println(a[tail % 100]);
- }
- public static void pop_back() {
- if(tail - head == 0) {
- out.println("error");
- return;
- }
- int n = a[head % 100];
- head++;
- out.println(n);
- }
- public static void front() {
- if(tail - head == 0) {
- out.println("error");
- return;
- }
- out.println(a[(tail - 1) % 100]);
- }
- public static void back() {
- if(tail - head == 0) {
- out.println("error");
- return;
- }
- out.println(a[head % 100]);
- }
- public static void size() {
- out.println(Math.abs(tail - head));
- }
- public static void clear() {
- a = new int[100];
- head = 26000;
- tail = 26000;
- out.println("ok");
- }
- public static void exit() {
- out.println("bye");
- }
- public static void main(String[] args) throws FileNotFoundException {
- Scanner in = new Scanner(new File("input.txt"));
- out = new PrintWriter(new File("output.txt"));
- while(in.hasNext()) {
- String m = in.next();
- if (m.equals("push_front")) {
- push_front(in.nextInt());
- continue;
- }
- if (m.equals("push_back")) {
- push_back(in.nextInt());
- continue;
- }
- if (m.equals("pop_front")) {
- pop_front();
- continue;
- }
- if (m.equals("pop_back")) {
- pop_back();
- continue;
- }
- if (m.equals("front")) {
- front();
- continue;
- }
- if (m.equals("back")) {
- back();
- continue;
- }
- if (m.equals("size")) {
- size();
- continue;
- }
- if (m.equals("clear")) {
- clear();
- continue;
- }
- if (m.equals("exit")) {
- exit();
- in.close();
- out.close();
- return;
- }
- }
- in.close();
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement