Question

I have the code for the implementation of this pretty close to being done, what is causing my IndexOutofBounds error seems to be happening in one case of inserting into the queue. Any ideas? At the start of my class i set the rear and front to be -1, and the count to be 0. The array has a max size of 100. There is a isfull class that tests to see if the count is the max size.

    public boolean insert(int n){

    if (isFull()){
        //not inserted
        return false;
    }
    else{

       //make it the first in queue if queue is empty
       if ( front == -1 && rear == -1){
           front++;
           rear++;
           list[front] = n;
           list[rear] = n;
           count++;
           return true;

       }
       //not at end of queue, add to end
        else if ( (count+1) <= 100 ){
            rear++;
            list[rear] = n;
            count++;
            return true;
        }
        //else add to front
        else{
            //update rear
            rear = ((count+1)%100)-1;
            list[rear] = n;
            return true;
        }
    }
}   

This code so far inserts a number into the array in this order: 0. Check to see if its full. If it is quit. 1. If the queue is empty, make it the first item in there. 2. If the queue is not empty or full, check to see if the back of the array doesnt exceed the max spots. If it doesnt, add it to the end. 3. If the queue is not empty or full, but the back of the queue is full. Loop around and insert it into the beginning of the array.

The problem is in the case where for example: - The array is filled with numbers 1-100. The array is full at this point. - Remove the front, so then the array goes from 2-100 with the first slot being empty. - Insert the number you just removed, this causes the error. At this point the count+1 doesn't exceed the max spots, so it tries to add it to the rear. But since the last spot is full, it doesn't loop around, throwing a array out of bounds exception. What can i add for it to check if the last spot is filled and to add to the beginning of the array in that case?

My remove method:

    public int remove(){
    //if empty return -1
    if (isEmpty()){
        return -1;
    }
    else{//else remove
        front++;
        int x = list[front-1];
        count--;
        return x;
    }
   }
Was it helpful?

Solution

public boolean insert(int n){

    if (isFull()){
        //not inserted
        return false;
    }
    else{

       //make it the first in queue if queue is empty
       if (isEmpty()){ //use empty
           front=0;//just set it 
           rear=0;//just set it 
       }
       list[rear] = n;
       rear = (rear+1)%100; //just rewind it when it reaches 100 index 0 must be free at this point

       count++;
       return true;
    }

}

I suppose count is the number of elements, so remove should do count--. in this case count is always <100 because the array is not full after u checked for that... so the only thing you must do is to rewind the rear counter;

additionally remove should do front = (front+1)%100;

public int remove(){
    if (isEmpty()){
        return -1;
    }
    else{//else remove

        int x = list[front];//reorder
        front = (front+1)%100;//rewind
        count--;
        return x;
    }
}

empty() and full() should use count

front points on the the element to remove() next

last always points on the next free spot (or on front which is the next free spot too)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top