스택

백준 10828 문제는 Stack를 직접 구현하고 입력으로 주어지는 명령을 처리하는 코드를 작성하면 된다. 다만 입력 값을 받기 위해서 BufferedReader를 사용하여 입력을 효율적으로 처리하고 Stack, Note 클래스를 사용하여 Stack를 구현했다.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        Stack stack = new Stack();
        for (int i = 0; i < n; i++) {
            String[] command = br.readLine().split(" ");
            if (command[0].equals("push")) {
                stack.push(Integer.parseInt(command[1]));
            }
            if (command[0].equals("pop")) {
                System.out.println(stack.pop());
            }
            if (command[0].equals("size")) {
                System.out.println(stack.size());
            }
            if (command[0].equals("empty")) {
                System.out.println(stack.isEmpty());
            }
            if (command[0].equals("top")) {
                System.out.println(stack.top());
            }
        }
    }
}

class Stack {

    private int size;
    private Node current;

    public void push(int n) {
        if (isEmpty() == 1) {
            current = new Node(n);
        } else {
            Node node = new Node(n);
            node.setPrevNode(current);
            current = node;
        }
        size++;
    }

    public int pop() {
        if (current == null) {
            return -1;
        } else {
            int value = current.getValue();
            current = current.getPrevNode();
            size--;
            return value;
        }
    }

    public int size() {
        return size;
    }

    public int isEmpty() {
        return size == 0 ? 1 : 0;
    }

    public int top() {
        if (current == null) {
            return -1;
        } else {
            return current.getValue();
        }
    }
}

class Node {
    private Node prevNode;
    private final int value;

    public Node(int value) {
        this.value = value;
    }

    public Node getPrevNode() {
        return prevNode;
    }

    public void setPrevNode(Node node) {
        prevNode = node;
    }

    public int getValue() {
        return value;
    }
}