Question: A queue is a first in first out linear data structure in which items are added at one end and removed from the other end.
A queue is a first in first out linear data structure in which items are added at one end and removed from the other end.
A generic Queue class can be built with the following specification:
Queue() : creates an empty queue
void enqueue(T item): add new item to the end of the queue
T dequeue(): remove and return the item from the front of the queue int size(): return the number of elements in the queue
boolean isEmpty(): return true if the queue is empty, false otherwise. void clear(): clear the queue
T peek(): return the entry from the front of the queue, null if the queue is empty In addition to the above basic operations, it is useful to have the following for queue applications:
int positionOf(T item): Return the position of the specified item and -1 if not found. void remove(T item): Remove the first occurrence (from front) of specified item
T first(): Return the first item in the queue(front), null if queue is empty
T next(): Return the next item in the queue relative to the previous call to first or next. Return null if end of queue is reached.
Implement the generic Queue class using an ArrayList to store the Queue elements. Ensure appropriate error checks.
public class GenericQueue
{
private ArrayList
int cursor; //the cursor is mainly used for the first and the next methods.
//continue rest of the code
}
Write a demo program to test the various methods in the GenericQueue class similar to the GenericStack.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
