Learning Data Structures in Java: Circular Queue

AI Thread Summary
The discussion revolves around implementing a circular queue in Java, with specific queries about the main method and handling the return value of the pop() function. The user is advised to create a main method to test the queue functionality and to properly handle the value returned by pop(), suggesting it can be printed or stored in a variable. Errors in the provided code are identified, particularly in the show() method, which incorrectly indicates when the queue is empty. The user is encouraged to revise their logic to ensure accurate queue status reporting and to follow consistent formatting in their code to avoid compilation issues. Overall, the focus is on correcting code errors and understanding circular queue operations.
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 ishika17
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
1
Views
2K
Replies
7
Views
2K
Replies
7
Views
3K
Replies
2
Views
4K
Replies
12
Views
2K
Replies
5
Views
3K
Replies
2
Views
1K
Back
Top