0

hey guys i have a problem when trying to print out the circular queue array heres my code:

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

}

and here's the result EMPTY: <. . . . . > ENQUEUE (0):

i am supposed to enqueue 0-4 and dequeue some element but it stops after enqueue 0.

1
  • You are using ( front == 0 && rear == -1 ) to detect BOTH full AND empty states AND whether to print a . or something else. I suggest you revisit that expression in all three places and rethink what it means, especially bearing in mind front and rear change as you add/remove entries. Commented Mar 14, 2013 at 14:06

3 Answers 3

1

A CircularQueue can be in 3 states whose invariants are given below :
Empty : front == -1 && rear == -1
Full : (rear+1)%queue.length == front
Neither empty nor full : Does not satisfy the conditions mentioned above


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

    }

}    

}

Sign up to request clarification or add additional context in comments.

Comments

1

This is my approach. But I am assuming here that the empty blocks in the array are initialized with zero, and that any valid entry would be non-zero. Also, it will print zeros in case the queue is partially filled.

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++;
                    }
                }
            }
        }

Comments

0

Problem with circular arrays and front/rear indices is that 'full' and 'empty' are indistinguishable. You will have to add a boolean 'empty', which is initially true, and is used in the tests.

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;

}

7 Comments

i know what you mean, but i am not not supposed to add other methods because its a requirement for this assignment, i can only use enqueue, dequeue and print methods.
Another alternative is to use front == rear as full and (rear + 1) % bufferSize == front as empty. This way you can fit the maximum number of elements in the buffer.
if you look at the comments in code i am not supposed to change the constructor. the problem is that the problem ends after enqueue(0) so maybe something is wrong with my print method?
There are not enough states front and rear can have. If you want them both in the range [0-5), which is the case after the first enqueue, there are 5 options for rear per value of front. However, there are 6 states: 0, 1, 2, 3, 4, and 5 elements in the queue. Conclusion: you need an extra variable.
but i can't add an extra variable, it was my mistake it should be 0-4 which is 5. but the problem is the program ends itself after enqueue(0) its not even printing the "<" in the print method. So i am thinking theres somthing wrong with the exception if in enqueu method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.