利用数组实现队列--顺序队列

代码实现:

public class ArrayQueue {

    /**
     * 数组--元素
     */
    private String[] items;

    /**
     * head 表示队头下标
     */
    private int head = 0;

    /**
     * tail 表示队尾下标
     */
    private int tail = 0;

    /**
     * 队列大小
     */
    private int size = 0;

    /**
     * 初始化队列
     */
    public ArrayQueue(int size) {
        this.items = new String[size];
        this.size = size;
    }

    /**
     * 队中元素个数
     */
    public int getCount() {
        return tail - head;
    }

    /**
     * 判空
     */
    public boolean isEmpty() {
        return head == tail;
    }

   /**
     * 入队
     */
//    public boolean enqueue(String item) {
//        if (size == tail) {
//            return false;
//        }
//        items[tail] = item;
//        tail = tail + 1;
//        return true;
//    }

    // 入队操作,将 item 放入队尾
    public boolean enqueue(String item) {
        // tail == size 表示队列末尾没有空间了
        if (tail == size) {
            // tail == size && head == 0,表示整个队列都占满了
            if (head == 0) {
                return false;
            }
            // 数据搬移
            for (int i = head; i < tail; ++i) {
                items[i-head] = items[i];
            }
            // 搬移完之后重新更新 head 和 tail
            tail -= head;
            head = 0;
        }

        items[tail] = item;
        ++tail;
        return true;
    }

    /**
     * 出队
     */
    public String dequeue() {
        if (isEmpty()) {
            return null;
        }
        String ret = items[head];
        head = head + 1;
        return ret;
    }

    /**
     * 获取队头的元素
     */
    public String peekHead() {
        if (isEmpty()) {
            return null;
        }
        String front = items[head];
        return front;
    }

    /**
     * 获取对尾的元素
     */
    public String peekTail() {
        if (isEmpty()) {
            return null;
        }
        String rear = items[tail];
        return rear;
    }
}

测试用例:

public class ArrayQueueTest {

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(5);
        boolean a = arrayQueue.enqueue("1");
        boolean b = arrayQueue.enqueue("2");
        boolean c = arrayQueue.enqueue("3");
        boolean d = arrayQueue.enqueue("4");
        boolean e = arrayQueue.enqueue("5");
        boolean f = arrayQueue.enqueue("6");

        System.out.println("a=" + a
                + ",b=" + b
                + ",c=" + c
                + ",d=" + d
                + ",e=" + e
                + ",f=" + f);

        int encount = arrayQueue.getCount();
        System.out.println("encount=" + encount);

        String dequeue1 = arrayQueue.dequeue();
        String dequeue2 = arrayQueue.dequeue();
        String dequeue3 = arrayQueue.dequeue();
        String dequeue4 = arrayQueue.dequeue();
        String dequeue5 = arrayQueue.dequeue();
        String dequeue6 = arrayQueue.dequeue();
        System.out.println("dequeue1=" + dequeue1
                + ",dequeue2=" + dequeue2
                + ",dequeue3=" + dequeue3
                + ",dequeue4=" + dequeue4
                + ",dequeue5=" + dequeue5
                + ",dequeue6=" + dequeue6);

        int decount = arrayQueue.getCount();
        System.out.println("decount=" + decount);
    }
}

结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值