Learning Data Structures in Java: Circular Queue

Click For Summary

Discussion Overview

The discussion revolves around implementing a circular queue in Java, focusing on the structure and functionality of the code, particularly the main method, the handling of the pop() function, and issues related to queue overflow and underflow. Participants are addressing both conceptual and technical aspects of the implementation.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant seeks clarification on how to write the main() method and what to do with the value returned by the pop() function.
  • Another participant suggests adding a main method and indicates that the returned value from pop() can be used as desired, recommending testing the queue functionality.
  • Concerns are raised about compilation errors due to inconsistent use of braces in the push() method, with a suggestion to rewrite it for clarity.
  • A participant mentions that the queue is incorrectly reporting "Queue is Empty" instead of "Queue is full," indicating a potential logic flaw in the show() method.
  • There is a discussion about the handling of the value returned by pop(), with suggestions to either print it or store it in a variable for further use.

Areas of Agreement / Disagreement

Participants express differing views on the logic of the queue implementation, particularly regarding the conditions for queue overflow and underflow. There is no consensus on the correctness of the current implementation, as multiple issues are identified and debated.

Contextual Notes

Participants note limitations in the logic of the show() method and the handling of the front and rear indices, which may affect the correct identification of queue states. There are unresolved questions about the overall functionality and correctness of the code.

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K