数据结构之队列结构

数据结构之队列结构

 

 

队列:队列结构是一种线性结构。从数据的存储结构来进一步划分,队列包括两类:
          顺序队列结构:使用一组地址连续的内存单元依次保存队列中的数据;
          链式队列结构:使用链表形式保存队列中各元素的值;

在队列结构中允许对两端进行操作,但是两端的操作不同。在表的一端只能进行删除操作,称为队头;

在表的另一端只能进行插入操作,称为队尾。若队伍中没有数据元素,则称为空队列。

队列是按照“先进先出”的原则处理结点数据的。

 

  1 Java实现的队列:
  2 
  3 /**
  4  * 队列: 先进先出,后进后出的线性表. head端删除;tail端插入.
  5  * 
  6  * @see java.util.ArrayDeque
  7  */
  8 public class xQueue<E> {
  9 
 10     Object[] elements;
 11 
 12     int head; // 队列的头部,位置固定.
 13 
 14     int tail; // 队列的尾部
 15 
 16     private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
 17 
 18     private static final int MAX_CAPACITY = 1 << 30;
 19 
 20     /** 构造 */
 21     public xQueue() {
 22         elements = new Object[DEFAULT_INITIAL_CAPACITY];
 23     }
 24 
 25     /** 构造 */
 26     public xQueue(Collection<? extends E> c) {
 27         elements = new Object[calculateSize(c.size())];
 28         addAll(c);
 29     }
 30 
 31     /* 计算Queue大小 */
 32     private int calculateSize(int capacity) {
 33         if (capacity < DEFAULT_INITIAL_CAPACITY)
 34             return DEFAULT_INITIAL_CAPACITY;
 35         int c = (int) Math.pow(2, capacity >> 2);
 36         return Math.min(c, MAX_CAPACITY);
 37     }
 38 
 39     /* 扩容 */
 40     private void doubleCapacity() {
 41         int b = elements.length;
 42         int newCapacity = Math.min(b << 1, MAX_CAPACITY);
 43         if (newCapacity < 0)
 44             throw new IllegalStateException("xQueue too big");
 45         Object[] o = new Object[newCapacity];
 46         System.arraycopy(elements, 0, o, 0, b);
 47         elements = o;
 48     }
 49 
 50     /**
 51      * 插入若干个元素
 52      * 
 53      * @param c
 54      * @return
 55      */
 56     public boolean addAll(Collection<? extends E> c) {
 57         boolean modified = false;
 58         for (E e : c)
 59             if (add(e))
 60                 modified = true;
 61         return modified;
 62     }
 63 
 64     /**
 65      * 将指定的元素插入队列,成功则返回true.
 66      * 
 67      * @param e
 68      * @return
 69      */
 70     public boolean add(E e) {
 71         Objects.requireNonNull(e);
 72         int l = elements.length;
 73         if (tail < l) {
 74             elements[tail] = e;
 75             tail++;
 76         } else if (tail >= l) {
 77             doubleCapacity(); // 扩容
 78             elements[tail] = e;
 79             tail++;
 80         }
 81         return true;
 82     }
 83 
 84     /**
 85      * 获取但不移除队列的头.若队列为空,则抛出异常.
 86      * 
 87      * @throws NoSuchElementException
 88      * @return
 89      */
 90     public E getHead() {
 91         @SuppressWarnings("unchecked")
 92         E result = (E) elements[head];
 93         if (result == null)
 94             throw new NoSuchElementException();
 95         return result;
 96     }
 97 
 98     /**
 99      * 获取并移除队列的头
100      * 
101      * @return
102      */
103     public E remove() {
104         int h = head;
105         @SuppressWarnings("unchecked")
106         E result = (E) elements[h];
107         if (result == null)
108             return null;
109         elements[h] = null;
110         int l = elements.length;
111         Object[] o = new Object[l];
112         System.arraycopy(elements, h + 1, o, 0, l - 1); // 移除头后,所有元素都要向前移动,保证头永远是0.
113         elements = o;
114         tail--;
115         return result;
116     }
117 
118     /**
119      * 获取队列长度
120      * 
121      * @return
122      */
123     public int length() {
124         return (tail - head) & (elements.length - 1);
125     }
126 
127     /**
128      * 返回queue对象字符串表示
129      */
130     public String toString() {
131         if (length() == 0)
132             return "[]";
133         Object[] o = Arrays.copyOf(elements, length());
134         return Arrays.toString(o);
135     }
136 
137 }

猜你喜欢

转载自www.cnblogs.com/ggf123456789/p/9820312.html
今日推荐