Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The array based implementation of the ADT queue in this chapter assumed a maximu

ID: 3544627 • Letter: T

Question

The array based implementation of the ADT queue in this chapter assumed a maximum queue size of 50 items. Modify this implementation so that when the queue becomes full, the size of the array is doubled. Write a test program which can demonstrate the added feature of doubling size of the array.




public class QueueArrayBased implements QueueInterface

{

private final int MAX_QUEUE = 50;

private Object[] items;

private int front, back, count;

public QueueArrayBased()

{

items = new Object[MAX_QUEUE];

front = 0;

back = MAX_QUEUE-1;

count = 0;

}

public boolean isEmpty()

{

return count == 0;

}

public boolean isFull()

{

return count == MAX_QUEUE;

}

public void enqueue(Object newItem) throws QueueException

{

if (!isFull())

{

back = (back+1) % (MAX_QUEUE);

items[back] = newItem;

++count;

}

else

{

throw new QueueException("QueueException on enqueue: " + "queue full");

}

}

public Objectdequeue() throws QueueException

{

if (!isEmpty())

{

Object queueFront = items[front];

front = (front+1) % (MAX_QUEUE);

--count;

return queueFront;

}

else

{

throw new QueueException("QueueException on dequeue: " + "Queue empty");

}

}

public void dequeueAll()

{

items = new Object[MAX_QUEUE];

front = 0;

back = MAX_QUEUE-1;

count = 0;

}

public Object peek() throws QueueException

{

if (!isEmpty())

{

return items[front];

}

else

{

throw new QueueException("QueueException on peek:" + "Queue empty");

}

}

}

Explanation / Answer

http://pastebin.com/FudYkpkK


There's the new implementation code.


The only differences are that I changed MAX_SIZE from a constant (final) to being mutable (variable) and then continued on to change enqueue to create temporary array that's double the size of the other, storing all current values from items[] into it, then setting that array as the value of items, and finally, setting size equal to double its previous value.


In order to test the data structure, you may want to reduce the initial size from 50 to something quite a bit smaller. You could even start it at 1 and it would increase as you added more values (if it works properly, that is)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote