显示 Java 中的循环队列

huangapple go评论86阅读模式
英文:

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(&quot;The queue is full&quot;);
}
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(&quot;The queue is empty&quot;);
}
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(&quot;The queue is empty&quot;);
}
else
{
for(int i = first; i &lt; 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(&quot;The queue is empty&quot;);
}
else
{
for(int i = 0; i &lt; 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 &lt; currentSize; i++)
{
queueArray[(first + i) % queueArray.length].display();
}

huangapple
  • 本文由 发表于 2020年4月10日 00:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61125929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定