Simple Infinite loop help
I have the following display() method for a circular queue, but I can't find the reason why it goes into an infiti loop when rear is 2 and front is 3 when the MAX_SIZE is 5.
```java
public void display() {
System.out.println("Front: " + front);
System.out.println("Rear: " + rear);
System.out.println("Array : ");
System.out.println(" : ");
int Cfront = front;
if(front==-1)
Cfront++;
int i=0;
for(i = Cfront; i != rear;i++){
if(i==MAX_SIZE){
System.out.println("true");
i=0;
}
System.out.println(queue[i]);
}
System.out.println(queue[i]);
}
3 Replies
⌛
This post has been reserved for your question.
Hey @Lana Rhodes™! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Its because you are in each interation setting i = Cfront
i = 3
3 is not equal to 2 (front)
Inc i by 1 now i is 4
4 is not equal to 2
And around we go…
If i then hits MAX_SIZE
you set i to 0
But then in the forloop you start by setting i = Cfront, writing over your zero.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.