LinkedList实现队列简易版

来源:互联网 发布:mac os x10.12升级卡死 编辑:程序博客网 时间:2024/05/07 23:41
public class MyQueue<T> {
    LinkedList<T> list=new LinkedList<>();
    public void offer(T a){
        list.add(a);
    }
    public T poll(){
        T b=null;
        b=list.removeFirst();
        return b;
    }
    public T peek(){
        T b=null;
        b=list.getFirst();
        return b;
    }
    public boolean isEmpty(){
        return list.isEmpty();
    }

}

//测试

        MyQueue<Integer> queue=new MyQueue<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        System.out.println(queue.peek());
        while(!queue.isEmpty()){
            System.out.println(queue.poll());
        }

0 0
原创粉丝点击