
Implement a class Queue which supports the following operations:
boolean isEmpty()
Returns true if the queue is empty, and false otherwise.
int peak()
Returns the value at the front of a non-empty queue without deleting it.
int pop()
Removes the item at the front of a non-empty queue and returns it.
void push(int o)
Inserts the argument o at the back of the queue.
int size()
Returns the total number of elements in the queue.
You can use an array as your basic data structure and set its size to 100, i.e. assuming that the user will never enter more than 100 items.
Your not allowed to move around any elements inside the array. Instead use two integers front, size which indicates the front and length of the queue. Then wrap around at the end of the array when accessing elements, using the mod operator.

Here is a code that implements a class Queue.
public class Queue{
public Queue( )
{
this( DEFAULT_CAPACITY );
}
public Queue( int capacity )
{
theArray = new Object[ capacity ];
makeEmpty( );
}
public boolean isEmpty( )
{
return currentSize == 0;
}
public boolean isFull( )
{
return currentSize == theArray.length;
}
public void makeEmpty( )
{
currentSize = 0;
front = 0;
back = -1;
}
public Object getFront( )
{
if( isEmpty( ) )
return null;
return theArray[ front ];
}
public Object dequeue( )
{
if( isEmpty( ) )
return null;
currentSize--;
Object frontItem = theArray[ front ];
theArray[ front ] = null;
front = increment( front );
return frontItem;
}
public void enqueue( Object x ) throws Overflow
{
if( isFull( ) )
throw new Overflow( );
back = increment( back );
theArray[ back ] = x;
currentSize++;
}
private int increment( int x )
{
if( ++x == theArray.length )
x = 0;
return x;
}
private Object [ ] theArray;
private int currentSize;
private int front;
private int back;
static final int DEFAULT_CAPACITY = 10;
public static void main( String [ ] args )
{
Queue q = new Queue( );
try
{
for( int i = 0; i < 10; i++ )
q.enqueue( new MyInteger( i ) );
}
catch( Overflow e ) { System.out.println( "Unexpected overflow" ); }
while( !q.isEmpty( ) )
System.out.println( q.dequeue( ) );
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.