Matt Erickson
Matt Erickson

Reputation: 11

JAVA how to implement wraparound on a deque

I am trying to implement wraparound on my double ended queue and for some reason my insertRight() and removeRight() methods are outputting wrong and my removeLeft() method is just throwing an error. I have looked around and I cannot seem to find an answer to why my methods are not working here they are:

public void insertRight(int newItem) {
    if (isFull() == true) {
        System.out.print("Full");
    } // end of if
    else {
        if (right == capacity) {
            right = 0;
        } // end of nested if
        else {
            deque[right] = newItem;
            nElems++;
            right++;
        } // end of nested else
    } // end of else
}// end of insertRight

public void removeRight() {

    if (isEmpty() == true) {
        System.out.println("Empty");
    } // end of if
    if (isEmpty() == false) {
        System.out.println(right);
        if (right == capacity) {
            right = 0;
        } // end of nested if
        int temp = deque[right];
        right++;
        nElems--;
    } // end of if
}// end of removeRight

public void removeLeft() {
    if (isEmpty() == true) {
        System.out.println("Empty");
    } // end of if
    if (isEmpty() == false) {
        if (left == capacity) {
            left = -1;
        } // end of nested if
        else {
            System.out.println(left);
            int temp = deque[left];
            left++;
            nElems--;
        } // end of else
    } // end of if
}// end of removeLeft

Upvotes: 1

Views: 1286

Answers (2)

Lothar
Lothar

Reputation: 5449

I see a couple of issues here:

    if (right == capacity) {
        right = 0;
    } // end of nested if
    else {
        deque[right] = newItem;
        nElems++;
        right++;
    } // end of nested else

If right == capacity you reset the index but don't insert newItem to the array.

It should be something like this (directly typed, not tested):

    if (right == capacity) {
        right = 0;
    } // end of nested if
    deque[right] = newItem;
    nElems++;
    right++;

Now to your removeRight-method:

    System.out.println(right);
    if (right == capacity) {
        right = 0;
    } // end of nested if
    int temp = deque[right];
    right++;
    nElems--;

Here you use the same algorithm to check boundaries but it should be a "mirror" to the one you used in insertRight, so something like this (directly typed, not tested):

    System.out.println(right);
    if (right == 0) {
        right = capacity;
    } // end of nested if
    int temp = deque[right - 1];
    right--;
    nElems--;

And finally removeLeft:

    if (left == capacity) {
        left = -1;
    } // end of nested if
    else {
        System.out.println(left);
        int temp = deque[left];
        left++;
        nElems--;
    } // end of else

Without the insertLeft method, I can only guess that it has similar problems.

Upvotes: 0

Peter Kramer
Peter Kramer

Reputation: 192

A little more information on the actual meaning/values of left and right would be helpful.

At first glance, it looks like removeLeft() fails because the wrap-around point for left would be 0, not capacity, if my understanding of your code so far is correct.

Also, negative array indices do not work in java. You'll want to refer to the actual last index directly.

And I really recommend looking into code formatting. Your indentation makes it very hard to tell where one block ends and a new one begins. You could save yourself explicit comments by just following a consistent indentation pattern:

public void insertRight(int newItem) {
    if (isFull()) {
        System.out.print("Full");
    } else {
        if (right == capacity) {
            right = 0;
        } else {
            deque[right] = newItem;
            nElems++;
            right++;
        }
    }
}


public void removeRight() {

    if (isEmpty()) {
        System.out.println("Empty");
    } else {
        System.out.println(right);
        if (right == capacity) {
            right = 0;
        }
        int temp = deque[right];
        right++;
        nElems--;
    }
}


public void removeLeft() {
    if (isEmpty()) {
        System.out.println("Empty");
    } else {
        // My assumption inserted here:
        if (left == 0) {
            left = capacity - 1;
        } else {
            System.out.println(left);
            int temp = deque[left];
            left++;
            nElems--;
        }
    }
}

Upvotes: 1

Related Questions