0% found this document useful (0 votes)
72 views

Queue Java

The ArrayQueue class implements a queue using an array to store elements. It provides methods to check if the queue is empty, add elements to the back of the queue, remove elements from the front of the queue, and view the front element. When the queue is full, it doubles the size of the underlying array to make room for more elements.

Uploaded by

boyzdc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Queue Java

The ArrayQueue class implements a queue using an array to store elements. It provides methods to check if the queue is empty, add elements to the back of the queue, remove elements from the front of the queue, and view the front element. When the queue is full, it doubles the size of the underlying array to make room for more elements.

Uploaded by

boyzdc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

 

public class ArrayQueue implements Queue
{

    public ArrayQueue( )
    {
        theArray = new Object[ DEFAULT_CAPACITY ];
        makeEmpty( );
    }
    public boolean isEmpty( )
    {
        return currentSize == 0;
    }

    public void makeEmpty( )
    {
        currentSize = 0;
        front = 0;
        back = -1;
    }
     
    public Object dequeue( )
    {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayQueue dequeue" );
        currentSize--;

        Object returnValue = theArray[ front ];
        front = increment( front );
        return returnValue;
    }
    

    public Object getFront( )
    {
        if( isEmpty( ) )
            throw new UnderflowException( "ArrayQueue getFront" );
        return theArray[ front ];
    }

    public void enqueue( Object x )
    {
        if( currentSize == theArray.length )
            doubleQueue( );
        back = increment( back );
        theArray[ back ] = x;
        currentSize++;
    }

    private int increment( int x )
    {
        if( ++x == theArray.length )
            x = 0;
        return x;
    }
    
    private void doubleQueue( )
    {
        Object [ ] newArray;

        newArray = new Object[ theArray.length * 2 ];

            // Copy elements that are logically in the queue
        for( int i = 0; i < currentSize; i++, front = increment( front ) 
)
            newArray[ i ] = theArray[ front ];

        theArray = newArray;
        front = 0;
        back = currentSize - 1;
    }

    private Object [ ] theArray;
    private int        currentSize;
    private int        front;
    private int        back;

    private static final int DEFAULT_CAPACITY = 10;
    
    
}
public class UnderflowException extends RuntimeException
{

    public UnderflowException( String message )
    {
        super( message );
    }
}

You might also like