hwangnk1004 / Algorithm

0 stars 0 forks source link

BAEKJOON #스택 #296

Open hwangnk1004 opened 4 years ago

hwangnk1004 commented 4 years ago

import java.util.Scanner;

public class Main { public static class Stack {

    String arr[];
    int num;

    public Stack(int n) {
        num = 1;
        arr = new String[n];
        arr[0] = "-1";
    }

    public void push(String s) {
        arr[num++] = s;

    }

    public void pop() {
        System.out.println(arr[num - 1]);
        num--;

    }

    public void empty() {
        if (num == 0) {
            System.out.println("1");
        } else
            System.out.println("0");
    }

    public void size() {
        System.out.println(num);

    }

    public void top() {
        System.out.println(arr[num - 1]);

    }

}

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

    int n = sc.nextInt();
    sc.nextLine();
    Stack stack = new Stack(n);

    for (int i = 0; i < n; i++) {

        String line = sc.nextLine();
        String arr[] = line.split(" ");

        String check = arr[0];

        switch (check) {
            case "push":
                stack.push(arr[1]);
                break;
            case "pop":
                stack.pop();
                break;
            case "top":
                stack.top();
                break;
            case "empty":
                stack.empty();
                break;
            case "size":
                stack.size();
                break;
        }

    }
}

}