@@ -0,0 +1,2508 @@
+package week2;%0A%0Aimport java.util.Iterator;%0Aimport java.util.NoSuchElementException;%0A%0A/**%0A * Author: Mark%0A * Date  : 2015/2/27%0A * Time  : 22:25%0A */%0Apublic class QueueWithArray%3CItem%3E implements Iterable%3CItem%3E %7B%0A%0A%0A    public static void main(String%5B%5D args) %7B%0A        QueueWithArray%3CInteger%3E queue = new QueueWithArray%3C%3E();%0A        for (int i = 0; i %3C 10; i++) %7B%0A            queue.enqueue(i);%0A        %7D%0A        System.out.println(queue.size());%0A        System.out.println(queue.isEmpty());%0A        System.out.println(queue.dequeue());%0A        for (int i : queue) %7B%0A            System.out.println(i);%0A        %7D%0A        System.out.println(queue.isEmpty());%0A        System.out.println(queue.size());%0A        while (!queue.isEmpty()) %7B%0A            System.out.println(queue.dequeue());%0A        %7D%0A    %7D%0A%0A%0A    private Item%5B%5D items;%0A    private int head;%0A    private int tail;%0A    private int size;%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public QueueWithArray() %7B%0A        items = (Item%5B%5D) new Object%5B256%5D;%0A        head = -1;%0A        tail = -1;%0A        size = 0;%0A    %7D%0A%0A    public void enqueue(Item item) %7B%0A        if (isEmpty()) %7B%0A            items%5B++head%5D = item;%0A            tail = head;%0A        %7D else %7B%0A            if (tail + 1 %3E= items.length) %7B%0A                tail = -1;%0A            %7D%0A            items%5B++tail%5D = item;%0A        %7D%0A        size++;%0A    %7D%0A%0A    public Item dequeue() %7B%0A        if (isEmpty()) %7B%0A            throw new NoSuchElementException();%0A        %7D%0A        Item res = items%5Bhead++%5D;%0A        items%5Bhead - 1%5D = null;%0A        size--;%0A        if (head %3E= items.length) %7B%0A            head = 0;%0A        %7D%0A        return res;%0A    %7D%0A%0A    public boolean isEmpty() %7B%0A        return size == 0;%0A    %7D%0A%0A    private boolean isFull() %7B%0A        return size == items.length;%0A    %7D%0A%0A    public int size() %7B%0A        return size;%0A    %7D%0A%0A    @Override%0A    public Iterator%3CItem%3E iterator() %7B%0A        return new QueueIterator(head);%0A    %7D%0A%0A    private class QueueIterator implements Iterator%3CItem%3E %7B%0A%0A        private int cur;%0A%0A        public QueueIterator(int cur) %7B%0A            this.cur = cur;%0A        %7D%0A%0A        @Override%0A        public boolean hasNext() %7B%0A            return cur != tail;%0A        %7D%0A%0A        @Override%0A        public Item next() %7B%0A            Item res = items%5Bcur++%5D;%0A            if (cur == items.length) %7B%0A                cur = 0;%0A            %7D%0A            return res;%0A        %7D%0A%0A        @Override%0A        public void remove() %7B%0A            throw new UnsupportedOperationException();%0A        %7D%0A    %7D%0A%7D%0A

add queue