隊列(Queue)是隻容許在一端進行插入,而在另外一端進行刪除的運算受限的線性表。java
(1)容許刪除的一端稱爲隊頭(Front)。數組
(2)容許插入的一端稱爲隊尾(Rear)。
(3)當隊列中沒有元素時稱爲空隊列。
(4)隊列亦稱做先進先出(First In First Out)的線性表,簡稱爲FIFO表。
隊列的修改是依先進先出的原則進行的。新來的成員老是加入隊尾,每次離開的成員老是隊列頭上的(不容許中途離隊)。app
隊列的順序存儲結構函數
隊列的順序存儲結構稱爲順序隊列,順序隊列其實是運算受限的順序表。oop
和順序表同樣,順序隊列利用內存中一段連續的存儲空間來存放當前隊列中的元素。
因爲隊列的隊頭和隊尾的位置是變化的,設置兩個指針front和rear分別指示隊頭元素和隊尾元素,它們的初值在隊列初始化時均應置爲0。this
入隊時:將新元素插入rear所指的位置的後一位。
出隊時:刪去front所指的元素,而後將front加1並返回被刪元素。spa
①「下溢」現象
當隊列爲空時,作出隊運算產生的溢出現象。「下溢」是正常現象,經常使用做程序控制轉移的條件。.net
② "真上溢"現象
當隊列滿時,作進棧運算產生空間溢出的現象。「真上溢」是一種出錯狀態,應設法避免。指針
③ "假上溢"現象
因爲入隊和出隊操做中,頭尾指針只增長不減少,導致被刪元素的空間永遠沒法從新利用。當隊列中實際的元素個數遠遠小於內存中本分配的空間時,也可能因爲尾指針已超越向量空間的上界而不能作入隊操做。該現象稱爲"假上溢"現象。以下圖code
如上圖所示,這種頭尾相接的順序存儲結構稱爲循環隊列(circular queue)。
循環隊列中須要注意的幾個重要問題:
①隊空的斷定條件,隊空的條件是front=rear;
②隊滿的斷定條件,(rear+1)%QueueSize=front。QueueSize爲隊列初始空間大小。
[java] view plain copy
隊列的順序存儲結構實現
public class Queue<E> { private Object[] data=null; private int maxSize; //隊列容量 private int front; //隊列頭,容許刪除 private int rear; //隊列尾,容許插入 //構造函數 public Queue(){ this(10); } public Queue(int initialSize){ if(initialSize >=0){ this.maxSize = initialSize; data = new Object[initialSize]; front = rear =0; }else{ throw new RuntimeException("初始化大小不能小於0:" + initialSize); } } //判空 public boolean empty(){ return rear==front?true:false; } //插入 public boolean add(E e){ if(rear== maxSize){ throw new RuntimeException("隊列已滿,沒法插入新的元素!"); }else{ data[rear++]=e; return true; } } //返回隊首元素,但不刪除 public E peek(){ if(empty()){ throw new RuntimeException("空隊列異常!"); }else{ return (E) data[front]; } } //出隊 public E poll(){ if(empty()){ throw new RuntimeException("空隊列異常!"); }else{ E value = (E) data[front]; //保留隊列的front端的元素的值 data[front++] = null; //釋放隊列的front端的元素 return value; } } //隊列長度 public int length(){ return rear-front; } }
循環隊列的順序存儲結構實現
import java.util.Arrays; public class LoopQueue<E> { public Object[] data = null; private int maxSize; // 隊列容量 private int rear;// 隊列尾,容許插入 private int front;// 隊列頭,容許刪除 private int size=0; //隊列當前長度 public LoopQueue() { this(10); } public LoopQueue(int initialSize) { if (initialSize >= 0) { this.maxSize = initialSize; data = new Object[initialSize]; front = rear = 0; } else { throw new RuntimeException("初始化大小不能小於0:" + initialSize); } } // 判空 public boolean empty() { return size == 0; } // 插入 public boolean add(E e) { if (size == maxSize) { throw new RuntimeException("隊列已滿,沒法插入新的元素!"); } else { data[rear] = e; rear = (rear + 1)%maxSize; size ++; return true; } } // 返回隊首元素,但不刪除 public E peek() { if (empty()) { throw new RuntimeException("空隊列異常!"); } else { return (E) data[front]; } } // 出隊 public E poll() { if (empty()) { throw new RuntimeException("空隊列異常!"); } else { E value = (E) data[front]; // 保留隊列的front端的元素的值 data[front] = null; // 釋放隊列的front端的元素 front = (front+1)%maxSize; //隊首指針加1 size--; return value; } } // 隊列長度 public int length() { return size; } //清空循環隊列 public void clear(){ Arrays.fill(data, null); size = 0; front = 0; rear = 0; } }
隊列的鏈式存儲結構實現
public class LinkQueue<E> { // 鏈棧的節點 private class Node<E> { E e; Node<E> next; public Node() { } public Node(E e, Node next) { this.e = e; this.next = next; } } private Node front;// 隊列頭,容許刪除 private Node rear;// 隊列尾,容許插入 private int size; //隊列當前長度 public LinkQueue() { front = null; rear = null; } //判空 public boolean empty(){ return size==0; } //插入 public boolean add(E e){ if(empty()){ //若是隊列爲空 front = new Node(e,null);//只有一個節點,front、rear都指向該節點 rear = front; }else{ Node<E> newNode = new Node<E>(e, null); rear.next = newNode; //讓尾節點的next指向新增的節點 rear = newNode; //以新節點做爲新的尾節點 } size ++; return true; } //返回隊首元素,但不刪除 public Node<E> peek(){ if(empty()){ throw new RuntimeException("空隊列異常!"); }else{ return front; } } //出隊 public Node<E> poll(){ if(empty()){ throw new RuntimeException("空隊列異常!"); }else{ Node<E> value = front; //獲得隊列頭元素 front = front.next;//讓front引用指向原隊列頭元素的下一個元素 value.next = null; //釋放原隊列頭元素的next引用 size --; return value; } } //隊列長度 public int length(){ return size; } }
基於LinkedList實現隊列結構
/** * 使用java.util.Queue接口,其底層關聯到一個LinkedList(雙端隊列)實例. */ import java.util.LinkedList; import java.util.Queue; public class QueueList<E> { private Queue<E> queue = new LinkedList<E>(); // 將指定的元素插入此隊列(若是當即可行且不會違反容量限制),在成功時返回 true, //若是當前沒有可用的空間,則拋出 IllegalStateException。 public boolean add(E e){ return queue.add(e); } //獲取,可是不移除此隊列的頭。 public E element(){ return queue.element(); } //將指定的元素插入此隊列(若是當即可行且不會違反容量限制),當使用有容量限制的隊列時, //此方法一般要優於 add(E),後者可能沒法插入元素,而只是拋出一個異常。 public boolean offer(E e){ return queue.offer(e); } //獲取但不移除此隊列的頭;若是此隊列爲空,則返回 null public E peek(){ return queue.peek(); } //獲取並移除此隊列的頭,若是此隊列爲空,則返回 null public E poll(){ return queue.poll(); } //獲取並移除此隊列的頭 public E remove(){ return queue.remove(); } //判空 public boolean empty() { return queue.isEmpty(); } }