黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 設計循環隊列詳解

設計循環隊列詳解

更新時間:2022-06-06 09:51:17 來源:動力節點 瀏覽1160次

設計循環隊列的實現。循環隊列是一種線性數據結構,其操作基于FIFO(先進先出)原則,最后一個位置與第一個位置連接形成一個圓圈。它也被稱為“環形緩沖區”。

循環隊列的好處之一是我們可以利用隊列前面的空間。在普通隊列中,一旦隊列滿了,即使隊列前面有空間,我們也無法插入下一個元素。但是使用循環隊列,我們??可以使用空間來存儲新值。

您的實現應支持以下操作:

MyCircularQueue(k):構造函數,設置隊列大小為k。

Front:從隊列中獲取最前面的項目。如果隊列為空,則返回 -1。

Rear:從隊列中獲取最后一項。如果隊列為空,則返回 -1。

enQueue(value): 將一個元素插入循環隊列。如果操作成功,則返回 true。

deQueue():從循環隊列中刪除一個元素。如果操作成功,則返回 true。

isEmpty():檢查循環隊列是否為空。

isFull():檢查循環隊列是否已滿。

例子:

MyCircularQueue circularQueue = new MycircularQueue(3); // set the size to be 3
circularQueue.enQueue(1);  // return true
circularQueue.enQueue(2);  // return true
circularQueue.enQueue(3);  // return true
circularQueue.enQueue(4);  // return false, the queue is full
circularQueue.Rear();  // return 3
circularQueue.isFull();  // return true
circularQueue.deQueue();  // return true
circularQueue.enQueue(4);  // return true
circularQueue.Rear()

分析

數組排序實現:

重點是確定循環的空位和滿員情況,以及下一個前后標的位置。

一個int length可以記錄當前隊列的元素個數,和循環周期的大小比較就可以得出是否滿,檢查長度是否為0,則檢測出是否為空。

對后方和前方的下標位置有不同的應用思路:

1.前面代表隊列的頭部元素位置,代表隊列的位置;初始化rear=-1, front=0

2.前面代表隊列的頭部元素位置,代表隊列時可以代表新元素的位置:rear=0, front=0

Tricky不過,對于第一個,讀取Front()和Rear()可以直接用front和rear作為下標,對于2,讀取Rear()時,需要計算下標:(rear + q.length - 1) % q.length

解決方案

數組實現 1 - init front = 0,rear = -1

class MyCircularQueue {
    private int length;
    private int rear, front;
    private int[] q;
    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        q = new int[k];
        length = 0;
        front = 0;
        rear = -1;
    }
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        rear = (rear + 1) % (q.length);
        q[rear] = value;
        length++;
        return true;
    }
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % (q.length);
        length--;
        return true;
    }
    /** Get the front item from the queue. */
    public int Front() {
        return isEmpty() ? -1 : q[front];
    }
    /** Get the last item from the queue. */
    public int Rear() {
        return isEmpty() ? -1 : q[rear];
    }
    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        return length == 0;
    }
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        return length == q.length;
    }
}
/**
 * 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();

數組實現 2 - init front = 0,rear = 0

class MyCircularQueue {
    private int length;
    private int rear, front;
    private int[] q;
    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        q = new int[k];
        length = 0;
        front = 0;
        rear = 0;
    }
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        q[rear] = value;
        rear = (rear + 1) % (q.length);
        length++;
        return true;
    }
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % (q.length);
        length--;
        return true;
    }
    /** Get the front item from the queue. */
    public int Front() {
        return isEmpty() ? -1 : q[front];
    }
    /** Get the last item from the queue. */
    public int Rear() {
        return isEmpty() ? -1 : q[(rear + q.length - 1) % q.length];
    }
    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        return length == 0;
    }
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        return length == q.length;
    }
}
/**
 * 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();

LeetCode 官方解決方案 - 數組實現

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();

使用(雙)鏈表

class ListNode {
    int val;
    ListNode prev, next;
    public ListNode(int x) {
        val = x;
        prev = null;
        next = null;
    }
}
class MyCircularQueue {
    int queueSize, currSize;
    ListNode head, tail;
    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        queueSize = k;
        currSize = 0;
        head = new ListNode(-1);
        tail = new ListNode(-1);
        head.next = tail;
        tail.prev = head;
    }
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        ListNode newNode = new ListNode(value);
        newNode.next = tail;
        newNode.prev = tail.prev;
        tail.prev.next = newNode;
        tail.prev = newNode;
        currSize++;
        return true;
    }
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        ListNode toBeDeleted = head.next;
        head.next = toBeDeleted.next;
        toBeDeleted.next.prev = head;
        toBeDeleted.next = null;
        toBeDeleted.prev = null;
        currSize--;
        return true;
    }
    /** Get the front item from the queue. */
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return head.next.val;
    }
    /** Get the last item from the queue. */
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        return tail.prev.val;
    }
    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        return currSize == 0;
    }
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        return currSize == queueSize;
    }
}

以上就是關于“設計循環隊列詳解”的介紹,大家如果想了解更多相關知識,不妨來關注一下動力節點的Java隊列,里面有更詳細的知識等著大家去學習,希望對大家能夠有所幫助哦。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 在线免费观看黄色小视频 | 亚洲视频精品 | 国产在线天堂a v | 一区二区三区国产精品 | 国产亚洲精品在天天在线麻豆 | 88影视在线观看污污 | 欧美一区二区不卡视频 | 日本不卡中文字幕 | 亚洲一区视频在线播放 | 国产成人精品免费大全 | 国产又黄又爽又猛的免费视频播放 | 欧美日韩美女 | 中文在线观看永久免费 | 一二三四视频社区在线播放中国 | 天天干人人干 | 亚洲tube| 亚洲经典在线中文字幕 | 五月激情综合 | 亚洲欧美在线视频 | 久久er热视频在这里精品 | 久久综合99 | 人人成人免费公开视频 | 欧美精品一区二区三区免费观看 | 日本三级香港三级三级人 | 深夜福利国产 | 欧美色久 | 中文字幕小明 | 日韩综合在线视频 | 91久久澡人人爽人人添 | 欧美国产91 | 普通话中国videos | 免费黄色视屏 | 欧美wwwxxx| 国产日韩欧美另类 | 免费午夜网站 | 成人 在线欧美亚洲 | 成人午夜在线观看 | 免费99精品国产自在现线观看 | 日韩影院在线观看 | 在线亚洲欧美日韩 | 日韩免费高清视频 |