實現一個隊列

隊列的定義:

隊列(Queue)是隻容許在一端進行插入,而在另外一端進行刪除的運算受限的線性表。java

(1)容許刪除的一端稱爲隊頭(Front)。數組

(2)容許插入的一端稱爲隊尾(Rear)。
(3)當隊列中沒有元素時稱爲空隊列。
(4)隊列亦稱做先進先出(First In First Out)的線性表,簡稱爲FIFO表。
   隊列的修改是依先進先出的原則進行的。新來的成員老是加入隊尾,每次離開的成員老是隊列頭上的(不容許中途離隊)。app

隊列的存儲結構及實現

隊列的順序存儲結構函數

(1) 順序隊列的定義:

 隊列的順序存儲結構稱爲順序隊列,順序隊列其實是運算受限的順序表。oop

(2)順序隊列的表示:

和順序表同樣,順序隊列利用內存中一段連續的存儲空間來存放當前隊列中的元素。
因爲隊列的隊頭和隊尾的位置是變化的,設置兩個指針front和rear分別指示隊頭元素和隊尾元素,它們的初值在隊列初始化時均應置爲0。this

 

(3)順序隊列的基本操做


入隊時:將新元素插入rear所指的位置的後一位。
出隊時:刪去front所指的元素,而後將front加1並返回被刪元素。spa

(4)順序表的溢出現象

 ①「下溢」現象
 當隊列爲空時,作出隊運算產生的溢出現象。「下溢」是正常現象,經常使用做程序控制轉移的條件。.net

② "真上溢"現象
當隊列滿時,作進棧運算產生空間溢出的現象。「真上溢」是一種出錯狀態,應設法避免。指針

③ "假上溢"現象
因爲入隊和出隊操做中,頭尾指針只增長不減少,導致被刪元素的空間永遠沒法從新利用。當隊列中實際的元素個數遠遠小於內存中本分配的空間時,也可能因爲尾指針已超越向量空間的上界而不能作入隊操做。該現象稱爲"假上溢"現象。以下圖code

 循環隊列:

 如上圖所示,這種頭尾相接的順序存儲結構稱爲循環隊列(circular queue)。

循環隊列中須要注意的幾個重要問題:

①隊空的斷定條件,隊空的條件是front=rear;

②隊滿的斷定條件,(rear+1)%QueueSize=front。QueueSize爲隊列初始空間大小。

循環隊列的java實現代碼

[java] view plain copy

在CODE上查看代碼片派生到個人代碼片

  1. package study_02.datastructure.queue;  
  2.   
  3. /** 
  4.  * 循環隊列 
  5.  *
  6.  */  
  7. public class CirQueue<E> {  
  8.     //對象數組,隊列最多存儲a.length-1個對象  
  9.     E[] a;  
  10.     //默認初始化大小  
  11.     private static final int DEFAULT_SIZE=10;  
  12.     //對首下標  
  13.     int front;  
  14.     //隊尾下標  
  15.     int rear;  
  16.       
  17.     public CirQueue(){  
  18.         this(DEFAULT_SIZE);  
  19.     }  
  20.     /** 
  21.      * 初始化指定長度的隊列 
  22.      * @param size 
  23.      */  
  24.     @SuppressWarnings("unchecked")  
  25.     public CirQueue(int size){  
  26.         a=(E[])(new Object[size]);  
  27.         front=0;  
  28.         rear=0;  
  29.     }  
  30.       
  31.     /** 
  32.      * 將一個對象追加到隊列尾部 
  33.      * @param obj 
  34.      * @return 隊列滿時返回false,不然返回true 
  35.      * @author WWX 
  36.      */  
  37.     public boolean enqueue(E obj){  
  38.         if((rear+1)%a.length==front){  
  39.             return false;  
  40.         }else{  
  41.             a[rear]=obj;  
  42.             rear=(rear+1)%a.length;  
  43.             return true;  
  44.         }  
  45.     }  
  46.       
  47.     /** 
  48.      * 隊列頭部出隊 
  49.      * @return 
  50.      * @author WWX 
  51.      */  
  52.     public E dequeue(){  
  53.         if(rear==front)  
  54.             return null;  
  55.         else{  
  56.             E obj =a[front];  
  57.             front=(front+1)%a.length;  
  58.             return obj;  
  59.         }  
  60.     }  
  61.       
  62.     /** 
  63.      * 隊列長度 
  64.      * @return 
  65.      * @author WWX 
  66.      */  
  67.     public  int size(){  
  68.         return (rear-front)&(a.length-1);  
  69.     }  
  70.     //隊列長度(另外一種方法)  
  71.     public int length(){  
  72.         if(rear>front){  
  73.             return rear-front;  
  74.         }else  
  75.             return a.length-1;  
  76.     }  
  77.       
  78.     /** 
  79.      * 判斷是否爲空  
  80.      * @return 
  81.      * @author WWX 
  82.      */  
  83.     public boolean isEmpty(){  
  84.         return rear==front;  
  85.     }  
  86.       
  87.   
  88.   
  89.     public static void main(String[] args) {  
  90.         CirQueue<String> queue=new CirQueue<String>(4);  
  91.         queue.enqueue("1");  
  92.         queue.enqueue("2");  
  93.         queue.enqueue("3");  
  94.         System.out.println("size="+queue.size());  
  95.         int size=queue.size();  
  96.         System.out.println("*******出棧操做*******");  
  97.         for(int i=0; i<size;i++){  
  98.             System.out.print(queue.dequeue()+" ");  
  99.         }  
  100.           
  101.     }  
  102.       

 

 

隊列順序存儲結構實現

複製代碼

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();
    }
}
相關文章
相關標籤/搜索