HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Array Implementation of Queue

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arrayimplementationqueue

Problem

I've implemented the queue data structures using array in java. Anything I need to change in my code?

Queue.java

import java.util.Arrays;

public class Queue {

    private int front;
    private int rear;
    int size;
    T[] queue;

    public Queue(int inSize) {
        size = inSize;
        queue = (T[]) new Object[size];
        front = -1;
        rear = -1;
    }

    public boolean isempty() {
        return (front == -1 && rear == -1);
    }

    public void enQueue(T value) {
        if ((rear+1)%size==front) {
            throw new IllegalStateException("Queue is full");

        } else if (isempty()) {
            front++;
            rear++;
            queue[rear] = value;

        } else {
            rear=(rear+1)%size;
            queue[rear] = value;

        }
    }

    public T deQueue() {
        T value = null;
        if (isempty()) {
            throw new IllegalStateException("Queue is empty, cant dequeue");
        } else if (front == rear) {
            value = queue[front];
            front = -1;
            rear = -1;

        } else {
            value = queue[front];
            front=(front+1)%size;

        }
        return value;

    }

    @Override
    public String toString() {
        return "Queue [front=" + front + ", rear=" + rear + ", size=" + size
                + ", queue=" + Arrays.toString(queue) + "]";
    }

}


QueueImpl.java

```
public class QueueImpl {

public static void main(String[] args) {
Queue newQueue = new Queue(5);
newQueue.enQueue(10);
newQueue.enQueue(20);
newQueue.enQueue(30);
newQueue.enQueue(40);
newQueue.enQueue(50);
System.out.println((T) newQueue.toString());
System.out.println((T) newQueue.deQueue().toString());
System.out.println((T) newQueue.deQueue().toString());
System.out.println((T) newQueue.toString());
newQueue.enQueue(60);
newQueue.enQueue(70);
System.out.println((T)

Solution

Your fields size and queue have package access, they need to be private so only your class can control them.

private final int size;
private final T[] queue;


And because their values are known at initialization, its good practice to declare them final.

your isempty method is not Camelcase, use isEmpty instead.
Some validation on the size parameter is needed as well.

if(size<=0){
  throw new IllegalArgumentException("Size cannot be less than or equal to zero");
 }


Something I love about the this keyword is that it helps you finding a name for your variables

public Queue(int size) { // size is more relevant than inSize 
   if(size<=0){
       throw new IllegalArgumentException("Size cannot be less than or equal to zero");
    }
    this.size = size;
    queue = (T[]) new Object[size];
    front = -1;
    rear = -1;
}

Code Snippets

private final int size;
private final T[] queue;
if(size<=0){
  throw new IllegalArgumentException("Size cannot be less than or equal to zero");
 }
public Queue(int size) { // size is more relevant than inSize 
   if(size<=0){
       throw new IllegalArgumentException("Size cannot be less than or equal to zero");
    }
    this.size = size;
    queue = (T[]) new Object[size];
    front = -1;
    rear = -1;
}

Context

StackExchange Code Review Q#64258, answer score: 8

Revisions (0)

No revisions yet.