Last active
August 29, 2015 13:57
-
-
Save jingz8804/9468319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class ResizingArrayQueue<Item> implements Iterable<Item>{ | |
private Item[] items; // the actual array holding the items | |
private int head; // the index of the head of the queue | |
private int tail; // the index of the tail of the queue | |
private int count; // the number of items in the queue | |
public ResizingArrayQueue(){ | |
items = (Item[]) new Object[2]; // have to use a cast since java does not allow generic array | |
head = tail = 0; | |
count = 0; | |
} | |
public int size(){ | |
return count; | |
} | |
public void enqueue(Item item){ | |
// first check if the queue is full | |
int len = items.length; | |
if (count == len) resize(2 * len); | |
items[tail++] = item; | |
if (tail == len) tail = 0; // wrap around; this happens when the queue is not full (empty slot at the beginning) | |
// otherwise, if there is no space at the beginning of the array, it should be resized | |
count++; // do not forget to increase the count | |
} | |
public Item dequeue(){ | |
// always check if the queue is empty first before dequeue operation | |
if(count == 0) throw new NoSuchElementException("Queue underflow!"); | |
int len = items.length; | |
Item item = items[head]; | |
items[head] = null; // avoid loiterting | |
head++; | |
if (head == len) head = 0; // wrap around; same reason as above | |
count--; | |
if (count > 0 && count == len / 4) resize(len / 2); // halving the queue but be careful, we don't want to do this | |
// when there is nothing left in the queue | |
return item; | |
} | |
private void resize(int size){ | |
Item[] newItems = (Item[]) new Object[size]; | |
int len = items.length; | |
for (int i = 0; i < len; i++){ | |
newItems[i] = items[(head + i) % len]; | |
} | |
items = newItems; | |
// now we have moved everything to the new array, let's reset the head and tail | |
head = 0; | |
tail = count; | |
} | |
public Iterator<Item> iterator(){ | |
return new ArrayQueueIterator(); | |
} | |
private class ArrayQueueIterator implements Iterator<Item>{ | |
int current = 0; | |
int len = items.length; | |
public boolean hasNext(){return current < len;} | |
public void remove() {throw new UnsupportedOperationException();} | |
public Item next() { | |
if (!hasNext()) throw new NoSuchElementException(); | |
Item item = items[(current + head) % len]; | |
current++; | |
return item; | |
} | |
} | |
} |
Author
jingz8804
commented
Mar 10, 2014
- Before dequeue operation, be sure to check if the queue is empty first.
- When halving the queue, be sure to check that this is not an empty queue.
- When deleting an item, make sure there is no loitering
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment