码迷,mamicode.com
首页 > 其他好文 > 详细

1.3.35

时间:2018-05-22 20:43:58      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:rabl   The   each   temp   nta   repr   hint   add   str   

question:

Random queue. A random queue stores a collection of items and supports the following API:

public class Randomqueue<Item>
            RandomQueue()      //create an empty random queue
    boolean isEmpty()          //is the queue empty?
       void enqueue(Item item) //add an item
       Item dequeue()          //remove and return a random item
                               //(sample without replacement)
       Item sample()           //return a random item, but do not remove
                               //(sample with replacement)

Write a class RandomQueue that implements this API. Hint: Use an array representation(with resizing). To remove an item, swap one at a random position(indexd 0 through N-1) with the one at the last position(index N-1). Then delete and return the last object, as in ResizingArrayStack. Write a client that deals bridge hands(13 cards each) using RandomQueue<Card>.

answer:

import edu.princeton.cs.algs4.*;
import java.util.Iterator;//注意别忘记

public class RandomQueue<Item> implements Iterable<Item>
{
    private Item [] a;
    int N;
    
    public RandomQueue()
    {
        a = (Item[]) new Object[1];
    }
    
    public boolean isEmpty()
    {
        return N == 0;
    }
    
    public void enqueue(Item item)
    {
        if(N == a.length)
        {
            resizing(a.length * 2);
        }
        a[N++] = item;
    }
    
    public void resizing(int size)
    {
        Item [] p = (Item[]) new Object[size];
        for(int i = 0; i < N; i++)
        {
            p[i] = a[i];
        }
        a = p;
    }
    
    public Item dequeue()//随机删除一项并拿最后一项来补删除的那项
    {
        if(this.isEmpty())
        {
            return null;
        }
        if(N == a.length / 4)
        {
            resizing(a.length / 2);
        }
        int index = StdRandom.uniform(N);//p30页,integer between 0 and N-1
        Item x = a[index];
        a[index] = a[--N];
        a[N] = null;
        return x;
    }
    
    public Item sample()
    {
        int index = StdRandom.uniform(N);
        return a[index];
    }
    
    public Iterator<Item> iterator()
    {
        return new RandomQueueIterator();
    }
    
    public class RandomQueueIterator implements Iterator<Item>
    {
        private Item[] temp;
        private int index;
        
        public RandomQueueIterator()
        {
            temp = (Item[]) new Object[N];
            for(int i = 0; i < N; i++)
            {
                temp[i] = a[i];
            }
            StdRandom.shuffle(temp);//p30,randomly shuffle the array a[]
            index = 0;
        }
        
        public boolean hasNext()
        {
            return index < N;
        }
        
        public Item next()
        {
            return temp[index++];
        }
        
        public void remove()
        {
        }
    }
    
    public static void main(String[] args)
    {
        RandomQueue<Integer> queue = new RandomQueue<Integer>();
        for(int i = 1; i <= 52; i++)
        {
            queue.enqueue(i);
        }
        
        for(Integer integer : queue)
        {
            StdOut.print(integer + "\t");
        }
        StdOut.print("\n");
    }
}

1.3.35

标签:rabl   The   each   temp   nta   repr   hint   add   str   

原文地址:https://www.cnblogs.com/w-j-c/p/9073719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!