首页 > 编程语言 > 详细

队列——数组队列

时间:2019-03-04 13:23:55      阅读:159      评论:0      收藏:0      [点我收藏+]

队列是一种先进先出,支持队尾插入,在对头删除元素。

如有不会的地方请指出,共同提高,谢谢!!!

如下图:

技术分享图片

下面就看下代码吧:

package com.shenqi.dataStructure.queue;

/**
 * @Author:shenqi
 * @Description: 数组队列
 * @Date:2019/1/23 12:14
 */
public class ArrayQueue {

    private int size;
    private int count;
    private Object[] array;
    private int next;

    public ArrayQueue(int size) {
        this.size = size;
        array = new Object[size];
    }

    /**
     * 入队
     *
     * @param obj
     * @return
     */
    public boolean enqueue(Object obj) {
        if (size == count) {
            if (next == 0) return false;
            // 数据搬动
            for (int i = next; i < count; i++) {
                array[i - next] = array[i];
            }
            count -= next;
            next = 0;
        }
        array[count] = obj;
        count++;
        return true;
    }

    /**
     * 出队
     *
     * @return
     */
    public Object dequeue() {
        if (count == 0) return null;
        if (next == size) return null;
        Object obj = array[next];
        next++;
        return obj;
    }
}

  

队列——数组队列

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

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