Queue队列实现---有界队列BQueue

  • 先进先出
/* 
 * @(#)BQueue.java 
 */ 
package ds.util; 
import java.util.NoSuchElementException; 
/** 
 * This class implements a finite queue using a circular queue model 
 * to store and retrieve elements. 
 */
有界队列底层结构是固定大小的数组,这个数组被设定为循环队列。 
/* 
 * @(#)BQueue.java 
 */ 
package ds.util; 
import java.util.NoSuchElementException; 
/** 
 * This class implements a finite queue using a circular queue model 
 * to store and retrieve elements. 
 */
有界队列底层结构是固定大小的数组,这个数组被设定为循环队列。 
public class BQueue<T> implements Queue<T>{ 
   public static final int MAXQSIZE = 50; 

   // array holding the queue elements 
   private T[] queueArray; 
   // index of the front and back of the queue
qfront对应队列的第一个元素索引位置,qback对应队列要增加元素的索引位置。
通过索引位置的移动减少系统开销,避免移动数组元素 
   private int qfront, qback; 
   // the number of elements in the queue, 0 <= count <= MAXQSIZE 
   private int count; 

   /** 
    * Creates an empty queue that holds a maximum of 50 elements with the 
    * specified generic type T. 
    */ 
   public BQueue() 
   { 
      this(MAXQSIZE); 
   } 

   /** 
    * Creates an empty queue that has a specified fixed size for elements 
with the 
    * specified generic type T. 
    * @param size  maximum number of elements in the queue. 
    */ 
   public BQueue(int size) 
   { 
      queueArray = (T[])new Object[size]; 
      qfront = 0; 
      qback = 0; 
      count = 0; 
   } 

   /** 
    * Insert item at the back of the queue provided the queue is not full. 
Throws 
    * an IndexOutOfBoundsException exception if the queue is full. 
    * @param item  insert item at the back of the queue 
    * @throws IndexOutOfBoundsException if the queue is full. 
    */ 
   public void push(T item) 
   { 
      // is the array filled up? if so, throw IndexOutOfBoundsException 
      if (count == MAXQSIZE) 
         throw new IndexOutOfBoundsException("BQueue push(): queue full"); 

      // perform a circular queue insertion 
      queueArray[qback] = item;
增加元素,将下一个增加位置锁定至下一位,并可以循环 
      qback = (qback+1) % MAXQSIZE; 

      // increment the queue size 
      count++; 
   } 

   /** 
    * Remove the element at the front of the queue and return its value. 
    * @return value of the element removed from the front of the queue. 
    * @throws <tt>NoSuchElementException</tt> if the queue is empty. 
    */ 
   public T pop() 
   { 
      // if queue is empty, throw NoSuchElementException 
      if (count == 0) 
         throw new NoSuchElementException("BQueue pop(): empty queue"); 

      // save the front of the queue 
      T queueFront = queueArray[qfront]; 

      // perform a circular queue deletion
删除队列第一个元素,并将第一个元素索引前移 
      qfront = (qfront+1) % MAXQSIZE; 

      // decrement the queue size 
      count--; 

      // return the front 
      return queueFront; 
   } 

   /** 
    * Return the value of the element at the front of the queue. 
    * @return value of element at the front of the queue. 
    * @throws <tt>NoSuchElementException</tt> if the queue is empty. 
    */ 
   public T peek() 
   { 
      // if queue is empty, throw NoSuchElementException 
      if (count == 0) 
         throw new NoSuchElementException("BQueue front(): empty queue"); 
直接返回qfront队列第一个索引元素 
      return queueArray[qfront]; 
   } 

   /** 
    * Return the number of elements currently in the queue. 
    * @return number of elements in the queue. 
    */ 
   public int size() 
   { 
      return count; 
   } 

   /** 
    * Return a boolean value that indicates whether the queue is empty. 
Return 
    * true if empty and false if not empty. 
    * @return true if the queue is empty and false otherwise. 
    */ 
   public boolean isEmpty() 
   { 
      return count == 0; 
   } 

   /** 
    * Return a boolean value that indicates whether the queue is full. 
Return 
    * true if full and false if not full. 
    * @return true if the queue is full and false otherwise. 
    */ 
   public boolean full() 
   { 
      return count == MAXQSIZE; 
   } 

   /** 
    * Returns a string that displays the elements in the queue from 
    * front to back. The description is a comma separated list of 
    * elements enclosed in brackets. 
    * @return string that contains the list of elements in the queue. 
    */ 
   public String toString() 
   { 
        if (queueArray == null) 
            return "null"; 
        else if (queueArray.length == 0) 
            return "[]"; 

        // start with the left bracket 
        String str = "[" + queueArray[0]; 

        // append all but the last element, separating items with 
a comma 
        // polymorphism calls toString() for the array type 
        for (int i = 1; i < queueArray.length; i++) 
            str +=  ", " + queueArray[i]; 

        str += "]"; 

        return str; 
    }}

