ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • boj)10828 - 스택구현
    PS/boj 2020. 9. 13. 19:10
    import java.io.*;
    
    public class boj_10828 {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
            int N = Integer.parseInt(br.readLine());
            stack s = new stack(N);
    
            for (int i = 0; i < N; i++) {
                String str = br.readLine();
    
                switch (str) {
                    case "push" : s.push(Integer.parseInt(str.split(" ")[1]));
                    case "pop" : s.pop();
                    case "size" : s.size();
                    case "top" : s.top();
                    case "empty" :
                        int empty = s.empty();
                        if (empty == 1) {
                            System.out.println(1);
                        } else {
                            System.out.println(0);
                        }
                }
            }
        }
    }
    
    class stack {
        int top;
        int size;
        int[] arr;
    
        public stack(int size) {
            top = -1;
            this.size = size;
            arr = new int[this.size];
        }
    
        public int empty() {
            return top == -1 ? 1 : 0;
        }
    
        public void push(int item) {
            arr[++top] = item;
        }
    
        public void pop() {
            if (empty() == 1) {
                System.out.println(-1);
            } else {
                System.out.println(arr[top]);
                arr[top--] = 0;
            }
        }
    
        public void top() {
            if (empty() == 1) {
                System.out.println(-1);
            } else {
                System.out.println(arr[top]);
            }
        }
    
        public void size() {
            System.out.println(top+1);
        }
    }

    'PS > boj' 카테고리의 다른 글

    boj)9012 - 괄호  (0) 2020.09.13
    boj)9093 - 단어 뒤집기  (0) 2020.09.13
    boj)2644 - 촌수계산  (0) 2020.09.05
    boj)7576 - 토마토  (0) 2020.09.05
    boj)2178 - 미로 탐색  (0) 2020.09.03
킹수빈닷컴