<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 數組隊列的問題 數組隊列的實現是具有局限性的,關鍵問題就是出隊的時間復雜度是O(n)級別的 . 當隊首元素出隊的時候所有的元素要向前移動一位, 這樣如果有百萬量的數據的話,消耗的計算資源是非常龐大的.有沒有其他方法呢 ? ![](https://box.kancloud.cn/b7545bcd22fa5added4b5cb6752a69a3_1526x419.png) 當我們推出隊首的元素后,所有元素位置保持不變,front++ ### 循環隊列 當front == tail 的時候隊列為空,當有新元素入隊,tail++,當推出元素就front++. 當tail超過隊列的容量之后,tail的位置移動到最前面,可以把這個隊列看成是一個環形的.所以叫循環隊列. ![](https://box.kancloud.cn/aba67a9190d9330777e72272a97f10a2_1510x418.png) 當front == tail 的時候隊列為空,所以當tail + 1 == front 隊列滿,就說明我們要擴容了.在這里我們有意識的浪費了一個空間 . tail +1 == front說明隊列滿其實是不準確的. 真實應該是(tail+1)%c(容量) == front 說明隊列滿. ### 實現 ~~~ public class LoopQueue<E> implements Queue<E> { private E[] data; private int front, tail; private int size; public LoopQueue(int capacity) { data = (E[]) new Object[capacity + 1]; front = 0; tail = 0; size = 0; } public LoopQueue() { this(10); } public int getCapacity() { return data.length - 1; } private void resize(int newCapacity) { E[] newData = (E[]) new Object[newCapacity + 1]; for(int i = 0; i < size; i++) { newData[i] = data[(i + front) % data.length]; } data = newData; front = 0; tail = size; } @Override public boolean isEmpty() { return front == tail; } @Override public int getSize() { return size; } @Override public void enqueue(E e) { if((tail + 1) % data.length == front) resize(getCapacity() * 2); data[tail] = e; tail = (tail + 1) % data.length; size++; } @Override public E dequeue() { if(isEmpty()) throw new IllegalArgumentException("cannot dequeue frm empty queue."); E ret = data[front]; data[front] = null; front = (front + 1) % data.length; size--; if(size == getCapacity() / 4 && getCapacity() / 2 != 0) resize(getCapacity() / 2); return ret; } @Override public E getFront() { if(isEmpty()) throw new IllegalArgumentException("Queue is empty"); return data[front]; } @Override public String toString() { StringBuilder res = new StringBuilder(); res.append(String.format("Queue:size = %d ,capacity = %d\n", size, getCapacity())); res.append("front ["); for(int i = front; i != tail; i = (i + 1) % data.length) { res.append(data[i]); if((i + 1) % data.length != tail) res.append(","); } res.append("] tail"); return res.toString(); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看