集册 Java实例教程 一般队列

一般队列

欢马劈雪     最近更新时间:2020-01-02 10:19:05

432
一般队列

public class GenericQueue<Item> {

    private Node<Item> first;

    private Node<Item> last;
    /*
    nowjava.com - 时代Java 提 供
    */


    public Item dequeue() {

        if (first == null) {

            throw new java.lang.IndexOutOfBoundsException();

        }

        Item firstval = first.item;

        // check if this is the last item

        if (first.next == null) {

            first = null;

            last = null;

        } else

            first = first.next;

        return firstval;
        /**
        n o w    j a v a  . c o m 提供 
        **/

    }


    public void enqueue(Item s) {

        Node<Item> newlast = new Node<Item>();

        newlast.item = s;

        newlast.next = null;

        if (first == null)

            first = newlast;

        else

            last.next = newlast;

        last = newlast;

    }


    public static void main(String[] args) {

        GenericQueue<Integer> queue = new GenericQueue<Integer>();

        while (!StdIn.isEmpty()) {

            String s = StdIn.readString();

            //StdOut.print(s);

            if (s.equals("-"))

           
展开阅读全文