1 /* 2 *需求:请用LinkedList模拟栈数据结构的集合,并测试 3 *创建一个类将Linked中的方法封装 4 */ 5 public class Demo4_LinkedList { 6 public static void main(String[] args) { 7 Stack stack = new Stack(); 8 stack.in("a"); 9 stack.in("b"); 10 stack.in("c"); 11 stack.in("d"); 12 while (!stack.isEmpty()) { 13 System.out.println(stack.out()); 14 } 15 } 16 }
1 import java.util.LinkedList; 2 3 public class Stack { 4 @SuppressWarnings("rawtypes") 5 private LinkedList list = new LinkedList(); 6 7 @SuppressWarnings("unchecked") 8 public void in(Object obj) {// 模拟进栈 9 list.addLast(obj); 10 } 11 12 public Object out() {// 模拟出栈 13 return list.removeLast(); 14 } 15 16 public boolean isEmpty() {// 判断是否为空 17 return list.isEmpty(); 18 }