//top为即将入栈的元素的索引,当top为0的时候栈为空。这样做的目的是方便计算栈的size,直接用top表示 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); STACK stack=new STACK(); int N=in.nextInt(); for(int i=0;i<N;i++){ String str=in.next(); switch(str){ case "push": int temp=in.nextInt(); stack.push(temp); break; case "pop": stack.pop(); break; case "query": stack.query(); break; case "size": stack.size(); break; } } in.close(); } } class STACK{ public static final int MAX_L=100000; int[] arr=new int[MAX_L]; int top=0; public void push(int x){ arr[top]=x; top++; } public void pop(){ if(top!=0){ top--; }else{ System.out.println("Empty"); } } public void query(){ if(top!=0){ System.out.println(arr[top-1]); }else{ System.out.println("Empty"); } } public void size(){ System.out.println(top); } }