Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Queues {
  5.     public static void main(String [] args) throws Exception {
  6.         new Queues();
  7.     }
  8.    
  9.     BufferedReader in;
  10.     PrintWriter out;
  11.     StringTokenizer st;
  12.    
  13.     int[] s = new int[2000000];
  14.     int left = 0, right = 0;
  15.    
  16.     String command = "";
  17.    
  18.     private String next() throws Exception {
  19.         if (st == null || !st.hasMoreElements())
  20.             st = new StringTokenizer(in.readLine());
  21.        
  22.         return st.nextToken();
  23.     }
  24.     private int nextInt() throws Exception {
  25.         return Integer.parseInt(next());
  26.     }
  27.    
  28.     public Queues() throws Exception {
  29.         in = new BufferedReader(new FileReader("input.txt"));
  30.         out = new PrintWriter(new FileWriter("output.txt"));
  31.        
  32.         solve();
  33.         out.close();
  34.     }
  35.    
  36.     private void solve() throws Exception {
  37.         while (true) {
  38.             command = next();
  39.            
  40.             if (command.equals("exit")) {
  41.                 out.println("bye");
  42.                 break;
  43.             }
  44.            
  45.             if (command.equals("push")) {
  46.                 int val = nextInt();
  47.                 push(val);
  48.             } else if (command.equals("pop")) {
  49.                 pop();
  50.             } else if (command.equals("front")) {
  51.                 front();
  52.             } else if (command.equals("size")) {
  53.                 size();
  54.             } else if (command.equals("clear")) {
  55.                 clear();
  56.             }
  57.         }
  58.     }
  59.    
  60.     private void push(int n) {
  61.         s[right] = n;
  62.         right++;
  63.         out.println("ok");
  64.     }
  65.    
  66.     private void pop() {
  67.         if (right - left >0) {
  68.             int res = s[left];
  69.             left++;
  70.             out.println(res);
  71.         } else {
  72.             out.println("error");
  73.         }
  74.     }
  75.    
  76.     private void front() {
  77.         if (right - left > 0)
  78.             out.println(s[left]);
  79.         else
  80.             out.println("error");
  81.     }
  82.    
  83.     private void size() {
  84.         out.println(right - left);
  85.     }
  86.    
  87.     private void clear() {
  88.         right = left = 0;
  89.         out.println("ok");
  90.     }
  91. }
Add Comment
Please, Sign In to add comment