Hi @gto11520,
Let image that you have a fixed array for 5 items. [][][][][][]
You put 1 front(value 1) & 1 rear (value2) with your code then you will have:
public void insertFront(Item item) // put item at rear of queue
{
if(front == 0)
front = maxSize;
queArray[--front] = item;
nItems++;
}
[][][][][1]
it's fine.
Then now I put 1 rear:
public void insertRear(Item item) // put item at rear of queue
{
if(rear==maxSize-1)
rear = -1;
queArray[++rear] = item;
nItems++;
}
So you will have:
[2][][][][1]
So how could this bellow code work ?
public String toString(){
String msg = "";
if (front <= rear){
for (int i = front; i <= rear; i++){
System.out.print("testing inner loop");
msg += queArray[i].toString() + "\n";
}
Let I talk you again, think about it more carefully to understand it & then implement later.
Let Imagine that you have an unlimited fixed array.
front=-1, rear = -1;
At first time, you put 1 item, then:
...[][front,rear][]...
front = 100 (for example)
rear = 100
Now you insert front:
...[front][rear][]....
front = 99, rear = 100
Now you still insert front:
...[front][item][rear][].....
front = 98
rear = 100
Now you insert rear:
...[front][item][item][rear]...
front = 98
rear = 101
got it ?
In case you just have a fixed array, if you couldn't put front -> return No Space Error, if you couldn't put rear -> return No Space Error.
After successful implement this above, you could think more to re-allocate your array or etc ...