美文网首页程序员
力扣 622 设计循环队列

力扣 622 设计循环队列

作者: zhaojinhui | 来源:发表于2020-11-11 00:34 被阅读0次

题意:构建循环队列

思路:用一个双向node 构建循环队列

思想:双向链表

复杂度:时间O(1),空间O(n)

class Node {
    Node pre = null;
    Node next = null;
    int val;
    public Node(int val) {
        this.val = val;
    }
}

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

相关文章

  • 力扣 622 设计循环队列

    题意:构建循环队列 思路:用一个双向node 构建循环队列 思想:双向链表 复杂度:时间O(1),空间O(n)

  • LeetCode 622. 设计循环队列

    622. 设计循环队列 设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原...

  • leetcode链表之设计循环队列

    622、设计循环队列[https://leetcode-cn.com/problems/design-circul...

  • leetcode641.设计循环双端队列

    题目链接 题解: 类似的题目为:leetcode622题设计循环队列,622题的 题解 先附上。本题和622题是一...

  • 622 设计循环队列

    题目描述: 设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被...

  • LeetCode 622——设计循环队列

    1. 题目 设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被...

  • [Leetcode 622]设计循环队列

    设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之...

  • leetcode 622 循环队列设计

    要求:设计实现循环队列循环队列定义:队尾指向队首;构造:设置一个长度为k的循环队列;要求的操作:取队首队尾/插入/...

  • Leetcode622. 设计循环队列

    题目 设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在...

  • leetcode622.设计循环队列

    题目链接 题解: 在我的文章数据结构之——队列与循环队列 中,有关于循环队列的设计,包括本题没有考虑过的resiz...

网友评论

    本文标题:力扣 622 设计循环队列

    本文链接:https://www.haomeiwen.com/subject/slokbktx.html