创建一个类Stack,代表堆栈(其特点为:后进先出),添加方法add(Object obj)、方法get()和delete( ),并编写main方法进行验证。
import java.util.LinkedList;
import java.util.List;
class Stack{
LinkedList list;
public Stack() {
list = new LinkedList();
}
public void add(Object obj){
list.addFirst(obj);
}
public Object get(){
return list.getFirst();
}
public void delete(){
list.removeFirst();
}
}
public class Ex13 {
public static void main(String[] args) {
Stack stack=new Stack();
stack.add(“1″);
stack.add(“2″);
stack.add(“3″);
System.out.println(stack.get());
stack.delete();
System.out.println(stack.get());
}
}