public class QueueAsArray implements Queue
{
protected int[] myArray;
int count, size, rear = 0;
public QueueAsArray(int newSize)
{
myArray = new int[newSize];
size = newSize;
count = 0;
}
public void purge()
{
int index = 0;
while(count > 0)
{
myArray[index] = 0;
index++;
count--;
}
rear = 0;
}
public int getHead() throws ContainerEmptyException
{
if(count == 0)
throw new ContainerEmptyException();
else
return myArray[0];
}
public void enqueue(int value) throws ContainerFullException
{
if(count == size)
throw new ContainerFullException();
else
{
myArray[rear++] = value;
count++;
}
}
public int dequeue() throws ContainerEmptyException
{
if(count == 0)
throw new ContainerEmptyException();
else
{
int value = myArray[0];
count--;
for(int x = 0; x <= count; x++)
{
myArray[x-1] = myArray[x];
}
rear--;
return value;
}
}
public String toString()
{
String report = "Data in the queue is: ";
for(int x = 0; x < myArray.length; x++)
{
report += myArray[x] + " ";
}
return report;
}
}
Was working on an assigment and ran into a problem where my dequeue function isn't working properly. I can't seem to figure out what the problem is. Thought I could get some fresh eyes on this? Anyone know?
**Data in queue before dequeuing (application output):
Data in the queue is: 77 32 56 22 87 1 46 999 131 91
**Data in queue after 2 consecutive dequeues (application output):
Data in the queue is: 32 56 22 87 1 46 999 131 91 91
Data in the queue is: 56 22 87 1 46 999 131 91 91 91
Side note: Also, my purge method now displays that last two 91's in the queue even after purging...
Data in the queue is: 0 0 0 0 0 0 0 0 91 91
Thanks in advance guys!