题解 | #【模板】栈#
【模板】栈
http://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main{
public static void main(String [] arg){
Scanner sc = new Scanner(System.in);
MyStatck statck = new MyStatck();
while(sc.hasNextLine()){
String tmpStr= sc.nextLine();
String [] tempArr= tmpStr.split(" ");
if("push".equals(tempArr[0])){
statck.push(Integer.valueOf(tempArr[1]));
continue;
}
if("pop".equals(tempArr[0])){
statck.pop();
continue;
}
if("top".equals(tempArr[0])){
statck.top();
}
}
}
}
class MyStatck{
List<Integer> data;
public MyStatck(){
data =new ArrayList();
}
public void push(int value){
data.add(value);
}
public void pop(){
if(data.isEmpty()){
System.out.println("error");
return;
}
int index= data.size()-1;
System.out.println(data.get(index));
data.remove(index);
}
public void top(){
if(data.isEmpty()){
System.out.println("error");
return;
}
System.out.println(data.get(data.size()-1));
}
}