算法第四版1.3背包、队列和栈:习题1.3.36

import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;

public class RandomQueueimplements Iterable {
    public static void main(String[]args){
        RandomQueue queue=new RandomQueue<>(10);
        for (int i=1;i<14;i++)
            queue.enqueue(i);
        for (int i:queue)
            StdOut.print(i+" ");
        StdOut.println();
    }

    private Item[] arr;
    private int N;
    public RandomQueue(int caps){
        arr=(Item[]) new Object[caps];
        N=0;
    }
    public boolean isEmpty(){return N==0;}
    public int size(){return N;}
    public void enqueue(Item item){
        if (arr.length==N)
            resizeArray(arr.length*2);
        arr[N++]=item;
    }
    public Item dequeue(){
        if (N iterator(){
        return new RandomQueueIterator();
    }
    private  class RandomQueueIterator implements Iterator{
        private Item[] newarr=(Item[])new Object[N];
        private int index;
        public RandomQueueIterator(){
            for (int i=0;i

 

你可能感兴趣的:(算法第四版1.3背包,队列和栈课后习题)