英文:
displaying circular queue in java
问题
以下是您提供的代码的翻译部分:
我有以下代码,其中我实现了一个循环数组。问题出在我尝试显示它时。显示方法在数组填满并且"last"回到0时出现问题。因此"last"和"first"都是0,所以for循环不执行。
public class PassengerQueue
{
private Passenger[] queueArray = new Passenger[TrainStation.WAITING_ROOM_CAPACITY];
private int first = 0;
private int last = 0;
private int maxStayInQueue = 0; // 最长等待时间(以秒为单位)
private int maxLength = 0; // 队列达到的最大长度
private int currentSize = 0;
public void add(Passenger next)
{
// 如果队列不满 - 检查循环队列
if (isFull()){
System.out.println("队列已满");
}
else
{
queueArray[last] = next;
last = (last + 1) % queueArray.length;
currentSize++;
maxLength++;
}
}
public Passenger remove()
{
Passenger removedPassenger = null;
// 如果队列不为空
// 移除乘客
if (isEmpty())
{
System.out.println("队列为空");
}
else
{
removedPassenger = queueArray[first];
queueArray[first] = null;
first = (first + 1) % queueArray.length;
currentSize--;
}
return removedPassenger;
}
public Boolean isEmpty()
{
return (currentSize == 0);
}
public Boolean isFull()
{
return (currentSize == queueArray.length);
}
public void display()
{
if (isEmpty())
{
System.out.println("队列为空");
}
else
{
for(int i = first; i < last; i++)
{
queueArray[i].display();
}
}
}
}
任何帮助都将不胜感激!谢谢
英文:
I have the following code where I have implemented a circular array. The problem comes when I try to display it. The display method works well until the array gets full and last goes back to 0. Therefore last and first are both 0 and the for loop doesn't execute.
public class PassengerQueue
{
private Passenger[] queueArray = new Passenger[TrainStation.WAITING_ROOM_CAPACITY];
private int first = 0;
private int last = 0;
private int maxStayInQueue = 0; //number of seconds that the passenger who stayed longest in the queue
private int maxLength = 0; //the maximum legth that was reached by the queue
private int currentSize = 0;
public void add(Passenger next)
{
//if the queue is not full - check for the circular queue
if (isFull()){
System.out.println("The queue is full");
}
else
{
queueArray[last] = next;
last = (last + 1) % queueArray.length;
currentSize++;
maxLength++;
}
}
public Passenger remove()
{
Passenger removedPassenger = null;
//if the queue array is not empty
//remove passenger
if (isEmpty())
{
System.out.println("The queue is empty");
}
else
{
removedPassenger = queueArray[first];
queueArray[first] = null;
first = (first + 1) % queueArray.length;
currentSize--;
}
return removedPassenger;
}
public Boolean isEmpty()
{
return (currentSize == 0);
}
public Boolean isFull()
{
return (currentSize == queueArray.length);
}
public void display()
{
if (isEmpty())
{
System.out.println("The queue is empty");
}
else
{
for(int i = first; i < last; i++)
{
queueArray[i].display();
}
}
}
Any help would be appreciated! Thank You
答案1
得分: 0
只需使用数组本身的属性来显示:
public void display()
{
if (isEmpty())
{
System.out.println("队列为空");
}
else
{
for(int i = 0; i < queueArray.length; i++)
{
queueArray[i].display();
}
}
}
英文:
Just use the properties on the array itself to display:
public void display()
{
if (isEmpty())
{
System.out.println("The queue is empty");
}
else
{
for(int i = 0; i < queueArray.length; i++)
{
queueArray[i].display();
}
}
}
答案2
得分: 0
你可以将循环修改为从 0 迭代到 size。这也可以修复当 last
小于 first
的问题,因为已删除了项目。
for (int i = 0; i < currentSize; i++) {
queueArray[(first + i) % queueArray.length].display();
}
英文:
You can change the loop so it iterates from 0 to size. This also fixes the problem where last
is less than first
because items have been removed.
for(int i = 0; i < currentSize; i++)
{
queueArray[(first + i) % queueArray.length].display();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论