循环队列java不能正确打印

时间:2013-03-14 14:01:09

标签: 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");
    }    

}

这是结果 EMPTY:         &LT ;. 。 。 。 。 &GT; ENQUEUE(0):

我应该将0-4排队并将某些元素出列,但在排队0后它会停止。

3 个答案:

答案 0 :(得分:1)

CircularQueue可以是3个状态,其不变量如下:
空:前面== -1&amp;&amp;后方== -1
满:(后+ 1)%queue.length ==前
既不空也不满:不满足上述条件


public class CircularQueue {

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]);   
              }
        }

    }

}    

}

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]); } } } }

答案 1 :(得分:1)

这是我的方法。但我在这里假设数组中的空块初始化为零,并且任何有效的条目都将为非零。此外,如果队列部分填充,它将打印零。

public void print() {
            if (isEmpty()) {
                System.out.println("Queue is empty");
            } else {
                System.out.println("In order from latest to oldest");
                int i = front;
                while (i < array.length) {
                    System.out.print(array[i] + " ");
                    i++;
                }
                i = i % array.length;
                if(array[i] != 0) {
                    while(i < front) {
                        System.out.print(array[i] + " ");
                        i++;
                    }
                }
            }
        }

答案 2 :(得分:0)

圆形阵列和前/后指数的问题是“完全”和“空”无法区分。你必须添加一个布尔型'empty',它最初为true,并在测试中使用。

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

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

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

// FILL IN:
// throws DSException if no element in the queue
// return the dequeued value
public int dequeue () throws DSException {
        if (empty){
                throw new DSException();
        }            

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

}