Laura A. Agalioti
Laura A. Agalioti

Reputation: 111

Circular queue java programming

I am trying to insert items circularly in a queue.. i have the following code but it doesn'n insert them right. what's the problem?

public boolean insert(int element){
    if ( head == -1 && tail == -1 ){
        head = 0;
        tail = 0;
        elements[tail] = element;
        return true;
    }  
    else if((tail+1)%capacity == head) {
        System.out.println("Full");
        return false;
    }
    else {
        tail = (tail+1)%capacity;
        elements[tail] = element;
        return true;
    }
}

Upvotes: 1

Views: 648

Answers (1)

Laura A. Agalioti
Laura A. Agalioti

Reputation: 111

public boolean insert(int element){
    if (getCapacity() == capacity) {
        System.out.println("Queue is full.");
        return false;
    }else {
        elements[tail] = element;
        tail = (tail+1)%capacity;
        n++;
       return true;
    }
}

and

public int getCapacity(){
    return n;
}

Upvotes: 1

Related Questions