大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) hot資訊 設(shè)計(jì)循環(huán)隊(duì)列詳解

設(shè)計(jì)循環(huán)隊(duì)列詳解

更新時(shí)間:2022-06-06 09:51:17 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1245次

設(shè)計(jì)循環(huán)隊(duì)列的實(shí)現(xiàn)。循環(huán)隊(duì)列是一種線性數(shù)據(jù)結(jié)構(gòu),其操作基于FIFO(先進(jìn)先出)原則,最后一個(gè)位置與第一個(gè)位置連接形成一個(gè)圓圈。它也被稱為“環(huán)形緩沖區(qū)”。

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

您的實(shí)現(xiàn)應(yīng)支持以下操作:

MyCircularQueue(k):構(gòu)造函數(shù),設(shè)置隊(duì)列大小為k。

Front:從隊(duì)列中獲取最前面的項(xiàng)目。如果隊(duì)列為空,則返回 -1。

Rear:從隊(duì)列中獲取最后一項(xiàng)。如果隊(duì)列為空,則返回 -1。

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

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

isEmpty():檢查循環(huán)隊(duì)列是否為空。

isFull():檢查循環(huán)隊(duì)列是否已滿。

例子:

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

分析

數(shù)組排序?qū)崿F(xiàn):

重點(diǎn)是確定循環(huán)的空位和滿員情況,以及下一個(gè)前后標(biāo)的位置。

一個(gè)int length可以記錄當(dāng)前隊(duì)列的元素個(gè)數(shù),和循環(huán)周期的大小比較就可以得出是否滿,檢查長(zhǎng)度是否為0,則檢測(cè)出是否為空。

對(duì)后方和前方的下標(biāo)位置有不同的應(yīng)用思路:

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

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

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

解決方案

數(shù)組實(shí)現(xiàn) 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();

數(shù)組實(shí)現(xiàn) 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 官方解決方案 - 數(shù)組實(shí)現(xiàn)

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;
    }
}

以上就是關(guān)于“設(shè)計(jì)循環(huán)隊(duì)列詳解”的介紹,大家如果想了解更多相關(guān)知識(shí),不妨來關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java隊(duì)列,里面有更詳細(xì)的知識(shí)等著大家去學(xué)習(xí),希望對(duì)大家能夠有所幫助哦。

提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-10-10 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 日本在线观看中文字幕 | 欧美日韩一区在线观看 | 久免费视频| 真实偷清晰对白在线视频 | 国产午夜亚洲精品久久www | 亚洲自拍成人 | 99在线热播精品免费 | 正在播放亚洲一区 | 午夜免费看 | 男女羞羞网站 | 99伊人| 欧美福利视频在线 | 婷婷亚洲五月 | 久久亚洲福利 | 久久免费视频在线观看30 | 久久视精品 | 亚洲一区二区三区四区五区 | 色综合综合网 | 在线播放国产精品 | 另类亚洲图片 | 国产亚洲在线观看 | 亚洲国产www | a大片久久爱一级 | 96精品视频在线播放免费观看 | 国产在热线精品视频国产一二 | 日日夜夜网站 | 精品国产日韩亚洲一区在线 | 九九热精品视频 | 99久久99久久 | 久青草国产高清在线视频 | 在线亚洲欧美日韩 | 亚洲精品美女一区二区三区乱码 | 久草新视频 | 日韩小视频 | 久久亚洲精品tv | 奇米影视4色 | 欧美一区中文字幕 | 久久99精品国产99久久 | 日韩高清一区二区三区不卡 | 奇米色第四色 | 天堂成人精品视频在线观 |