java - 循环队列toString方法忽略一些条件

标签 java queue tostring modular-arithmetic circular-queue

当我调用 toString() 方法时,如果索引环绕(前>后),它就不起作用。我已经包含了下面的代码。之后,我入队(5)和入队(6),toString似乎被完全忽略。起初我以为我没有覆盖 Java 中默认的 toString,但第一个 print 语句清楚地告诉我我是。不管怎样,检查一下代码:

public class driver {
public static void main(String[] args) {

    Queue queue = new Queue(4);
    System.out.println(queue);
    queue.enqueue(1);
    queue.enqueue(2);
    queue.enqueue(3);
    queue.enqueue(4);
    System.out.println(queue);
    queue.dequeue();
    System.out.println(queue);

    queue.dequeue();
    System.out.println(queue);

    queue.enqueue(5);
    queue.enqueue(6);

    System.out.println(queue);


}

public static class Queue {

    int front;
    int rear;
    int capacity;
    int[] queue;

    public Queue(int size) {
        this.capacity = size;
        this.front = this.rear = -1;
        this.queue = new int[capacity];
    }

    @Override
    public String toString() {
        String str = "";
        if (front > rear) {
            int i = front;
            while (i != rear) {
                str = str + queue[i % queue.length] + " ";
                i++;
            }
            //str= str+queue[rear];
        }
        if (front < rear) {
            for (int i = front; i <= rear; i++) {
                str = str + queue[i];
            }
        }
        if (front == rear) {
            str = "This Queue is Empty. Please Enqueue";
        }

        return str;
    }


    public boolean isFull() {

        return ((rear == this.queue.length - 1 && front == 0) || rear == front - 1);

    }

    public boolean isEmpty() {
        return (front == -1);
    }

    public void enqueue(int elem) {
        if (isFull()) {
            System.out.println("Full Queue - dequeue an element if you need to add an element in the queue");
        } else {
            if (isEmpty()) {
                this.queue[++rear] = elem;
                front = 0;
            } else {
                rear = (rear + 1) % this.queue.length;
                this.queue[rear] = elem;
            }
        }
    }

    public int dequeue() {
        if (isEmpty()) {
            System.out.println("empty queue. Enqueue some elments. ");
            return -1;
        } else {
            int store = this.queue[front];

            if (rear == front) {
                front = rear = -1;
            } else {

                front = front + 1 % this.queue.length;

            }
            return store;

        }

    }

}

*这里也有一个返回花括号,哈哈,对于发布问题还是个新手。 P.S 有人可以帮助我,因为显然我在问题中发布了太多代码。有什么解决方法吗?

最佳答案

问题出在 i++在 toString 的 while 循环中。

考虑 front = 3 的情况和rear = 1然后你用 i = front = 3 开始循环。但是,您会继续递增,直到达到i == rear。自从 rear < front 以来,这永远不会发生

你想要的是i循环回到0一旦达到capacity 。 您可以通过删除 i++ 来做到这一点并将其替换为 i = (i + 1) % capacity;

你的代码中似乎有一个不同的错误,因为当我运行它时我看到

This Queue is Empty. Please Enqueue
1234
234
34
3 4 5

您需要自己解决这个问题。

您甚至可以将其压缩为单个 for 语句

for(int i = front; i != rear; i = (++i) % capacity)

关于java - 循环队列toString方法忽略一些条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57045527/

相关文章:

java - 为什么 Truffle DSL 添加 @Specialization 注解时会出错?

java - OpenGL 纹理删除时出错

algorithm - 我可以在不使用堆栈的情况下反转队列吗?

Ruby object to_s object id 的编码是什么?

f# - 在 F# 中覆盖 ToString 时避免堆栈溢出

Java 使用重复代码的简写

ios - 异步执行但同步写入

java - JMS 多线程概念与 MQ

c# - 如何更改 double.ToString() 中的小数点符号?

Java无法获取当前目录中存在的文件的路径