LinkedList 实现 Queue
package cn.com.example; import java.util.LinkedList; /** * Created by Jack on 2017/3/8. */ public class MyQueue<E> { private LinkedList<E> list; public MyQueue() { list = new LinkedList<E>(); } // 入队 public void put(E e) { list.addLast(e); } // 出队 public E pop() { return list.removeFirst(); } public int size() { return list.size(); } // 获取第一个元素 public E getFirst() { return list.getFirst(); } // 获取最后一个元素 public E getLast() { return list.getLast(); } } class MyQueueTest { public static void main(String[] args) { MyQueue<String> queue = new MyQueue<String>(); queue.put("1"); System.out.println(queue.size()); System.out.println(queue.getFirst()); for (int i = 2; i <= 10; i++) { queue.put("" + i); } System.out.println(queue.size()); System.out.println(queue.pop()); System.out.println(queue.size()); System.out.println(queue.getLast()); } }
输出
1 1 10 1 9 10