我试图在队列中循环插入项目..我有以下代码,但它没有正确插入它们。有什么问题?
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;
}
}
答案 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;
}