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)