Untitled
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