View Javadoc

    1   package baseCode.dataStructure;
    2
    3   /***
    4    * Simple Queue implementation.
    5    * <p>
    6    * Copyright (c) 2004
    7    * </p>
    8    * <p>
    9    * Institution: Columbia University
    10   * </p>
    11   *
    12   * @author Paul Pavlidis
    13   * @version $Id: Queue.java,v 1.4 2004/07/29 08:38:49 pavlidis Exp $
    14   * @deprecated -- use java.util.List instead.
    15   */
    16  public class Queue {
    17
    18     private Object[] queue;
    19     private int front;
    20     private int back;
    21     private int currentSize;
    22
    23     private final static int DEFAULTCAPACITY = 10000;
    24
    25     public Queue() {
    26        this( DEFAULTCAPACITY );
    27     }
    28
    29     public Queue( int capacity ) {
    30        queue = new Object[capacity];
    31        makeEmpty();
    32     }
    33
    34     /***
    35      * @param obj Object
    36      */
    37     public void enqueue( Object obj ) {
    38        if ( isFull() ) {
    39           throw new IndexOutOfBoundsException(
    40                 "Attempt to enqueue in a full queue" );
    41        }
    42        back = increment( back );
    43        queue[back] = obj;
    44        currentSize++;
    45     }
    46
    47     /***
    48      * @return Object
    49      */
    50     public Object dequeue() {
    51        if ( isEmpty() ) {
    52           return null;
    53        }
    54        currentSize--;
    55        Object f = queue[front];
    56        queue[front] = null;
    57        front = increment( front );
    58        return f;
    59     }
    60
    61     /***
    62      * @return boolean
    63      */
    64     public boolean isEmpty() {
    65        return currentSize == 0;
    66     }
    67
    68     /***
    69      * @return boolean
    70      */
    71     public boolean isFull() {
    72        return queue.length == currentSize;
    73     }
    74
    75     /***
    76      *
    77      */
    78     public void makeEmpty() {
    79        currentSize = 0;
    80        front = 0;
    81        back = -1;
    82     }
    83
    84     private int increment( int i ) {
    85        if ( ++i == queue.length ) {
    86           i = 0;
    87        }
    88        return i;
    89     }
    90  }