循环队列java编程

时间:2015-04-11 16:58:06

标签: java

我试图在队列中循环插入项目..我有以下代码,但它没有正确插入它们。有什么问题?

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;
    }
}

1 个答案:

答案 0 :(得分:1)

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;
    }
}

public int getCapacity(){
    return n;
}