資料結構:隊列

來源:互聯網
上載者:User

標籤:let   方法   利用   htm   queue   last   object   width   efi   

資料結構:隊列先入先出的資料結構說明

  

  在先入先出資料結構中,將首先處理隊列中的第一個元素,即front所指的位置元素

  如所示,隊列是典型的 FIFO 資料結構。插入(insert)操作也稱作入隊(enqueue)新元素始終被添加在隊列的末尾 刪除(delete)操作也被稱為出隊(dequeue)。 你只能移除第一個元素

樣本入隊操作

     

出隊操作

 

隊列實現

  為了實現隊列,我們可以使用動態數組和指向隊列頭部的索引

  如上所述,隊列應支援兩種操作:入隊和出隊。入隊會向隊列追加一個新元素,而出隊會刪除第一個元素。 所以我們需要一個索引來指出起點

// "static void main" must be defined in a public class.class MyQueue {    // store elements    private List<Integer> data;             // a pointer to indicate the start position    private int p_start;                public MyQueue() {        data = new ArrayList<Integer>();        p_start = 0;    }    /** Insert an element into the queue. Return true if the operation is successful. */    public boolean enQueue(int x) {        data.add(x);        return true;    };        /** Delete an element from the queue. Return true if the operation is successful. */    public boolean deQueue() {        if (isEmpty() == true) {            return false;        }        p_start++;        return true;    }    /** Get the front item from the queue. */    public int Front() {        return data.get(p_start);    }    /** Checks whether the queue is empty or not. */    public boolean isEmpty() {        return p_start >= data.size();    }     };public class Main {    public static void main(String[] args) {        MyQueue q = new MyQueue();        q.enQueue(5);        q.enQueue(3);        if (q.isEmpty() == false) {            System.out.println(q.Front());        }        q.deQueue();        if (q.isEmpty() == false) {            System.out.println(q.Front());        }        q.deQueue();        if (q.isEmpty() == false) {            System.out.println(q.Front());        }    }}

  上面的實現很簡單,但在某些情況下效率很低。 隨著起始指標的移動,浪費了越來越多的空間。 當我們有空間限制時,這將是難以接受的

  

  如果我們出隊了一個元素,此時第一個位置將空出來,這個空間的浪費我們可以使用迴圈隊列來解決

迴圈隊列說明

  迴圈隊列是一種線性資料結構,其動作表現基於 FIFO(先進先出)原則並且隊尾被串連在隊首之後以形成一個迴圈。它也被稱為“環形緩衝器”。

  迴圈隊列的一個好處是我們可以利用這個隊列之前用過的空間。在一個普通隊列裡,一旦一個隊列滿了,我們就不能插入下一個元素,即使在隊列前面仍有空間。但是使用迴圈隊列,我們能使用這些空間去儲存新的值。

實現
class MyCircularQueue {        private int[] data;    private int head;    private int tail;    private int size;    /** Initialize your data structure here. Set the size of the queue to be k. */    public MyCircularQueue(int k) {        data = new int[k];        head = -1;        tail = -1;        size = k;    }        /** Insert an element into the circular queue. Return true if the operation is successful. */    public boolean enQueue(int value) {        if (isFull() == true) {            return false;        }        if (isEmpty() == true) {            head = 0;        }        tail = (tail + 1) % size;        data[tail] = value;        return true;    }        /** Delete an element from the circular queue. Return true if the operation is successful. */    public boolean deQueue() {        if (isEmpty() == true) {            return false;        }        if (head == tail) {            head = -1;            tail = -1;            return true;        }        head = (head + 1) % size;        return true;    }        /** Get the front item from the queue. */    public int Front() {        if (isEmpty() == true) {            return -1;        }        return data[head];    }        /** Get the last item from the queue. */    public int Rear() {        if (isEmpty() == true) {            return -1;        }        return data[tail];    }        /** Checks whether the circular queue is empty or not. */    public boolean isEmpty() {        return head == -1;    }        /** Checks whether the circular queue is full or not. */    public boolean isFull() {        return ((tail + 1) % size) == head;    }}/** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue obj = new MyCircularQueue(k); * boolean param_1 = obj.enQueue(value); * boolean param_2 = obj.deQueue(); * int param_3 = obj.Front(); * int param_4 = obj.Rear(); * boolean param_5 = obj.isEmpty(); * boolean param_6 = obj.isFull(); */
隊列用法

  除基本Collection操作外,隊列還提供額外的插入,提取和檢查操作。這些方法中的每一種都以兩種形式存在:一種在操作失敗時拋出異常,另一種返回特殊值(null或false,具體取決於操作)。後一種形式的插入操作專門用於容量限制的隊列 實現; 在大多數實現中,插入操作不會失敗。

// "static void main" must be defined in a public class.public class Main {    public static void main(String[] args) {        // 1. Initialize a queue.        Queue<Integer> q = new LinkedList();        // 2. Get the first element - return null if queue is empty.        System.out.println("The first element is: " + q.peek());        // 3. Push new element.        q.offer(5);        q.offer(13);        q.offer(8);        q.offer(6);        // 4. Pop an element.        q.poll();        // 5. Get the first element.        System.out.println("The first element is: " + q.peek());        // 7. Get the size of the queue.        System.out.println("The size is: " + q.size());    }}

 

資料結構:隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.