fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5. static StringBuilder sb=new StringBuilder();
  6. static int front=-1;
  7. static int rear=-1;
  8. static int[] queue;
  9.  
  10. public static void main(String args[]) throws Exception {
  11. int num=Integer.parseInt(br.readLine());
  12. if(num<1||num>2000000) return;
  13. queue=new int[num];
  14. for(int i=0;i<num;i++) {
  15. String str=br.readLine();
  16. switch(str) {
  17. case "pop":
  18. sb.append(pop()+"\n");
  19. break;
  20. case "size":
  21. sb.append(size()+"\n");
  22. break;
  23. case "empty":
  24. sb.append(empty()+"\n");
  25. break;
  26. case "front":
  27. sb.append(front()+"\n");
  28. break;
  29. case "back":
  30. sb.append(back()+"\n");
  31. break;
  32. default:
  33. if(str.startsWith("push")) {
  34. int temp=str.charAt(5)-'0';
  35. if(temp<1||temp>100000) return;
  36. push(temp);
  37. }
  38. }
  39. }
  40. System.out.println(sb);
  41. }
  42.  
  43. public static void push(int num) {
  44. queue[++rear]=num;
  45. }
  46.  
  47. public static int pop() {
  48. if(rear>front) return queue[++front];
  49. else return -1;
  50. }
  51.  
  52. public static int size() {
  53. return rear-front;
  54. }
  55.  
  56. public static int empty() {
  57. if(size()<=0) return 1;
  58. else return 0;
  59. }
  60.  
  61. public static int front(){
  62. if(size()<=0) return -1;
  63. else return queue[front+1];
  64. }
  65.  
  66. public static int back() {
  67. if(size()<=0) return -1;
  68. else return queue[rear];
  69. }
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
Success #stdin #stdout 0.08s 34092KB
stdin
2
push 99
front
stdout
9