Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
747 B
2
Indexable
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Stack<Integer> stack = new Stack<Integer>();
        while (true) {
            String line = sc.nextLine();
            if (line.equals("#")) break;
            if (line.contains("PUSH")) {
                int number = Integer.parseInt(line.split(" ")[1]);
                stack.push(number);
            } else if (line.contains("POP")) {
                if (!stack.isEmpty()) {
                    System.out.println(stack.pop());
                } else {
                    System.out.println("NULL");
                }
            }
        }
    }
}
Leave a Comment