java - 循环队列java打印不正确

标签 java

嘿伙计们,我在尝试打印循环队列数组时遇到问题 这是我的代码:

public class CircularQueue {

    private int [] queue;
    private int front, rear;

    // do not change the constructor
    CircularQueue() {
        queue = new int [5];
        front = 0;
        rear = -1;
    }

    // FILL IN:
    // throws DSException if out of space
    public void enqueue ( int item  ) throws DSException {
            if ( front == 0 && rear == -1 ){
                    throw new DSException();
            }
            queue[rear+1] = item;
            rear = (rear+1)%queue.length;
    }

    // FILL IN:
    // throws DSException if no element in the queue
    // return the dequeued value
    public int dequeue () throws DSException {
            if ( front == 0 && rear == -1 ){
                    throw new DSException();
            }

            int temp = queue[front];
            queue[front] = 0;
            front = (front+1)%queue.length;
            return temp;

    }


    // FILL IN:
    // return the value at beginning of the queue
    // throws DSException if no element in the queue
    public int first () throws DSException {
            return front;
    }

    // FILL IN:
    // print the circular queue in the following format
    // - print "+" before the value at the front
    // - print "-" after the value at the rear
    // - print "." for the places without valid element.

    public void print () {
        System.out.print("      <");
        for ( int i = 0; i < queue.length; i++ ){
                if ( front == 0 && rear == -1 ){
                        System.out.print("."+"\t");
                } else if ( i == front ) {
                        System.out.print("+"+ queue[i]);
                } else if ( i == rear ) {
                        System.out.print( queue[i]+ "-");
                } else if ( i == front && i == rear) {
                        System.out.print("+"+ queue[i] +"-");
                } else {
                        System.out.print( queue[i] );
                }
        }
        System.out.print(">\n");
    }    

}

这是结果 空的: <。 。 。 。 。 > 入队(0):

我应该将 0-4 入队并出队一些元素,但它在入队 0 后停止。

最佳答案

循环队列可以处于 3 种状态,其不变量如下:
空 : 前 == -1 && 后 == -1
满:(后+1)%queue.length ==前
非空非满:不满足上述条件

<br/> public class CircularQueue {

<pre><code>private int [] queue; private int front, rear; // do not change the constructor CircularQueue() { queue = new int [5]; front = -1; rear = -1; } // FILL IN: // throws DSException if out of space public void enqueue ( int item ) throws DSException,Exception { if ( front == -1 && rear == -1 ){ front = 0; rear = 0; queue[rear] = item; } else if((rear+1)%queue.length == front) { throw new Exception("Full"); } else { rear = (rear+1)%queue.length; queue[rear] = item; } } // FILL IN: // throws DSException if no element in the queue // return the dequeued value public int dequeue () throws DSException { if ( front == -1 && rear == -1 ){ throw new DSException(); } else { int ret = queue[front]; if(rear==front) { rear = -1; front = -1; } else { front = (front+1)%queue.length; } return ret; } } // FILL IN: // return the value at beginning of the queue // throws DSException if no element in the queue public int first () throws DSException { if(front==-1 && rear ==-1) { throw new DSException(); } return queue[front]; } // FILL IN: // print the circular queue in the following format // - print "+" before the value at the front // - print "-" after the value at the rear // - print "." for the places without valid element. public void print () { if(front==-1 && rear == -1) { for(int i=0;i<queue.length;i++) { System.out.print("."); } } else { if(front<=rear) { for(int i=0;i<=front-1;i++) { System.out.print("."); } System.out.print("+"); for(int i=front;i<=rear;i++) { System.out.print(queue[i]); } System.out.print("-"); for(int i=rear+1;i<=queue.length-1;i++) { System.out.print("."); } } else { for(int i=0;i<=rear;i++) { System.out.print(queue[i]); } System.out.print("-"); for(int i=rear+1;i<=front-1;i++) { System.out.print("."); } System.out.print("+"); for(int i=front;i<=queue.length-1;i++) { System.out.print(queue[i]); } } } } </code></pre> <p>}</p>

关于java - 循环队列java打印不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15411419/

相关文章:

java - android ndk递归扫描sd卡

java - Spring MVC : Adding multiple filters to list

java - 如何从 Eclipse 中的每个 View 获取选择节点以及项目名称和路径?

java - 如何在 Internet Explorer 中运行 selenium rc 测试用例

java - 向 JPA 实体添加其他方法

java - 如何在两个类之间使用 double ?

Java运行时错误

java - 如何使用java8获取上周和上个月

java - 如何使用java从智能卡读取文件

java - java 迭代匿名内部类