【问题标题】:Using an array like a queue使用像队列这样的数组
【发布时间】:2016-03-24 23:13:33
【问题描述】:

我的 Queue 类有问题。队列基于数组。当队列项到达数组的末尾时,它们应该被添加到数组的开头。如果队列已满,则应删除并显示最旧的队列项。插入第 8 个元素时,我无法显示已删除的最旧项目。

当用户选择显示前 3 个客户的姓名时,代码应将他们从队列中逐个移除(先进先出)并在移除时显示。

输入输出示例:

队列输入为: 一种 b C d e F G h

队列输出为: H b C d e F g

删除的项目是:h

预期的输出应该是:

H b C d e F G 删除的项目是:a


所以它将最后插入的项目移动到数组的前面但是我如何显示“删除的项目是:a” 我的代码是:

public class MyQueue {

    private final static int CAPACITY = 7;
    static String qitems[] = new String[CAPACITY];
    private static int front = 0, end = 0, count=0;

    public void addqueue(String name) {
        qitems[end] = name;
        count++;
        if(count==7) {
            takequeue();
            System.out.println("Queue is full");
        }
        end = (end + 1) % 7;
    }

    public void takequeue() {
        System.out.println("Item removed:"+qitems[front]);
        front = (front +1) % 7;
    }

    public void displayNames() {
        System.out.println(" 3 names entered are: ");
        for (int x = 0; x < 3; x++) {
            System.out.println(qitems[x]);
        }
    }
}

【问题讨论】:

  • 您的代码对我来说似乎工作正常。
  • 问题是它不会显示删除的项目是:a 它显示删除的项目是:h
  • 添加 8 个名称后会发生什么情况?它永远不会再等于 7。
  • 不是增加计数,而是尝试测试数组是否已满,即qitems.length == 7
  • 检查我的答案。另外,我认为添加第 9 个值时该方法不起作用,因此您应该考虑将条件更改为 count >= 7

标签: java arrays queue


【解决方案1】:


public class QWithArray {

    String[] qItems;
    int front;
    int end;
    int current;

    QWithArray(int CAPACITY) {
        qItems = new String[CAPACITY];
        current = 0;
        front = -1;
        end = -1;
    }

    public void addqueue(String element) {
        if (current == qItems.length) {
            System.out.println("Item removed was: " + qItems[(front + 1) % qItems.length]);
        }

        front = (front + 1) % qItems.length;
        qItems[front] = element;
        current++;

        if (end == -1) {
            end = front;
        }
    }

    public String takequeue() {
        if (current == 0) {
            System.out.println("Queue is empty; can't remove.");
            return null;
        }

        String result = qItems[end];
        qItems[end] = null;

        end = (end + 1) % qItems.length;
        current--;

        if (current == 0) {
            front = -1;
            end = -1;
        }
        return result;
    }

    public static void main(String[] args) {
        QWithArray q = new QWithArray(7);
        q.addqueue("a");
        q.addqueue("b");
        q.addqueue("c");
        q.addqueue("d");
        q.addqueue("e");
        q.addqueue("f");
        q.addqueue("g");
        System.out.println(Arrays.toString(q.qItems));

        q.addqueue("h");
        System.out.println(Arrays.toString(q.qItems));
    }
}

输出将是:


[a, b, c, d, e, f, g]

Item removed was: a

[h, b, c, d, e, f, g]


【讨论】:

    【解决方案2】:

    我通过添加以下 main 来运行您的代码。它确实打印“删除的项目:a”。粘贴到你的“main”中,这样​​我们就可以看到你在做什么。

    public static void main(String [] args) {             
             MyQueue q = new MyQueue();
             q.addqueue("a");
             q.addqueue("b");
             q.addqueue("c");
             q.addqueue("d");
             q.addqueue("e");
             q.addqueue("f");
             q.addqueue("g");
             q.addqueue("h");
         }
    

    【讨论】:

    • 添加第 7 项时,输出为:"Item removed:a" "Queue is full" 但是当第 8 项添加到队列中时,输出 "Item removed:a" 应该被删除并显示,第 8 项应该被推到顶部,在我显示 3 个名称的那一刻,输出是:“输入的 3 个名称是:”“a b c”
    【解决方案3】:

    您的 addQueue 方法中的问题是您在检查它是否等于 0 之前递增计数。因此,条件count == 7 将在添加第 7 个项目后评估为真,激活 takeQueue 方法。所以“删除的项目:a”将被打印出来。简单的解决方法是在检查 count 是否等于 7 后递增。

    public void addqueue(String name) 
    {
           if(count>=7)
           {
               takequeue();
               System.out.println("Queue is full");
           }
           qitems[end] = name;
           count++;
           end = (end + 1) % 7;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 2023-04-05
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      相关资源
      最近更新 更多