[アルゴリズム]どうやってキューを実現しますか?

4327 ワード

どうやってキューを実現しますか
package       ;

public class MyQueue<E> {

    private Node<E> head = null;
    private Node<E> tail = null;

    public boolean isEmpty(){
        return head == tail;
    }

    public void put(E data){
        Node<E> newNode = new Node<E>(data);
        if(head == null && tail == null){  //    
            head = tail = newNode;
        }else{
            tail.next = newNode;
            tail = newNode;
        }
    }

    public E pop(){
        if(isEmpty()){
            return null;
        }
        E data = head.data;
        head = head.next;
        return data;
    }

    public int size(){
        Node<E> tmp = head;
        int n = 0;
        while (tmp != null) {
            n++;
            tmp = tmp.next;
        }
        return n;
    }


    public static void main(String[] args) {
        MyQueue<Integer> q = new MyQueue<>();
        q.put(1);
        q.put(2);
        q.put(3);
        System.out.println("     :" + q.size());
        System.out.println("     :" + q.pop());
        System.out.println("     :" + q.pop());

    }
}
package       ;

public class Node<E> {

    Node<E> next = null;
    E data;
    public Node(E data){
        this.data = data;
    }
}
package       ;

import java.util.LinkedList;

public class MyQueue1<E> {

    private LinkedList<E> list = new LinkedList<>();
    private int size = 0;
    public synchronized void put(E e){
        list.addLast(e);
        size++;
    }

    public synchronized E pop(){
        size--;
        return list.removeFirst();
    }

    public synchronized boolean empty(){
        return size == 0;
    }

    public synchronized int size(){
        return size;
    }
}