資料結構(Java語言)——Queue簡單實現

來源:互聯網
上載者:User

標籤:資料   資料結構   java   隊列   queue   

和棧類似,隊列queue也是表。然而,使用隊列時插入在一端進行而刪除在另一端進行。

隊列的基本操作是enqueue(入隊)和dequeue(出隊),入隊是在隊尾rear(表的末端)插入一個元素,出隊是刪除在隊頭front(表的開頭)的元素。

和棧一樣,對於隊列而言任何錶的實現都可以,而且對於每種操作,鏈表實現和數組實現都是快速的O(1)時間。下面主要討論隊列的迴圈數組實現。

對於每一個隊列數組結構,我們保留一個數組theArray[]以及位置front和rear,它們表示隊列的兩端。我們還要記錄實際存在於隊列中的元素個數currentSize。為使一個元素x入隊,我們執行enqueue()使currentSize和rear增1,然後置theArray[rear]=x。為使元素出隊列,我們置返回值為theArray[front],並且currentSize減1,然後使front加1。以下是相關代碼實現:

import java.util.Iterator;import java.util.NoSuchElementException;public class MyQueue<AnyType> implements Iterable<AnyType> {private static final int DEFAULT_CAPACITY = 10;private AnyType[] theArray; // 以數組存放資料private int currentSize; // 當前隊列的長度,非數組長度private int front; // 隊頭private int rear; // 隊尾/** * 建構函式自動執行clear()函數 */public MyQueue() {clear();}/** * 分配記憶體,初始化各成員變數 */public void clear() {ensureCapacity(DEFAULT_CAPACITY);currentSize = 0;front = rear = 0;}/** * 返回當前數組長度 *  * @return 數組長度 */public int size() {return theArray.length;}/** * 當前隊列是否滿 *  * @return */public boolean isFull() {return currentSize == size();}/** * 當前隊列是否空 *  * @return */public boolean isEmpty() {return currentSize == 0;}/** * 根據參數確定數組長度 *  * @param newCapacity *            新的大小 */@SuppressWarnings("unchecked")public void ensureCapacity(int newCapacity) {// 第一次(數組未分配)直接分配記憶體if (theArray == null) {theArray = (AnyType[]) new Object[newCapacity];return;}// 參數小於當前長度直接返回if (newCapacity < size()) {return;}// 數組擴充時將原資料複製到新分配的數組裡AnyType[] old = theArray;theArray = (AnyType[]) new Object[newCapacity];for (int i = front; i < front + old.length; i++) {theArray[i - front] = old[i % old.length];}// 重新賦值各變數front = 0;rear = old.length - 1;currentSize = old.length;}/** * 入隊操作 *  * @param value *            入隊元素 */public void enqueue(AnyType value) {// 檢查隊列是否滿if (isFull()) {ensureCapacity(size() * 2);}// 第一次入隊時隊尾無資料,直接賦值。之後隊尾先加一取餘再賦值if (theArray[rear] != null) {rear = (rear + 1) % size();}theArray[rear] = value;currentSize++;}/** * 出隊操作 *  * @return 出隊元素 */public AnyType dequeue() {// 若隊列空則無法出隊if (isEmpty()) {throw new NullPointerException();}// 出隊操作currentSize--;AnyType value = theArray[front];theArray[front] = null;front = (front + 1) % size();return value;}/** * 實現Iterable介面 */public Iterator<AnyType> iterator() {return new QueueIterator();}private class QueueIterator implements Iterator<AnyType> {private int current = 0;public boolean hasNext() {return current < size();}public AnyType next() {if (!hasNext()) {throw new NoSuchElementException();}return theArray[current++];}}public static void main(String[] args) {MyQueue<Integer> queue = new MyQueue<Integer>();Iterator<Integer> iterator1 = queue.iterator();Iterator<Integer> iterator2 = queue.iterator();queue.enqueue(1);queue.enqueue(2);queue.enqueue(3);queue.enqueue(4);queue.enqueue(5);queue.enqueue(6);queue.enqueue(7);queue.enqueue(8);queue.enqueue(9);queue.enqueue(10);queue.dequeue();queue.dequeue();queue.dequeue();queue.dequeue();queue.enqueue(11);queue.enqueue(12);System.out.println("隊列未超出容量時結果:");while (iterator1.hasNext()) {System.out.print(iterator1.next() + " ");}System.out.println("\n隊列超出容量時結果:");queue.enqueue(13);queue.enqueue(14);queue.enqueue(15);while (iterator2.hasNext()) {System.out.print(iterator2.next() + " ");}}}
執行結果:

隊列未超出容量時結果:
11 12 null null 5 6 7 8 9 10 
隊列超出容量時結果:
5 6 7 8 9 10 11 12 13 14 15 null null null null null null null null null 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

資料結構(Java語言)——Queue簡單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.