Study/Problem Solving

[BOJ_10828] 스택

monitor 2023. 10. 18. 06:48

설명


자료구조를 구현하는 것이 목적인 문제이다. 일반적인 스택으로도 풀었고 배열을 이용해서도 풀어보았다.

 

 

코드


스택 자료 구조 사용

package Data_Structure;
import java.io.*;
import java.util.*;
public class BOJ_10828 {
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk  = new StringTokenizer(in.readLine()," ");
        int N = Integer.parseInt(tk.nextToken());
        Stack<Integer> st = new Stack<>();
        for(int i = 0; i < N; i++){
            tk  = new StringTokenizer(in.readLine()," ");
            String str = tk.nextToken();
            if("push".equals(str)){
                st.push(Integer.parseInt(tk.nextToken()));
            }
            if("top".equals(str)){
                if(st.isEmpty()){
                    System.out.println(-1);
                }else{
                    System.out.println(st.peek());
                }
            }
            if("size".equals(str)){
                System.out.println(st.size());
            }
            if("pop".equals(str)){
                if(st.isEmpty()){
                    System.out.println(-1);
                }else{
                    System.out.println(st.pop());
                }
            }
            if("empty".equals(str)){
                if(st.isEmpty()){
                    System.out.println(1);
                }else{
                    System.out.println(0);
                }
            }
        }
    }
}

 

배열으로 스택 자료구조 구현

package Data_Structure;
import java.io.*;
import java.util.*;
public class BOJ_10828 {
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk  = new StringTokenizer(in.readLine()," ");
        int N = Integer.parseInt(tk.nextToken());
        int stack[] = new int[N];
        int size = 0;
        for(int i = 0; i < N; i++){
            tk  = new StringTokenizer(in.readLine()," ");
            String str = tk.nextToken();
            if("push".equals(str)){
                stack[size++] = Integer.parseInt(tk.nextToken());
            }
            if("top".equals(str)){
                if(size == 0){
                    System.out.println(-1);
                }else{
                    System.out.println(stack[size - 1]);
                }
            }
            if("size".equals(str)){
                System.out.println(size);
            }
            if("pop".equals(str)){
                if(size == 0){
                    System.out.println(-1);
                }else{
                    System.out.println(stack[size - 1]);
                    stack[size - 1] = 0;
                    size--;
                }
            }
            if("empty".equals(str)){
                if(size == 0){
                    System.out.println(1);
                }else{
                    System.out.println(0);
                }
            }
        }
    }
}
 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net