美文网首页
循环队列创建笔记

循环队列创建笔记

作者: landlord_ | 来源:发表于2019-01-07 19:04 被阅读0次

在leetCode上学习数据结构时发现circulation Queue。从头理下思路,在此记录。

有时候我们在开发中可能会遇到一个恒定长度得队列,但队列中的数据确可能存在变化。(可以简单理解为一个定长、按时间顺序缓存的队列)

1、Create Circulation Queue Step:

能判断是否为空,或者存满,在未满时可以添加,未空时可以删除

step1:创建一个数组,在此创建: int[ ] data,定义队首字段:head,队尾字段:tail;

step2:初始化队列长度K,data = new int[ k ],head=-1,tail=-1;

step3:创建判断是否已满、是否为空的方法:isFull(),isEmpty(),

step4:增加元素方法:增加从队尾增加,value为需要添加到队列参数

           ① 判断是否已满,如果已满,则return false

           ②判断是否为空,如果为空,head增加一个数据后应head = 0;

                tail = (tail+1)%size

                data[tail] = value; return true;

step5: 减少元素方法,从队首减少,

                若为空,则return false;

               若不为空,正常删除,

                当head = tail时,则表示已删完,重置:head=-1.tail=-1;return true;

                  从队首删除,则head = (head+1)%size,return true;

以下为代码:

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;

    }

}

相关文章

  • 循环队列创建笔记

    在leetCode上学习数据结构时发现circulation Queue。从头理下思路,在此记录。 有时候我们在开...

  • 学习js数据结构与算法2—队列

    队列遵循先进先出原则——如同排队 4.1 创建队列 4.2 优先队列 4.3 循环队列——击鼓传花

  • 二叉树的层序遍历

    思路 创建两个队列A、B,先讲根节点放入队列A中,然后循环遍历队列A,并将A中的节点出队到队列B中,最终队列B中的...

  • 队列

    队列特性 对比队列和栈 基于数组的队列 对比队列学习循环队列 循环队列难点 阻塞队列 并发队列 应用:线程池中拒绝...

  • golang 源码剖析(5): 并发调度

    概述 基本流程 用go func() 创建G 放入P本地队列,或平衡到全局队列 唤醒或新建M执行任务 进入调度循环...

  • C++ 数据结构与算法 栈,队列,链式队列,循环队列

    栈 队列 链式队列 4 . 循环队列

  • iOS GCD

    任务和队列的创建方法 / 获取方法 队列的创建方法 / 获取方法 串行队列的创建方法 并发队列的创建方法。并发队列...

  • 有关“队列”的总结

    队列 定义 分类 链式队列 (用链表实现) 静态队列 (用数组实现)图静态队列通常都必须是循环队列循环队列的讲解:...

  • 数据结构入门——大师:queue(二) LoopQueue

    1.什么是循环队列 由于队列会出队入队,因此我们需要利用好队列出队的空间,因此我们需要设置循环队列 2.循环队列的...

  • iOS多线程编程理解-GCD

    创建队列 创建串行队列 创建并行队列 获取全局队列 系统提供dispatch方法 创建异步任务,开启新线程,不阻塞...

网友评论

      本文标题:循环队列创建笔记

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