kumar Anny
kumar Anny

Reputation: 366

Printing circular queue

How do we print circular queue in the below mentioned code. After the queue is full and we remove one item and the insert the next item which comes at index 0. How do we print it then?. How do we print circular queue in the below mentioned code.

class GQueue <T extends Object>
{
    T[] arr;
    int size;
    int front;
    int rear;
    int length;

    GQueue(int size)
    {
        this.size=size;
        arr=(T[])new Object[this.size];
        front=-1;
        rear=-1;
        length=arr.length;
    }

    void EnQueue(T data)
    { 
            if(isFull())
            {
                System.out.println("Queue full");
                return;
            }

                else if(isEmpty())
                  front=rear=0;
                else 
                 rear = (rear+1)%length;

             arr[rear]=data;
    }

    void DeQueue()
    {
        if(isEmpty())
           return;
        else if (front==rear)
        {
           front=rear=-1;
        }
        else 
           front=(front+1)%length;

    }

    T peek()
    {

        return arr[front];
    }

    boolean isEmpty()
    {
        if(front==-1 && rear==-1)
        return true;
        else 
            return false;


    }

    boolean isFull()
    {
        if( (rear+1) % length==front)
            return true;
        else 
            return false;

    }

    void print()
    {
        for(int i=front;i<=rear;i++)
        {
         System.out.print(arr[i]+" ");  
        }
         System.out.println();  
    }
}





public class GenericQueue {

public static void main(String[] args) {
    GQueue<Integer> gq = new GQueue<Integer>(10);
    gq.EnQueue(1);
    gq.EnQueue(2);
    gq.EnQueue(3);
    gq.EnQueue(4);
    gq.EnQueue(5);
    gq.EnQueue(6);
    gq.EnQueue(7);
    gq.EnQueue(8);
    gq.EnQueue(9);
    gq.EnQueue(10);


    gq.print();
    gq.DeQueue();
    gq.EnQueue(11);
    gq.print();
}

}

Upvotes: 0

Views: 5745

Answers (7)

This is my complete working solution, explained in steps along with code:

I implemented a circular queue using a String array. (I named it "words"). I seperated the conditions and wrote seperate loops for each of the following cases:

  1. When CircularQueue is Empty (front == rear && rear == -1)

You can't print anything, hence I printed "CQ empty" on the console

if (this.isEmpty())
    System.out.println("CQ Empty");
  1. When front < rear

I ran a for loop and printed the elements from front + 1 till the element rear is pointing to. (Here, words is the name of my array

for (int i = this.front + 1; i <= this.rear; i++)
    System.out.print(" " + this.words[i]);
  1. When front > rear || circularQueue.isFull() == true

My isFull() condition is: (front == rear && rear != -1) || (front == -1 && rear == size - 1). So here I implemented a do while loop to work when both front == rear and front < rear. Before I arrived at this implementation, I tried using a for loop, but it won't let me execute at all since the exit condition was met in the beginning itself, since it was an entry controlled loop.

int i = (this.front + 1) % this.size;
do {
    System.out.print(" " + this.words[i] + " " + i);
        i = (i + 1) % this.size;
} while (i != (this.rear + 1));

Note: The code implementation in the 3rd case does cover the 2nd case too, but I just wrote the second case for code readability and for easier initial understanding.

My final code

public void showAllCircularQueueElements(){
    System.out.println();
    if (this.isEmpty())
        System.out.println("CQ Empty");
    else if (this.front < this.rear) {
        for (int i = this.front + 1; i <= this.rear; i++)
            System.out.print(" " + this.words[i] + " " + i);
    } else if (this.isFull() || this.front > this.rear) {
        int i = (this.front + 1) % this.size;
        do {
            System.out.print(" " + this.words[i] + " " + i);
            i = (i + 1) % this.size;
        } while (i != (this.rear + 1));
    } else {
        System.out.println("Couldnt display");
    }
    System.out.println();
}

Upvotes: 0

Luis von Rondow
Luis von Rondow

Reputation: 1

Acctually, the correct awnser is:

void print(){
    if (!isEmpty()) {
        int i = front;
        do {
            System.out.print(" " + arr[i]);
            i = ++i % arr.length;
        } while (i != rear);
        
        if (front != rear) {
            System.out.print(" " + arr[rear]);
        }
    }
}

Upvotes: 0

Ankit Kumar
Ankit Kumar

Reputation: 117

There will be two cases:

  1. front == 0 & rear>=front

  2. rear < front

For case 1: (if)

  • Check if rear>=front
  • Iterate i from front to rear & print

For case 2: (else)

  • Iterate i from front till the end of the queue e.g., say, (Size-1)
  • Print Array!
  • Again iterate i from 0 to rear and print array

Upvotes: 0

pcdhan
pcdhan

Reputation: 87

For those looking for printing circular queue, the accepted answer fails in two scenarios:

  1. will print the complete queue when we have only one entry
  2. Does not print the last element

This is a modified version have a look:

assume we have only one entry 10 in the queue

int[] queue = new int[5]
queue = {10,0,0,0,0}

front = 0;
rear = 0;

The accepted answer output will be :

 10,0,0,0,0

We need to keep a check when to break, that is, once we have reached the rear position.

To get the correct result use following:

public void print() {
    if (!empty()) {
        int i = front;
        do {
            System.out.println(arr[i]);
            if(i==rear)
                break;
            i = (i + 1) % (arr.length);

        } while (i != front);
    }
}

Now your output will be:

 10

Also note, we are iterating till while (i != front) this allows us to print last element.

Upvotes: 0

trex
trex

Reputation: 1

You can try this code!

for(int i=front+1 ; i!=(rear+1) ; i=(i+1)%arr.length)
{
   System.out.println(" "+arr[i]);
}

Upvotes: 0

kushagra maurya
kushagra maurya

Reputation: 41

just use while(i in above code to avoid printing System.out.print(" "+arr[rear]) again.

Upvotes: 0

Kevin Anderson
Kevin Anderson

Reputation: 4592

void print(){
    if (!isEmpty()) {
        int i = front;
        do {
            System.out.print(" " + arr[i];
            i = ++i % arr.length;
        while (i != rear);
    }
}

Not tested, but I think it's right, or at least gives the general idea.

Upvotes: 2

Related Questions