Search code examples
javaarraysarraydeque

Dequeues in Java using arrays + odd and even numbers


I'm having some trouble with my programming exercise in which I should implement dequeues using arrays.

I already got the operations I need but after the implementation you should run through the numbers 1-20 and insert the even numbers at the end of the dequeue and the odd numbers add the beginning.

After that you should use the method removeFront to remove all numbers in the list and should print them on the console.

There is also the hint that the correct output is: (19,17,15...,1,2,4,...,20).

My problem now is that the number 1 is missing in the list and instead it prints out a null value as the first item to be removed.

public class Dequeues<E> {

private final int max;
private int head;
private int tail;
private E[] deque;
private int counter;

public Dequeues(int max) {
    this.max = max;
    deque = (E[]) new Object[max];
    this.head = 0;
    this.tail = 0;
    this.counter = 0;
}

public boolean isEmpty (){
    return (counter == 0);
}

public boolean isFull() {
    return(counter>= max);
}

public void addFront (E x){
    if(!isFull()) {
        if (head == 0) {
            head = deque.length-1;
            deque[head] = x;
        } else {
            deque[head--] = x;
        }
        counter++;
    }
    else throw new IndexOutOfBoundsException("Stack is full!");
}

public void addBack(E x){
    if(!isFull()) {
        if(tail == deque.length-1) {
            tail = 0;
            deque[tail] = x;
        } else {
            deque[tail++] = x;
        }
        counter++;
    }
    else throw new IndexOutOfBoundsException("Stack is full!");
}

public E removeFront(){
    if(!isEmpty()) {
        E ret = deque[head];
        deque[head++] = null;
        if(head >= deque.length) {
            head = 0;
        }
        counter--;
        return ret;
    }
    else throw new IndexOutOfBoundsException("Stack is empty");
}

public E removeBack(){
    if (!isEmpty()) { 
        E ret = deque[tail]; 
        deque[tail--] = null;
        if(tail < 0) {
            tail = deque.length-1;
        }
        counter--;
        return ret;
        }
    else throw new IndexOutOfBoundsException("Stack is empty");
}


public static void main (String [] args) {
Dequeues test = new Dequeues(20);
for (int i = 1; i <= test.deque.length; i++) {
    if(i % 2 == 0) {
        test.addBack(i);
    } else if(i % 2 == 1) {
        test.addFront(i);
    }
}

System.out.println("Use of removeFront and output of the values: ");
for (int i = 0; i < test.deque.length; i++) {       
    System.out.print(test.removeFront() + " ");
    }   
}}

Output is the following:

Use of removeFront and output of the values: null 19 17 15 13 11 9 7 5 3 2 4 6 8 10 12 14 16 18 20


Solution

  • You just simply wrong used -- operator. Right implementation of the addFront method should be:

    public void addFront (E x){
        if(!isFull()) {
            if (head == 0) {
                head = deque.length-1;
                deque[head] = x;
            } else {
                deque[--head] = x;
            }
            counter++;
        }
        else throw new IndexOutOfBoundsException("Stack is full!");
    }
    

    So, the difference is here deque[--head] = x;

    --head means reduce head value by one and then use it.

    head-- means use value head and then reduce its value

    Your situation was:

    1. head = deque.length-1; head == 19

    2. head != 0 and you go to the else statement. head value = 19. You used head-- and got again 19 and decremented it by one, but had to use --head.