Learning Data Structures in Java: Circular Queue

In summary: System.out.println("QUEUE OVERFLOWS"); } else { if (front == -1 && rear == -1) { front = 0; rear = 0; } else
  • #1
ishika17
10
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
  • #2
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?
 
  • #3
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:
  • #4
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

Related to Learning Data Structures in Java: Circular Queue

What is a circular queue in Java?

A circular queue in Java is a linear data structure that follows the FIFO (First In First Out) principle. It is a type of queue in which the first element added to the queue will also be the first one to be removed. However, unlike a regular queue, a circular queue has a fixed size and the elements are stored in a circular manner, meaning that when the last element is reached, the next element will be added at the beginning of the queue, thus creating a circular structure.

What are the advantages of using a circular queue in Java?

There are several advantages of using a circular queue in Java, including:

  • Efficient use of memory: As a circular queue has a fixed size, it uses memory more efficiently compared to other data structures like arrays or linked lists.
  • Efficient insertion and deletion operations: As elements are added and removed from the beginning and end of the queue, the time complexity for these operations is O(1), making them more efficient.
  • Can be used in applications with limited memory: Due to its efficient use of memory, circular queues are often used in embedded systems or applications with limited memory.

How do you implement a circular queue in Java?

To implement a circular queue in Java, you can use an array or a linked list. If using an array, you would need to keep track of the front and rear indices, and use modular arithmetic to handle the circular structure. If using a linked list, you would need to keep track of the head and tail nodes, and use the tail node's next pointer to point back to the head node, creating a circular structure.

What are some common operations performed on a circular queue in Java?

Some common operations performed on a circular queue in Java include:

  • Enqueue: Adding an element to the end of the queue.
  • Dequeue: Removing an element from the beginning of the queue.
  • Peek: Returning the element at the front of the queue without removing it.
  • IsFull: Checking if the queue is full.
  • IsEmpty: Checking if the queue is empty.

What are some real-world applications of circular queues in Java?

Circular queues are commonly used in applications that require a fixed size data structure and efficient insertion and deletion operations. Some examples include:

  • Operating systems: Circular queues are used in operating systems to manage processes and threads.
  • Buffering data: In computer networks, circular queues are used to buffer data packets before they are transmitted.
  • Printer spooling: Circular queues are used in printer spooling systems to manage print jobs.
  • Music and video players: Circular queues can be used to implement a playlist in music or video players, where the next song or video is played after the current one finishes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top