hwangnk1004 / Algorithm

0 stars 0 forks source link

BAEKJOON #큐 #297

Open hwangnk1004 opened 4 years ago

hwangnk1004 commented 4 years ago

import java.util.Scanner;

public class Main {

public static class Queque {

    int arr[];
    int num;

    public Queque(int n) {
        num = 0;
        arr = new int[n];
    }

    public void push(int s) {
        arr[num++] = s;
    }
    public void front() {
        if (num ==0) {
            System.out.println(-1);
        } else {
            System.out.println(arr[0]);
        }
    }
    public void back() {
        if (num ==0) {
            System.out.println(-1);
        } else {
            System.out.println(arr[num-1]);
        }
    }
    public void size() {
        System.out.println(num);
    }
    public void empty() {
        if (num ==0) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }
    }

    public void pop() {
        if (num ==0 ) {
            System.out.println(-1);
        } else {
            System.out.println(arr[0]);
            for(int i=0; i<num; i++){
                arr[i] = arr[i+1];
            }
            arr[num] = 0;
            num--;
        }
    }
}

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    Queque queue = new Queque(n);
    sc.nextLine();

    for(int i=0; i<n; i++){
        String line = sc.nextLine();
        String arr2[] = line.split(" ");

        String check = arr2[0];
        switch (check) {
            case "push" :
                queue.push(Integer.parseInt(arr2[1]));
                break;
            case "front" :
                queue.front();
                break;
            case "back" :
                queue.back();
                break;
            case "size" :
                queue.size();
                break;
            case "empty" :
                queue.empty();
                break;
            case "pop" :
                queue.pop();
                break;
        }
    }

}

}