Advertisement
vov44k

Untitled

Nov 28th, 2022
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Main {
  8.  
  9.     static PrintWriter pw;
  10.  
  11.     public static void main(String[] args) throws FileNotFoundException {
  12.         Scanner in = new Scanner(new File("input.txt"));
  13.         pw = new PrintWriter(new File("output.txt"));
  14.         MyQueue queue = new MyQueue(100000);
  15.         String command;
  16.         int s;
  17.         do {
  18.             command = in.next();
  19.             switch (command) {
  20.                 case "push":
  21.                     queue.push(in.nextInt());
  22.                     break;
  23.                 case "pop":
  24.                     s = queue.pop(queue.size());
  25.                     pw.println((s == -999999999) ? ("error") : (s));
  26.                     break;
  27.                 case "front":
  28.                     pw.println((queue.front(queue.size()) == -999999999) ? ("error") : queue.front(queue.size()));
  29.                     break;
  30.                 case "size":
  31.                     pw.println(queue.size());
  32.                     break;
  33.                 case "clear":
  34.                     queue.clear();
  35.                     break;
  36.                 default:
  37.                     queue.exit();
  38.             }
  39.         } while (!command.equals("exit"));
  40.  
  41.         in.close();
  42.         pw.close();
  43.     }
  44.  
  45.     static class MyQueue {
  46.         int maxSize;
  47.         int[] queArray;
  48.         int front;
  49.         int rear;
  50.         int nItems;
  51.  
  52.         public MyQueue(int sizeQueue) {
  53.             maxSize = sizeQueue;
  54.             queArray = new int[maxSize];
  55.             front = 0;
  56.             rear = -1;
  57.             nItems = 0;
  58.         }
  59.  
  60.         public void push(int element) {
  61.             if (rear == maxSize - 1) rear = -1;
  62.             queArray[++rear] = element;
  63.             nItems++;
  64.             pw.println("ok");
  65.         }
  66.  
  67.         public int pop(int size) {
  68.             if (size != 0) {
  69.                 int temp = queArray[front++];
  70.                 if (front == maxSize) {
  71.                     front = 0;
  72.                     rear = -1;
  73.                     nItems = 1;
  74.                 }
  75.                 nItems--;
  76.                 return temp;
  77.             } else return -999999999;
  78.         }
  79.  
  80.         public int front(int size) {
  81.             if (size != 0) return (queArray[front]);
  82.             else return -999999999;
  83.  
  84.         }
  85.  
  86.         public int size() {
  87.             return nItems;
  88.         }
  89.  
  90.         public void clear() {
  91.             front = 0;
  92.             rear = -1;
  93.             nItems = 0;
  94.             pw.println("ok");
  95.         }
  96.  
  97.         public void exit() {
  98.             pw.println("bye");
  99.         }
  100.  
  101.     }
  102.  
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement