Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环队列java未正确打印_Java - Fatal编程技术网

循环队列java未正确打印

循环队列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; }

嘿,伙计们,我在打印循环队列数组时遇到了一个问题 这是我的密码:

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

}
公共类循环队列{
私有int[]队列;
前,后;
//不要更改构造函数
循环队列(){
队列=新整数[5];
正面=0;
后部=-1;
}
//填写:
//如果空间不足,则引发DSException
公共void排队(int项)引发DSException{
如果(前==0和后==-1){
抛出新的DSException();
}
队列[后部+1]=项目;
后=(后+1)%queue.length;
}
//填写:
//如果队列中没有元素,则引发DSException
//返回退出队列的值
public int dequeue()引发DSException{
如果(前==0和后==-1){
抛出新的DSException();
}
int temp=队列[前端];
队列[前端]=0;
前端=(前端+1)%queue.length;
返回温度;
}
//填写:
//返回队列开头的值
//如果队列中没有元素,则引发DSException
public int first()抛出DSException{
返回前线;
}
//填写:
//按以下格式打印循环队列
//-在前面的值前打印“+”
//-在后面的值后面打印“-”
//-对于没有有效元素的位置,打印“.”。
公开作废印刷品(){
系统输出打印(“\n”);
}    
}
结果如下 空的: 排队(0):


我应该将0-4排成一列,并将某个元素排成一列,但在将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;

}

循环队列可以有3种状态,其不变量如下所示:
空:前==-1和后==-1
满:(后部+1)%queue.length==前部
既不空也不满:不满足上述条件


公共类循环队列{

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[]队列;
前,后;
//不要更改构造函数
循环队列(){
队列=新整数[5];
正面=-1;
后部=-1;
}
//填写:
//如果空间不足,则引发DSException
公共void排队(int项)引发DSException,Exception{
如果(前部==-1和后部==-1){
正面=0;
后部=0;
队列[后方]=项目;
}  
如果((后部+1)%queue.length==前部),则为else{
抛出新异常(“完全”);
}
否则{
后=(后+1)%queue.length;
队列[后方]=项目;
}
}
//填写:
//如果队列中没有元素,则引发DSException
//返回退出队列的值
public int dequeue()引发DSException{
如果(前部==-1和后部==-1){
抛出新的DSException();
}
否则{
int-ret=队列[前端];
如果(后==前){
后部=-1;
正面=-1;
}
否则{
前端=(前端+1)%queue.length;
}
返回ret;
}
}
//填写:
//返回队列开头的值
//如果队列中没有元素,则引发DSException
public int first()抛出DSException{
如果(前部==-1和后部==-1){
抛出新的DSException();
}
返回队列[前面];
}
//填写:
//按以下格式打印循环队列
//-在前面的值前打印“+”
//-在后面的值后面打印“-”
//-对于没有有效元素的位置,打印“.”。
公开作废印刷品(){
如果(前部==-1和后部==-1){

对于(int i=0;i这是我的方法。但是我在这里假设数组中的空块是用零初始化的,并且任何有效条目都是非零的。此外,如果队列部分被填满,它将打印零

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++;
                    }
                }
            }
        }
public void print(){
if(isEmpty()){
System.out.println(“队列为空”);
}否则{
System.out.println(“从最新到最旧的顺序”);
int i=前部;
while(i
您正在使用
(前==0和后==-1)
检测满状态和空状态,以及是否打印
或其他内容。我建议您在所有三个位置重新审视该表达式,并重新思考其含义,尤其要记住
前面
后面
在添加/删除条目时会发生变化。我知道您的意思,但我不应该添加其他内容方法因为这是此任务的一个要求,所以我只能使用排队、出列和打印方法。另一种选择是使用
front==rear
作为full和
(rear+1)%bufferSize==front
为空。通过这种方式,可以在缓冲区中容纳最大数量的元素。如果查看代码中的注释,我不应该更改构造函数。问题是,问题在排队(0)后结束所以我的打印方法可能有问题?前后两个状态都不够。如果您希望它们都在[0-5]范围内,这是第一次排队后的情况,每个
front
的值有5个
rear
选项。但是,队列中有6个状态:0、1、2、3、4和5个元素。结论:您需要一个额外的变量。但我不能添加额外的变量,这是我的问题