I am trying to implement a Deque using circular array here is the code I have (sorry to post entire class)
public class Deque<Item> implements Iterable<Item> {
private int frontIndex;
private int backIndex;
private static final int defaulSize = 8;
private int size;
private Item holder[];
private int capacity;
public Deque() {
holder = (Item[]) new Object[defaulSize];
this.size = 0;
this.frontIndex = 0;
this.backIndex = 0;
this.capacity = defaulSize;
}
public boolean isEmpty() {
return this.size == 0;
}
public int size() {
return this.size;
}
public void addFirst(Item item) throws Exception {
if (item == null) {
throw new NoSuchElementException();
}
if (size == capacity) {
doubleCapacity();
}
holder[this.frontIndex] = item;
this.frontIndex = Math.floorMod((this.frontIndex + 1), capacity);
size++;
}
public void addLast(Item item) throws Exception {
if (item == null) {
throw new NoSuchElementException();
}
if (size == capacity) {
doubleCapacity();
}
holder[this.backIndex] = item;
this.backIndex = Math.floorMod((this.backIndex - 1), capacity);
size++;
}
public Item removeFirst() throws Exception {
if (isEmpty()) {
throw new Exception("Deque is empty.");
}
this.frontIndex = Math.floorMod((this.frontIndex - 1), capacity);
this.size--;
Item e = holder[this.frontIndex];
holder[this.frontIndex] = null;
return e;
}
public Item removeLast() throws Exception {
if (isEmpty()) {
throw new Exception("Deque is empty.");
}
this.backIndex = Math.floorMod((this.backIndex + 1), capacity);
this.size--;
Item e = holder[this.backIndex];
holder[this.backIndex] = null;
return e;
}
private void doubleCapacity() {
int p = this.backIndex;
int n = holder.length;
int r = n - p; // number of elements to the right of p
capacity = (n) * 2;
Object[] a = new Object[capacity];
System.arraycopy(holder, p, a, 0, r);
System.arraycopy(holder, 0, a, r, p);
holder = (Item[]) a;
this.backIndex = n;
this.frontIndex = 0;
}
private class DequeIterator<E> implements Iterator<E> {
private int pos = 0;
public boolean hasNext() {
return holder.length > pos;
}
public E next() {
return (E) holder[pos++];
}
public void remove() {
throw new UnsupportedOperationException("Cannot remove an element of an array.");
}
}
@Override
public Iterator<Item> iterator() {
return this.new DequeIterator<Item>();
}
}
The problem come when I am trying to resize the circular array with deque it seems like all the indexes are getting messed up
I used this method similar to how it done in java implementation to experiment
private void doubleCapacity() {
int p = this.backIndex;
int n = holder.length;
int r = n - p; // number of elements to the right of p
capacity = (n) * 2;
Object[] a = new Object[capacity];
System.arraycopy(holder, p, a, 0, r);
System.arraycopy(holder, 0, a, r, p);
holder = (Item[]) a;
this.backIndex = n;
this.frontIndex = 0;
}
Is there a specific way on how to resize circular array if it used as deque, or how can I resize this deque
(IT NOT A HOMEWORK JUST PERSONAL STUDY)
addFirstand thenaddLastthe first element will be overwritten.