`
xurichusheng
  • 浏览: 336961 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

java 队列

    博客分类:
  • java
阅读更多

 

java 队列

 

 

import java.util.Arrays;

/**
* 
* <队列>
* <先进先出FIFO>
* 
* @author  
* @version  [版本号, 2011-9-20]
* @see  [相关类/方法]
* @since  [产品/模块版本]
*/
public class SequenceQueue<T>{
    //初始化容量
    private int DEFAULT_INIT_SIZE = 10;
    
    //数组的长度
    private int capacity;
    
    //定义一个数组,用于保存顺序栈的元素
    private Object[] elementData;
    
    //队头
    private int front = 0;
    
    //队尾
    private int rear = 0;
    
    /**
     * <以默认数组长度创建顺序队列>
     */
    public SequenceQueue(){
        this.capacity = DEFAULT_INIT_SIZE;
        elementData = new Object[capacity];
    }
    
    /**
     * <以一个初始化元素创建顺序队列>
     * @param element 指定顺序队列中第一个元素
     */
    public SequenceQueue(T element){
        this();
        elementData[0] = element;
        rear++;
    }
    
    /**
     * <以指定长度的数组来创建顺序队列>
     * @param element 指定顺序队列中第一个元素
     * @param initSize 指定顺序队列底层数组的长度
     */
    public SequenceQueue(T element, int initSize){
        this.capacity = initSize;
        elementData = new Object[capacity];
        elementData[0] = element;
        rear++;
    }
    
    /**
     * <获取顺序队列的大小>
     * @return [参数说明]
     * 
     * @return int [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     * @author 
     * @date 2011-9-20
     */
    public int length(){
        return (rear-front);
    }
    
    /**
     * <判断队列是否为空>
     * @return [参数说明]
     * 
     * @return boolean [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     * @author 
     * @date 2011-9-20
     */
    public boolean isEmpty(){
        return rear == front;
    }
    
    /**
     * <插入队列>
     * @param element [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     * @author 
     * @date 2011-9-20
     */
    public void add(T element){
        if (rear > capacity-1){
            throw new IndexOutOfBoundsException("队列已满");
        }
        elementData[rear++] = element;
    }
    
    /**
     * <移除队列,并返回被移除的数据>
     * <在队头进行移除>
     * 
     * @return T [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     * @author 
     * @date 2011-9-20
     */
    @SuppressWarnings("unchecked")
    public T remove(){
        if (isEmpty()){
            throw new IndexOutOfBoundsException("队列为空");
        }
        //取得队头数据
        T frontValue = (T)elementData[front];
        //释放队头元素
        elementData[front++] = null;
        
        return frontValue;
    }
    
    /**
     * <返回队头元素>
     * @return [参数说明]
     * 
     * @return T [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     * @author 
     * @date 2011-9-20
     */
    @SuppressWarnings("unchecked")
    public T element(){
        if (isEmpty()){
            throw new IndexOutOfBoundsException("队列为空");
        }
        return (T)elementData[front];
    }
    
    /**
     * <清空顺序队列>
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     * @author 
     * @date 2011-9-20
     */
    public void clear(){
        //将数组中的所有元素赋为null
        Arrays.fill(elementData, null);
        front = 0;
        rear = 0;
    }
    
    public String toString(){
        if (isEmpty()){
            return "[]";
        }
        StringBuilder sb = new StringBuilder("[");
        for (int i=front; i<rear; i++){
            sb.append(elementData[i].toString()+", ");
        }
        int len = sb.length();
        return sb.delete(len-2, len).append("]").toString();
    }
}

 

测试:

 

public class Test{
    
    /** <一句话功能简述>
     * <功能详细描述>
     * @param args [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     * @author 
     * @date 2011-9-20
     */
    public static void main(String[] args){
        
        SequenceQueue<String> queue = new SequenceQueue<String>();
        
        queue.add("aaaa");
        queue.add("bbbb");
        queue.add("cccc");
        queue.add("dddd");
        
        System.out.println(queue);
        
        System.out.println("队头元素:"+queue.element());
        
        String remove = queue.remove();
        System.out.println("被删除的元素:"+remove);
        
        System.out.println("第一次删除后:"+queue);
    }
    
}

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics