美文网首页数据结构与算法
队列--设计循环双端队列

队列--设计循环双端队列

作者: 暮想sun | 来源:发表于2020-01-08 14:24 被阅读0次

设计实现双端队列。
你的实现需要支持以下操作:
MyCircularDeque(k):构造函数,双端队列的大小为k。
insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true。
insertLast():将一个元素添加到双端队列尾部。如果操作成功返回 true。
deleteFront():从双端队列头部删除一个元素。 如果操作成功返回 true。
deleteLast():从双端队列尾部删除一个元素。如果操作成功返回 true。
getFront():从双端队列头部获得一个元素。如果双端队列为空,返回 -1。
getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回 -1。
isEmpty():检查双端队列是否为空。
isFull():检查双端队列是否满了。
思路:循环队列实现方式。使用数组现有数据量大小来判断队满和队空


    //数据
    private int[] elementData;

    //数组数据量
    private int size;

    //队尾指针
    private int rear;

    //对头指针
    private int front;

    /**
     * Initialize your data structure here. Set the size of the deque to be k.
     */
    public MyCircularDeque(int k) {
        elementData = new int[k];
        size = rear = front = 0;
    }

    /**
     * Adds an item at the front of Deque. Return true if the operation is successful.
     */
    public boolean insertFront(int value) {
        if (isFull()) {
            return false;
        }

        //插入front-1处,front向前移动
        front = (front - 1 + elementData.length) % elementData.length;
        elementData[front] = value;
        size ++;
        return true;
    }

    /**
     * Adds an item at the rear of Deque. Return true if the operation is successful.
     */
    public boolean insertLast(int value) {
        if (isFull()) {
            return false;
        }

        //数据存储在队尾指针
        elementData[rear] = value;

        //队尾指针位置
        rear = (rear + 1) % elementData.length;

        size ++;
        return true;
    }

    /**
     * Deletes an item from the front of Deque. Return true if the operation is successful.
     */
    public boolean deleteFront() {
        if (isEmpty()) {
            return false;
        }

        //对头指针向前移动
        front = (front + 1) % elementData.length;
        size--;
        return true;
    }

    /**
     * Deletes an item from the rear of Deque. Return true if the operation is successful.
     */
    public boolean deleteLast() {
        if (isEmpty()) {
            return false;
        }

        rear = (rear - 1 + elementData.length) % elementData.length;
        size--;
        return true;
    }

    /**
     * Get the front item from the deque.
     */
    public int getFront() {
        if (isEmpty()) {
            return -1;
        }
        return elementData[front];
    }

    /**
     * Get the last item from the deque.
     */
    public int getRear() {
        if (isEmpty()) {
            return -1;
        }
        return elementData[(rear - 1 + elementData.length) % elementData.length];

    }

    /**
     * Checks whether the circular deque is empty or not.
     */
    public boolean isEmpty() {
        //队空判断
        return size == 0;

    }

    /**
     * Checks whether the circular deque is full or not.
     */
    public boolean isFull() {
        //堆满判断
        return size == elementData.length;
    }

相关文章

  • 数据结构-队列(Queue)-FIFO

    数据结构-队列(Queue)-FIFO 队列的接口设计 双端队列-Deque 循环队列-CircleQueue 双...

  • 数据结构与算法之队列(五)

    目录 队列简介队列的接口设计用栈实现队列双端队列实现循环队列实现循环双端队列 一 简介 队列是一种特殊的线性表,只...

  • ARTS第八周20200712

    Algorithm 设计循环双端队列 设计实现双端队列。 你的实现需要支持以下操作:MyCircularDeque...

  • 队列 - 双端队列 - 循环队列 - 循环双端队列

    队列是一种特殊的线性表,只能在头尾两端进行操作队尾(rear):只能从队尾添加元素,一般叫做 enQueue,入队...

  • 队列--设计循环双端队列

    设计实现双端队列。你的实现需要支持以下操作:MyCircularDeque(k):构造函数,双端队列的大小为k。i...

  • 7.双端队列Deque

    目录:1.双端队列的定义2.双端队列的图解3.双端队列定义操作4.双端队列的实现 1.双端队列的定义 2.双端队列...

  • LeetCode:641. 设计循环双端队列(中等)

    问题链接 641. 设计循环双端队列[https://leetcode-cn.com/problems/desig...

  • 数据结构 - 循环队列

    单向循环队列 可以使用数组来实现队列,并且各项接口也可以优化到 O(1) 的时间复杂度--这就是循环队列 循环双端...

  • LeetCode - 641. 设计循环双端队列 swift &

    设计实现双端队列。你的实现需要支持以下操作: MyCircularDeque(k):构造函数,双端队列的大小为k。...

  • 双端队列

    双端队列 双端队列是与队列类似的项的有序集合。双端队列有两个端部,首部和尾部,并且项在集合中保持不变。双端队不同的...

网友评论

    本文标题:队列--设计循环双端队列

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