首页 > 其他 > 详细

队列——循环队列

时间:2019-03-07 12:57:35      阅读:134      评论:0      收藏:0      [点我收藏+]

这次看下循环队列,也是使用数组实现,但是避免了数据的搬动。

如有不对请提出,共同提高,谢谢!!!

public class CircularQueue {

    private String[] items;
    private int n = 0;
    private int head = 0;
    private int tail = 0;

    public CircularQueue(int capacity) {
        items = new String[capacity];
        n = capacity;
    }

    public boolean enqueue(String item) {
        if ((tail + 1) % n == head) return false;
        items[tail] = item;
        tail = (tail + 1) % n;
        return true;
    }

    public String dequeue() {
        if (head == tail) return null;
        String ret = items[head];
        head = (head + 1) % n;
        return ret;
    }

}

  

队列——循环队列

原文:https://www.cnblogs.com/shenqiaqia/p/10488817.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!