public class BQueue<T> implements Queue<T>{ 
   public static final int MAXQSIZE = 50; 

   // array holding the queue elements 
   private T[] queueArray; 
   // index of the front and back of the queue
qfront对应队列的第一个元素索引位置,qback对应队列要增加元素的索引位置。
通过索引位置的移动减少系统开销,避免移动数组元素 
   private int qfront, qback; 
   // the number of elements in the queue, 0 <= count <= MAXQSIZE 
   private int count; 

   /** 
    * Creates an empty queue that holds a maximum of 50 elements with the 
    * specified generic type T. 
    */ 
   public BQueue() 
   { 
      this(MAXQSIZE); 
   } 

   /** 
    * Creates an empty queue that has a specified fixed size for elements 
with the 
    * specified generic type T. 
    * @param size  maximum number of elements in the queue. 
    */ 
   public BQueue(int size) 
   { 
      queueArray = (T[])new Object[size]; 
      qfront = 0; 
      qback = 0; 
      count = 0; 
   } 

   /** 
    * Insert item at the back of the queue provided the queue is not full. 
Throws 
    * an IndexOutOfBoundsException exception if the queue is full. 
    * @param item  insert item at the back of the queue 
    * @throws IndexOutOfBoundsException if the queue is full. 
    */ 
   public void push(T item) 
   { 
      // is the array filled up? if so, throw IndexOutOfBoundsException 
      if (count == MAXQSIZE) 
         throw new IndexOutOfBoundsException("BQueue push(): queue full"); 

      // perform a circular queue insertion 
      queueArray[qback] = item;
增加元素,将下一个增加位置锁定至下一位,并可以循环 
      qback = (qback+1) % MAXQSIZE; 

      // increment the queue size 
      count++; 
   } 

   /** 
    * Remove the element at the front of the queue and return its value. 
    * @return value of the element removed from the front of the queue. 
    * @throws <tt>NoSuchElementException</tt> if the queue is empty. 
    */ 
   public T pop() 
   { 
      // if queue is empty, throw NoSuchElementException 
      if (count == 0) 
         throw new NoSuchElementException("BQueue pop(): empty queue"); 

      // save the front of the queue 
      T queueFront = queueArray[qfront]; 

      // perform a circular queue deletion
删除队列第一个元素,并将第一个元素索引前移 
      qfront = (qfront+1) % MAXQSIZE; 

      // decrement the queue size 
      count--; 

      // return the front 
      return queueFront; 
   } 

   /** 
    * Return the value of the element at the front of the queue. 
    * @return value of element at the front of the queue. 
    * @throws <tt>NoSuchElementException</tt> if the queue is empty. 
    */ 
   public T peek() 
   { 
      // if queue is empty, throw NoSuchElementException 
      if (count == 0) 
         throw new NoSuchElementException("BQueue front(): empty queue"); 
直接返回qfront队列第一个索引元素 
      return queueArray[qfront]; 
   } 

   /** 
    * Return the number of elements currently in the queue. 
    * @return number of elements in the queue. 
    */ 
   public int size() 
   { 
      return count; 
   } 

   /** 
    * Return a boolean value that indicates whether the queue is empty. 
Return 
    * true if empty and false if not empty. 
    * @return true if the queue is empty and false otherwise. 
    */ 
   public boolean isEmpty() 
   { 
      return count == 0; 
   } 

   /** 
    * Return a boolean value that indicates whether the queue is full. 
Return 
    * true if full and false if not full. 
    * @return true if the queue is full and false otherwise. 
    */ 
   public boolean full() 
   { 
      return count == MAXQSIZE; 
   } 

   /** 
    * Returns a string that displays the elements in the queue from 
    * front to back. The description is a comma separated list of 
    * elements enclosed in brackets. 
    * @return string that contains the list of elements in the queue. 
    */ 
   public String toString() 
   { 
        if (queueArray == null) 
            return "null"; 
        else if (queueArray.length == 0) 
            return "[]"; 

        // start with the left bracket 
        String str = "[" + queueArray[0]; 

        // append all but the last element, separating items with 
a comma 
        // polymorphism calls toString() for the array type 
        for (int i = 1; i < queueArray.length; i++) 
            str +=  ", " + queueArray[i]; 

        str += "]"; 

        return str; 
    }}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值