Untitled
Anonymous
plain_text
08/21/2024 4:34 AM
996 B
23
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");
}
}
}
}
}Editor is loading...
Leave a Comment