Learning Data Structures in Java: Circular Queue

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
ishika17
Messages
10
Reaction score
3
New poster has been reminded to post schoolwork problems in the Homework Help forums
Summary:: I have recently started with data structures in java and I tried doing this Program .But I have few confusions.
1.How do I write the main() of the program?
2.What are we supposed to do with the value returned by function pop()?
If anyone could point out if there are any errors and please if you could help me with the above questions.
Thank You.

Java:
import java.io.*;
class CirQueue {
    int cirque[]; //to store the elements of array in circular queue
    int cap; //to store maximum capacity of array
    int front, rear; //to store front and rear indices
    CirQueue(int max) //constructor to initailize cap with the mximum value entered by the console
    {
        cap = max;
        front = 0;
        rear = 0;
    }
    void push(int n) //to add integer in the queue from rear end
    {
        if (front == 0 && rear == cap - 1 || front == rear + 1)
            System.out.println("QUEUE OVERFLOWS");
        else {
            if (front == -1 && rear == -1) {
                front = 0;
                rear = 0;
            } else
            if (rear == cap - 1)
                rear = 0;
            else
                rear = rear + 1;
            cirque[rear] = n;
        }
    }
    int pop() {
        if (front != rear) {
            int n = cirque[front];
            front = (front + 1) % cap;
            return (n);
        } else
            return (-9999);
    }
    void show() {
        if (front != rear) {
            for (int i = front; i <= rear; i = (i + 1) % cap) {
                System.out.print(cirque[i] + "\t");
                System.out.println();
            } else
                System.out.println("QUEUE IS EMPTY");
        }
    }
 
Physics news on Phys.org
Firstly I think this should be in a homework forum - I'll get it moved.

ishika17 said:
1.How do I write the main() of the program?
You add a method called 'main' that looks something like this:
Code:
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }

ishika17 said:
2.What are we supposed to do with the value returned by function pop()?
Whatever you want. I'd suggest that you write a main() method to create a small buffer and push and pop some items to see if it works properly.

ishika17 said:
If anyone could point out if there are any errors.
I think you know that there are errors because it won't compile. The reason you have got the error on line 41 is because of your inconsistent use of {...} in conditional statements. I am going to show what I mean by rewriting your push() method- note that this doesn't change how the code works at all, it just makes it easier to avoid mistakes and to find them when they do happen. Rewrite the rest of your code, including the broken show() method, in a similar way and it should be easy to spot where you have gone wrong.

Java:
    // to add integer in the queue from rear end
    void push(int n) {
        if (front == 0 && rear == cap - 1 || front == rear + 1) {
            System.out.println("QUEUE OVERFLOWS");
        } else {
            if (front == -1 && rear == -1) {
                front = 0;
                rear = 0;
            } else {
                if (rear == cap - 1) {
                    rear = 0;
                } else {
                    rear = rear + 1;
                }
            }
            cirque[rear] = n;
        }
    }

Once you have got it working you need a bit more work on the logic of your show() method - what will happen if the queue has wrapped arround and the front is at (say) item 3 and the rear is at item 1?
 
I tried again.This time I was able to compile it it is printing "Queue is Empty" when it should print "Queue is full" Also I don't know what to do with the value returned by the pop() function/should I simply print it?
Java:
import java.io.*;
class CirQueue {
    int cirque[]; //to store the elements of array in circular queue
    int cap; //to store maximum capacity of array
    int front, rear; //to store front and rear indices
    CirQueue(int max) //constructor to initailize cap with the mximum value entered by the console
    {
        cap = max;
        front = 0;
        rear = 0;
        cirque=new int[cap];
    }

    void push(int n) //to add integer in the queue from rear end
    {
        if (front == 0 && rear == cap - 1 || front == rear + 1)
            System.out.println("QUEUE OVERFLOWS");
        else
       {
            if (front == -1 && rear == -1)
            {
                front = 0;
                rear = 0;
            } else
            {
                if (rear == cap - 1)
                {
                  rear = 0;
                }
                else   {
                  rear = rear + 1;
               }
           }
           cirque[rear] = n;
        }
    }

    int pop()
    {
        if (front != rear)
        {
            int n = cirque[front];
            front = (front + 1) % cap;
            return (n);
        }
        else
            return (-9999);
    }

    void show()
    {
        if (front != rear)
         {
            for (int i = front; i <= rear; i = (i + 1) % cap)
            {
                System.out.print(cirque[i] + "\t");
                System.out.println();
            }
         }
        else
                System.out.println("QUEUE IS EMPTY");
     }

    public static void main(String args[])throws IOException
    {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
       System.out.println("ENTR THE MAXIMUM CAPACITY OF THE ARRAY");
       int num=Integer.parseInt(br.readLine());
        CirQueue obj=new CirQueue(num); 
       System.out.println("ENTER THE ELEMENTS TO BE STORED IN THE ARRAY");
       for(int i=0;i<num;i++)
       {
           obj.push(Integer.parseInt(br.readLine()));
       }
       obj.show();
    }
}
 
Last edited by a moderator:
ishika17 said:
I tried again.This time I was able to compile it it is printing "Queue is Empty" when it should print "Queue is full"
Your logic in the show() method is faulty. If the front and rear indexes are both 0, the queue is empty.
ishika17 said:
Also I don't know what to do with the value returned by the pop() function/should I simply print it?
The pop() method returns an int, the value that was popped off the queue. You can print it or you can store it in a variable, whichever you want. Here's an example of how you would store the popped value in a variable. This code would be in main().
Java:
int val;
.
.
.
val = obj.pop();
 
  • Like
Likes   Reactions: ishika17