0% found this document useful (0 votes)
3 views2 pages

Lab Assign 3

The ArrayQueue class implements a circular queue using an array, allowing for dynamic resizing and basic queue operations such as enqueue, dequeue, and checking if the queue is empty. It includes additional methods for clearing the queue, checking for palindromes, splicing another queue, and enqueuing items without duplicates. The class is generic, allowing it to handle any data type specified by the user.

Uploaded by

kelly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Lab Assign 3

The ArrayQueue class implements a circular queue using an array, allowing for dynamic resizing and basic queue operations such as enqueue, dequeue, and checking if the queue is empty. It includes additional methods for clearing the queue, checking for palindromes, splicing another queue, and enqueuing items without duplicates. The class is generic, allowing it to handle any data type specified by the user.

Uploaded by

kelly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class ArrayQueue<T> implements QueueInterface<T> {

private T[] queue; // circular array of queue entries and one unused location
private int frontIndex;
private int backIndex;
private static final int DEFAULT_INITIAL_CAPACITY = 50;

public ArrayQueue() {
this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor

public ArrayQueue(int initialCapacity) {


// the cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempQueue = (T[]) new Object[initialCapacity + 1];
queue = tempQueue;
frontIndex = 0;
backIndex = initialCapacity;
} // end constructor

public void enqueue(T newEntry) {


ensureCapacity();
backIndex = (backIndex + 1) % queue.length;
queue[backIndex] = newEntry;
} // end enqueue

public T getFront(){
T front = null;
if (!isEmpty())
front = queue[frontIndex];

return front;
} // end getFront

public T dequeue() {
T front = null;
if (!isEmpty()) {
front = queue[frontIndex];
queue[frontIndex] = null;
frontIndex = (frontIndex + 1) % queue.length;
} // end if

return front;
} // end dequeue

private void ensureCapacity() {


if (frontIndex == ((backIndex + 2) % queue.length)) { // if array is full,
T[] oldQueue = queue;
int oldSize = oldQueue.length;
@SuppressWarnings("unchecked")
T[] tempQueue = (T[]) new Object[2 * oldSize];
queue = tempQueue;
for (int index = 0; index < oldSize - 1; index++) {
queue[index] = oldQueue[frontIndex];
frontIndex = (frontIndex + 1) % oldSize;
} // end for

frontIndex = 0;
backIndex = oldSize - 2;
} // end if
} // end ensureCapacity

public boolean isEmpty() {


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

public void clear() {


if(!isEmpty()) {
for (int index = frontIndex; index != backIndex; index =
(index+1)%queue.length)
queue[index] = null;
queue[backIndex] = null;
}
frontIndex = 0;
backIndex = queue.length - 1;
}

public static boolean check(String s) {


ArrayQueue<String> q = new ArrayQueue<String>();
s = s.replaceAll("[^a-zA-Z]", "");
for(int i=0; i < s.length()/2; i++)
q.enqueue(s.substring(i,i+1));
for(int i=s.length()/2; i<s.length(); i++) {
if(!q.dequeue().equals(s.substring(i,i+1)))
return false;
}
return true;
}

public void splice(QueueInterface<T> anotherQueue) {


while(!anotherQueue.isEmpty()) {
T x = anotherQueue.dequeue();
enqueue(x);
}
}

public boolean enqueueNoDuplicate(T item) {


for (int i=frontIndex; i!=backIndex; i=(i+1)%queue.length)
if(item.equals(queue[i]))
return false;
enqueue(item);
return true;
}

} // end ArrayQueue

You might also like