Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class Main {
- static PrintWriter pw;
- public static void main(String[] args) throws FileNotFoundException {
- Scanner in = new Scanner(new File("input.txt"));
- pw = new PrintWriter(new File("output.txt"));
- MyQueue queue = new MyQueue(100000);
- String command;
- int s;
- do {
- command = in.next();
- switch (command) {
- case "push":
- queue.push(in.nextInt());
- break;
- case "pop":
- s = queue.pop(queue.size());
- pw.println((s == -999999999) ? ("error") : (s));
- break;
- case "front":
- pw.println((queue.front(queue.size()) == -999999999) ? ("error") : queue.front(queue.size()));
- break;
- case "size":
- pw.println(queue.size());
- break;
- case "clear":
- queue.clear();
- break;
- default:
- queue.exit();
- }
- } while (!command.equals("exit"));
- in.close();
- pw.close();
- }
- static class MyQueue {
- int maxSize;
- int[] queArray;
- int front;
- int rear;
- int nItems;
- public MyQueue(int sizeQueue) {
- maxSize = sizeQueue;
- queArray = new int[maxSize];
- front = 0;
- rear = -1;
- nItems = 0;
- }
- public void push(int element) {
- if (rear == maxSize - 1) rear = -1;
- queArray[++rear] = element;
- nItems++;
- pw.println("ok");
- }
- public int pop(int size) {
- if (size != 0) {
- int temp = queArray[front++];
- if (front == maxSize) {
- front = 0;
- rear = -1;
- nItems = 1;
- }
- nItems--;
- return temp;
- } else return -999999999;
- }
- public int front(int size) {
- if (size != 0) return (queArray[front]);
- else return -999999999;
- }
- public int size() {
- return nItems;
- }
- public void clear() {
- front = 0;
- rear = -1;
- nItems = 0;
- pw.println("ok");
- }
- public void exit() {
- pw.println("bye");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement