Linkedlist练习
Linkedlist模拟一个队列或者堆栈数据结构
堆栈:先进后出
队列:先进先出(FIFO)
队列
package songyan; import java.util.LinkedList; public class Queue{ //队列:先进后出 private LinkedList link; public Object get() { return link.pollFirst(); } public void add(Object obj) { link.offerFirst(obj); } public Queue() { link=new LinkedList(); } public boolean isEmpty() { return link.isEmpty(); } public static void main(String[] args) { Queue qu= new Queue(); qu.add("name1"); qu.add("name2"); qu.add("name3"); System.out.println(qu.get()); System.out.println(qu.get()); System.out.println(qu.get()); while(!qu.isEmpty()) { System.out.println(qu.get()); } } }
堆栈
package songyan; import java.util.LinkedList; public class List{ //队列:先进后出 private LinkedList link; public Object get() { return link.pollFirst(); } public void add(Object obj) { link.offer(obj); } public List() { link=new LinkedList(); } public boolean isEmpty() { return link.isEmpty(); }