3
\$\begingroup\$

Here is my code of Array Implementation of Queue in java.

import java.util.Scanner;
class CircularQueue
{
int maxSize;
int rear;
int front;
int aQueue[];
{
    rear = -1;
    front = -1;
}
CircularQueue(int maxSize)
{
    this.maxSize = maxSize;
    this.aQueue = new int[maxSize];
}
void enQueue(int item)
{
    if(((rear+1) % maxSize) == front)
    {
        System.out.println("Queue is Full");
    }
    else
    {
        if (rear == front && front == -1)
        {
            front += 1;
        }
        rear = (rear+1) % maxSize;
        aQueue[rear] = item;
    }
}
void deQueue()
{
    if(rear == front && rear == -1)
    {
        System.out.println("Queue is Empty.");
    }
    else
    {
        int item = aQueue[front];
        if(rear == front)
        {
            rear = -1;
            front = -1;
        }
        else
        {
            front = (front + 1) % maxSize;
        }
        System.out.println(item + " is deQueued from the Queue");
    }
}

String elementOrElements()
{
    String send = (rear == front)? (" ") :("s ");
    return send;
}

void display()
{
    int tmpfront = front;
    if(rear == front && rear == -1)
            {
                    System.out.println("Queue is Empty.");
            }
            else
    {
        System.out.println("The element"+ elementOrElements() + "on the Queue are:- ");
        for(int i=0; i<maxSize ; i++)
        {
            if(tmpfront != rear)
            {
                System.out.println(aQueue[tmpfront]);
                tmpfront = (tmpfront + 1) % maxSize;
            }
            else
            {
                System.out.println(aQueue[rear]);
                break;
            }
        }
    }
}
public static void main(String []args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter total no of elements to be in the queue:- ");
    int maxSize = input.nextInt();
    CircularQueue queue = new CircularQueue(maxSize);
    int select;
    int term = 0;
    while (term == 0)
    {
        System.out.print("\nOption:\tTo Do:\n1\tTo push element.\n2\tTo POP element.\n3\tTo Display the Queue elements.\n4\tTo Exit the Program.\nEnter your option:- ");
                select = input.nextInt(); 
        switch(select)
        {
            case 1:
            {
                System.out.print("Enter element to insert in the Queue:- ");
                int ele = input.nextInt();
                queue.enQueue(ele); 
                break;
            }
            case 2:
            {
                queue.deQueue();
                break;
            }
            case 3:
            {
                queue.display();
                break;
            }
            case 4:
            {
                term = 1;
                System.out.println("Thank you!");
                break;
            }
            default:
                System.out.println("Enter a valid options");
        }
    }
  }
}

Is there anything I can do to improve it? Code written for DSA class practical.

\$\endgroup\$
1
  • \$\begingroup\$ maxSize seems redundant. Just use aQueue.length instead. \$\endgroup\$
    – markspace
    Commented Oct 22, 2019 at 15:56

2 Answers 2

5
\$\begingroup\$
  1. Since this is in Java and for academic purposes, my guess would be that you're using Eclipse. Hit CTRL+SHIFT+F to format your code, and move any lines that it chops up back into one where appropriate. The key binding is similar for Netbeans and IntelliJ's IDEA.

  2. Too many useless variables. You have a bad habit of creating variables for one off-uses, e.g. in elementOrElements(). Just return what send equals. If you want to describe what the code does, then use comments (which you don't use at all in your implementation btw).

  3. If the functionality isn't obvious, then use comments. Most programmers can discern what generic methods and operations look like, but when in doubt comment. Remember that you're writing software primarily for other humans. This is critical in a professional environment.

  4. Delete elementOrElements(). In the single print call in which it's used, just say, The contents of the Queue are:.

  5. Consistent variable naming is required. You have tmpfront, which should be tempFront according to Java conventions. Not a huge deal since you seem to adhere to this throughout the program. I add the extra e out of personal preference (and the fact that it helps other humans ever so slightly).

  6. Use proper Queue naming conventions, i.e. use enqueue instead of enQueue. Creating a couple of helper methods like isEmpty() and isFull() would help your implementation in a couple of places.

  7. Move CircularQueue to CircularQueue.java and add some public modifiers to your methods. This is mainly for convention training.

\$\endgroup\$
4
\$\begingroup\$

Having a queue program running, is an achievement of its own. So please take the following hints as suggestions for your next learning steps on your path to a software engineer. Maybe some of the concepts will be new for you, and that's quite OK. Rome wasn't built in a day...

A Queue should be a component that you can re-use without modification if you need one in some future program. It is not meant to be a stand-alone program.

So, change the deQueue() method to return the item instead of printing it inside of this method, and let the outside (in your case the main() method) do the printing. Maybe this next program wants to do some computation with the numbers it gets from the queue before printing the results, or maybe it's some embedded program controlling a quad-copter that doesn't even have a place to write text to.

Similarly, instead of printing that the queue is full or empty, have the methods throw an exception if enQueue() can't accept a new item or if deQueue() has no more items available. That'll introduce you to the important concept of exception-handling, where even seasoned developers sometimes struggle to find the right way.

To give your Queue's user a chance to see whether it can enqueue or dequeue another item, add two more methods returning boolean: isFull() and isEmpty().

Right now, your Queue can only accept integer items. Using Java Generics, you can change the code so that you can use the same Queue source code for integers, for floats, for strings, and any other data type you want. That's another special concept that takes some time to get familiar with, but it's worth the pain.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.