0

when I call my toString() method it doesn't work if after the index wraps around (front > rear). I have included the code below. After, I enqueue(5) and enqueue(6), the toString seems to be completely ignored. At first I thought I wasn't overriding the default toString in Java but the first print statement clearly tells me I am. Anyways, check the code out:

public class driver {
public static void main(String[] args) {

    Queue queue = new Queue(4);
    System.out.println(queue);
    queue.enqueue(1);
    queue.enqueue(2);
    queue.enqueue(3);
    queue.enqueue(4);
    System.out.println(queue);
    queue.dequeue();
    System.out.println(queue);

    queue.dequeue();
    System.out.println(queue);

    queue.enqueue(5);
    queue.enqueue(6);

    System.out.println(queue);


}

public static class Queue {

    int front;
    int rear;
    int capacity;
    int[] queue;

    public Queue(int size) {
        this.capacity = size;
        this.front = this.rear = -1;
        this.queue = new int[capacity];
    }

    @Override
    public String toString() {
        String str = "";
        if (front > rear) {
            int i = front;
            while (i != rear) {
                str = str + queue[i % queue.length] + " ";
                i++;
            }
            //str= str+queue[rear];
        }
        if (front < rear) {
            for (int i = front; i <= rear; i++) {
                str = str + queue[i];
            }
        }
        if (front == rear) {
            str = "This Queue is Empty. Please Enqueue";
        }

        return str;
    }


    public boolean isFull() {

        return ((rear == this.queue.length - 1 && front == 0) || rear == front - 1);

    }

    public boolean isEmpty() {
        return (front == -1);
    }

    public void enqueue(int elem) {
        if (isFull()) {
            System.out.println("Full Queue - dequeue an element if you need to add an element in the queue");
        } else {
            if (isEmpty()) {
                this.queue[++rear] = elem;
                front = 0;
            } else {
                rear = (rear + 1) % this.queue.length;
                this.queue[rear] = elem;
            }
        }
    }

    public int dequeue() {
        if (isEmpty()) {
            System.out.println("empty queue. Enqueue some elments. ");
            return -1;
        } else {
            int store = this.queue[front];

            if (rear == front) {
                front = rear = -1;
            } else {

                front = front + 1 % this.queue.length;

            }
            return store;

        }

    }

}

*there is a return curly bracket here too lol still new to posting questions. P.S can someone help me because apparently I posted too much code in my question. Any workarounds?

2
  • It looks to me first glance that i should be decremented instead of incremented in the first case. (i starts out as front, and then you add one to it and see if it is equal to rear, but front > rear already... I am surprised you don't just enter an infinite loop there) Commented Jul 15, 2019 at 18:58
  • 1
    There is an infinite loop @Slabgorb. He just needs to loop it back around to 0 once capacity is reached Commented Jul 15, 2019 at 18:59

1 Answer 1

1

The issue is with i++ in your while loop of toString.

Consider the case where front = 3 and rear = 1 then you start the loop with i = front = 3. However, you keep incrementing until you reach i == rear which will never happen since rear < front

What you want is for i to loop back around to 0 once it reaches capacity. You can do this by removing i++ and replacing it with i = (i + 1) % capacity;

It seems like you have a different bug in your code, because when I run it I see

This Queue is Empty. Please Enqueue
1234
234
34
3 4 5

You'll need to figure this one out by yourself.

You can even condense it into a single for statement

for(int i = front; i != rear; i = (++i) % capacity)
Sign up to request clarification or add additional context in comments.

1 Comment

ooh I love this trick i = (i + 1) % capacity that's good stuff - I would have done it with about three or four statements

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.