0

I was practicing on how to implement queue using arrays. I have easily implemented how to enqueue and dequeue the elements in queue. But I have encountered an exception while implementing reverse of queue using stacks

public class QueueImpl {
private int capacity;
int queueArr[];
int front = 0;
int rear = -1;
int currentSize = 0;
QueueImpl(int queueSize){
    this.capacity=queueSize;
    queueArr=new int[this.capacity];
}

public void enqueue(int data){
    if(isQueueFull()){
        System.out.println("Overflow");
        return;
    }
    else{
        rear=rear+1;
        if(rear==capacity-1)
        {
            rear=0;
        }
        queueArr[rear]=data;
        currentSize++;
        System.out.println("Element " + data+ " is pushed to Queue !");
    }

}


public int dequeue(){
    if(isQueueEmpty()){
        System.out.println("UnderFlow");
    }
    else{
        front=front+1;
        if(front == capacity-1){
            System.out.println("Pop operation done ! removed: "+queueArr[front-1]);
            front = 0;
        } else {
            System.out.println("Pop operation done ! removed: "+queueArr[front-1]);
        }
        currentSize--;
    }
    return queueArr[front-1];

}
private boolean isQueueEmpty() {
    boolean status=false;
    if(currentSize==0){
        status=true;
    }
    return status;
}

private boolean isQueueFull() {
    boolean status=false;
    if(currentSize==capacity){
        status=true;
    }
    return status;
}

public static void main(String arg[]) {
    QueueImpl queueImpl=new QueueImpl(5);
    queueImpl.enqueue(5);
    queueImpl.enqueue(2);
    queueImpl.enqueue(9);
    queueImpl.enqueue(1);
//  queueImpl.dequeue();
    queueImpl.printQueue(queueImpl);
    queueImpl.reverse(queueImpl);
}

private void printQueue(QueueImpl queueImpl) {
System.out.println(queueImpl.toString());       
}
 @Override
    public String toString() {
        return "Queue [front=" + front + ", rear=" + rear + ", size=" + currentSize
                + ", queue=" + Arrays.toString(queueArr) + "]";
    }
private QueueImpl reverse(QueueImpl queueImpl) throws ArrayIndexOutOfBoundsException {
    int i=0;
    Stack<Integer> stack=new Stack<Integer>();  
    while(!queueImpl.isQueueEmpty()){
        stack.push(queueImpl.dequeue());
    }
    while(!stack.isEmpty()){
        stack.get(i);
        i++;
    }
    while(!stack.isEmpty()){
        queueImpl.enqueue(stack.pop());
    }
    return queueImpl;
}

}

The error log is -

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
 at com.tcs.QueueUsingAraay.QueueImpl.dequeue(QueueImpl.java:51)
 at com.tcs.QueueUsingAraay.QueueImpl.reverse(QueueImpl.java:93)
 at com.tcs.QueueUsingAraay.QueueImpl.main(QueueImpl.java:78)

1 Answer 1

1

There was a problem in your dequeue method: 1. you were making front=0 and were trying to access front-1 which was -1 as a result the exception was thrown.

  1. Removed stack.get as it was not required.

Corrected working code.

public class QueueImpl {
        private int capacity;
        int queueArr[];
        int front = 0;
        int rear = -1;
        int currentSize = 0;

        QueueImpl(int queueSize) {
            this.capacity = queueSize;
            queueArr = new int[this.capacity];
        }

        public void enqueue(int data) {
            if (isQueueFull()) {
                System.out.println("Overflow");
                return;
            } else {
                rear = rear + 1;
                if (rear == capacity - 1) {
                    rear = 0;
                }
                queueArr[rear] = data;
                currentSize++;
                System.out.println("Element " + data + " is pushed to Queue !");
            }

        }

        public int dequeue() {
            int element=-1;
            if (isQueueEmpty()) {
                System.out.println("UnderFlow");
            } else {
                element = queueArr[front];
                front=front+1;
                if (front == capacity - 1) {
                    System.out.println("Pop operation done ! removed: "
                            + queueArr[front - 1]);
                    front = 0;
                } else {
                    System.out.println("Pop operation done ! removed: "
                            + queueArr[front - 1]);
                }
                currentSize--;
            }
            return element;

        }

        private boolean isQueueEmpty() {
            boolean status = false;
            if (currentSize == 0) {
                status = true;
            }
            return status;
        }

        private boolean isQueueFull() {
            boolean status = false;
            if (currentSize == capacity) {
                status = true;
            }
            return status;
        }

        public static void main(String arg[]) {
            QueueImpl queueImpl = new QueueImpl(5);
            queueImpl.enqueue(5);
            queueImpl.enqueue(2);
            queueImpl.enqueue(9);
            queueImpl.enqueue(1);

            queueImpl.printQueue(queueImpl);
            queueImpl.reverse(queueImpl);
            queueImpl.printQueue(queueImpl);
        }

        private void printQueue(QueueImpl queueImpl) {
            System.out.println(queueImpl.toString());
        }

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

        private QueueImpl reverse(QueueImpl queueImpl)
                throws ArrayIndexOutOfBoundsException {
            Stack<Integer> stack = new Stack<Integer>();
            while (!queueImpl.isQueueEmpty()) {
                stack.push(queueImpl.dequeue());
            }
            while (!stack.isEmpty()) {
                queueImpl.enqueue(stack.pop());
            }
            return queueImpl;
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.