0

I have a homework assignment, this is my first question on here and have been working at this for a while. Normally I can figure things out but i'm really stuck. Here is my code and my output using a tester. My moveToBack is kind of a copy paste from the enqueue method. I still haven't figured out why the output is whacky. It doesn't even act like an enqueue!

import java.util.*;

public class NoDuplicatesArrayQueue<T> implements
        NoDuplicatesQueueInterface<T>, java.io.Serializable {
    private T[] queue;
    private int frontIndex;
    private int backIndex;
    private static final int INITIAL_CAPACITY = 50;
    private int length = 0;

    public NoDuplicatesArrayQueue() {
        this(INITIAL_CAPACITY);
    }// end NoDuplicatesArrayQueue

    public NoDuplicatesArrayQueue(int initialCapacity) {
        queue = (T[]) new Object[initialCapacity + 1];
        frontIndex = 0;
        backIndex = initialCapacity;
    }// end NoDuplicatesArrayQueue

    public void display() {
        for (int i = 0; i < length; i++)
            System.out.println(queue[i]);
    }// end display

    public T dequeue() {
        T front = null;

        if (!isEmpty()) {
            front = queue[frontIndex];
            frontIndex = (frontIndex + 1) % queue.length;

        } // end if
        length--;
        return front;
    } // end dequeue

    public void enqueue(T newEntry) {
        boolean found = false;
        for (int index = 0; !found && (index < length); index++) {
            if (newEntry.equals(queue[index]))
                found = true;
        }// end for
        if (found == false) {
            if (isArrayFull())
                doubleArray();

            backIndex = (backIndex + 1) % queue.length;
            queue[backIndex] = newEntry;
            // System.out.println(length);
            length++;
        } else {
            System.out.println("A duplicate exists");
        }//end else
    }//end enqueue

    public T getFront() {
        T front = null;

        if (!isEmpty())
            front = queue[frontIndex];

        return front;
    } // end getFront

    public boolean isEmpty() {
        return frontIndex == ((backIndex + 1) % queue.length);
    } // end isEmpty

    public void clear() {
        if (!isEmpty()) { // deallocates only the used portion
            for (int index = frontIndex; index != backIndex; index = (index + 1)
                    % queue.length) {
                queue[index] = null;
            } // end for

            queue[backIndex] = null;
        } // end if

        frontIndex = 0;
        backIndex = queue.length - 1;
    } // end clear

    public void moveToBack(T newEntry) {
        boolean found = false;
        for (int index = 0; !found && (index < length); index++) {
            if (newEntry.equals(queue[index]))
                found = true;
        }//end for
        if (found == false) {
            // if (isArrayFull())
            // doubleArray();

            backIndex = (backIndex + 1) % queue.length;
            queue[backIndex] = newEntry;
            System.out.println(newEntry);
            length++;
        } else {
            System.out.println("A duplicate exists");
        }//end else
    }//end moveToBack

    private boolean isArrayFull() {
        return frontIndex == ((backIndex + 2) % queue.length);
    } // end isArrayFull

    private void doubleArray() {
        T[] oldQueue = queue;
        int oldSize = oldQueue.length;

        queue = (T[]) new Object[2 * oldSize];

        for (int index = 0; index < oldSize - 1; index++) {
            queue[index] = oldQueue[frontIndex];
            frontIndex = (frontIndex + 1) % oldSize;
        } // end for

        frontIndex = 0;
        backIndex = oldSize - 2;
    } // end doubleArray
}

OUTPUT TESTER

public class LabDPartBDriver {

      public static void main(String args[])
    {
        NoDuplicatesArrayQueue<Integer> test1 = new NoDuplicatesArrayQueue<Integer>();

        test1.enqueue(1);
        test1.enqueue(3);
        test1.enqueue(2);
        test1.enqueue(0);
        test1.enqueue(-1);
        System.out.println("The queue has ");
        test1.display();
        System.out.println();


        test1.enqueue(test1.dequeue());
        test1.enqueue(test1.dequeue());
        test1.enqueue(test1.dequeue());
        test1.enqueue(test1.dequeue());
        test1.enqueue(test1.dequeue());
        System.out.println("The queue should be the same ");
        test1.display();
        System.out.println();


        test1.enqueue(1);
        test1.enqueue(3);
        test1.enqueue(2);
        test1.enqueue(0);
        test1.enqueue(-1);
        System.out.println("The queue should be the same ");
        test1.display();
        System.out.println();


        test1.moveToBack(3);
        System.out.println("The queue should be 1, 2, 0, -1, 3 ");
        test1.display();
        System.out.println();

        test1.moveToBack(0);
        System.out.println("The queue should be 1, 2, -1, 3, 0 ");
        test1.display();
        System.out.println();

        test1.moveToBack(1);
        System.out.println("The queue should be 2, -1, 3, 0, 1");
        test1.display();
        System.out.println();

        test1.moveToBack(5);
        test1.moveToBack(5);
        test1.moveToBack(5);
        test1.moveToBack(5);
        test1.moveToBack(5);
        test1.moveToBack(5);
        System.out.println("The queue should be 2, -1, 3, 0, 1, 5");
        test1.display();
        System.out.println();


    }


}

OUTPUT.. The queue has 1 3 2 0 -1

A duplicate exists A duplicate exists The queue should be the same 1 3 2

A duplicate exists A duplicate exists A duplicate exists The queue should be the same 1 3 2 0 -1

A duplicate exists The queue should be 1, 2, 0, -1, 3 1 3 2 0 -1

A duplicate exists The queue should be 1, 2, -1, 3, 0 1 3 2 0 -1

A duplicate exists The queue should be 2, -1, 3, 0, 1 1 3 2 0 -1

5 5 5 5 5 5 The queue should be 2, -1, 3, 0, 1, 5 1 3 2 0 -1 2 0 -1 0 -1 5

1 Answers1

0

it says its duplicate because when you dequeue it you are not removing the value from the array.. you are just getting the value and returning it..

to explain in detail consider this

  | 1 | 3 | 2 | 0 | -1 |

        ^(front pointer moved after deque)

while enqueing in the for loop you are searching from index = 0,

and in queue[0] resides the value 1 and you are enqueing the dequed 1 (which is not really dequeued) which results in a duplicate entry.

And i would also suggest you to consider using an ArrayList instead of an array and use an Iterator to iterate the list and remove when dequeing.

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43