データ構造の図(深さ優先探索と広さ優先探索で使用されるキューとスタック)


package graph;

/**
 * 
 *                  
 * @version  [   , 2012-12-25]
 */
public class Stack
{
    /**
     *      ,     
     */
    private int[] data;
    /**
     *        
     */
    private int nElement;
    
    /**
     * 
     */
    public Stack()
    {
        this(20);
    }
    
    /**
     * 
     */
    public Stack(int capacity)
    {
        data = new int[capacity];
        nElement = 0;
    }
    
    /**
     *        
     * @return
     * @see [ 、 #  、 #  ]
     */
    public boolean isEmpty()
    {
        return nElement == 0;
    }
    
    /**
     *     ,    ,    
     * @param index
     * @see [ 、 #  、 #  ]
     */
    public void push(int index)
    {
        if (nElement == data.length)
        {
            int[] temp = new int[data.length * 2];
            System.arraycopy(data, 0, temp, 0, data.length);
            data = temp;
        }
        data[nElement++] = index;
    }
    
    /**
     *     
     * @return
     * @throws Exception 
     * @see [ 、 #  、 #  ]
     */
    public int pop()
        throws Exception
    {
        if (isEmpty())
        {
            throw new Exception("Empty Stack Exception");
        }
        return data[--nElement];
    }
    
    /**
     *         
     * @return
     * @see [ 、 #  、 #  ]
     */
    public int peek()
    {
        return data[nElement - 1];
    }
}
/*
 *      :  Queue.java
 *     :  2012-12-25
 */
package graph;

/**
 *                       
 * @version  [   , 2012-12-25]
 * @see  [   /  ]
 * @since  [  /    ]
 */
public class Queue
{
    /**
     *       ,     
     */
    private int[] data;
    /**
     *         
     */
    private int nElement;
    /**
     *        
     */
    private int head, tail;
    
    /**
     * 
     */
    public Queue()
    {
        this(20);
    }
    
    /**
     * 
     */
    public Queue(int capacity)
    {
        if (capacity <= 0)
        {
            capacity = 20;
        }
        data = new int[capacity];
        nElement = 0;
        head = tail = -1;
    }
    
    /**
     *         
     * @return
     * @see [ 、 #  、 #  ]
     */
    public boolean isEmpty()
    {
        return nElement == 0;
    }
    
    /**
     *     ,     ,    
     * @param index
     * @see [ 、 #  、 #  ]
     */
    public void enQueue(int index)
    {
        // 
        if (isEmpty())
        {
            data[nElement++] = index;
            head = tail = 0;
        }
        // 
        else if (nElement == data.length)
        {
            int[] temp = new int[data.length * 2];
            for (int i = 0, current = head; i < nElement; i++, current++)
            {
                if (current >= nElement)
                {
                    current -= nElement;
                }
                temp[i] = data[current];
            }
            data = temp;
            data[nElement++] = index;
            head = 0;
            tail = nElement - 1;
        }
        //  
        else
        {
            if (tail + 1 >= data.length)
            {
                tail -= data.length;
            }
            data[++tail] = index;
            nElement++;
        }
    }
    
    /**
     *     
     * @return
     * @throws Exception 
     * @see [ 、 #  、 #  ]
     */
    public int deQueue()
        throws Exception
    {
        if (isEmpty())
        {
            throw new Exception("Empty Queue Exception");
        }
        else
        {
            if (head + 1 >= data.length)
            {
                head -= data.length;
            }
            nElement--;
            return data[head++];
        }
    }
    
    /**
     *         
     * @return
     * @see [ 、 #  、 #  ]
     */
    public int peek()
    {
        return data[head];
    }
}