day18

Day18 —— Circle Queue

1. Background

今天是学习java的第十八天了,今天学习的内容是循环队列。

2. Description

2.1 Circle queue

循环队列,顾名思义就是把队列做成了一个循环。类似于把一个直的跑道做成了环形跑道。也同样是拥有headtail这两个引用。一般来说,tail中不存数据,用于判断是否队列已满。但是这里和以前的不一样,head引用指向的是第0个元素,也就是说,这个是可以存数据的。

在这里插入图片描述

2.2 enQueue

这是往Queue中增加元素的方法。还是和普通队列一样,是往尾部增加元素。

首先,找到tail节点,然后往tail指向的格子里面填元素

在这里插入图片描述

再把tail往后移一个单位,即可。

在这里插入图片描述

注意,在循环队列中,由于head和tail可以往后转一圈又一圈,所以他们的值是可以大于队列最大值TOTAL_SPACE。但是队列中的格子在内存中的地址又是确定了的,不会超过TOTAL_SPACE,故,可以让tail和TOTAL_SPACE取模,这样就不用担心超过范围的问题了。

2.3 deQueue

这是往队列里面删除元素的方法。和普通队列一样,遵循先进先出的规则,所以从head这个地方出元素。

在这里插入图片描述

然后把head++即可

在这里插入图片描述

3. Code

package datastructure;

public class CircleIntQueue {
    /**
     * The tatal space. One space can't used.
     */
    public static final int TOTAL_SPACE = 10;

    int[] data;

    /**
     * 队列的头和尾。
     */
    int head;
    int tail;

    public CircleIntQueue() {
        data = new int[TOTAL_SPACE];
        head = 0;
        tail = 0;
    } // Of the first constructure
    
    public void enQueue(int paraValue) {
        if ((tail + 1) % TOTAL_SPACE == head) {
            System.out.println("Queue full.");
            return;
        } // Of if

        data[tail % TOTAL_SPACE] = paraValue;
        tail++;
    } // Of enQueue

    public int deQueue() {
        if (tail == head) {
            System.out.println("Queuue empty.");
            return -1;
        }

        int resultValue = data[head % TOTAL_SPACE];
        head++;

        return resultValue;
    } // Of deQueue

    public String toString() {
        String resultString = "";

        if (head == tail) {
            return "empty";
        } // Of if

        for (int i = head; i < tail - 1; i++) {
            resultString += data[i % TOTAL_SPACE] + ", ";
        } // Of for i

        resultString += data[(tail - 1) % TOTAL_SPACE];
        return resultString;
    } // Of toString

    /**
     ****************
     * The entrance of program.
     * 
     * @param args Not used now.
     ****************
     */
    public static void main(String args[]) {
		CircleIntQueue tempQueue = new CircleIntQueue();
		System.out.println("Initialized, the list is: " + tempQueue.toString());

		for (int i = 0; i < 5; i++) {
			tempQueue.enQueue(i + 1);
		} // Of for i
		System.out.println("Enqueue, the queue is: " + tempQueue.toString());

		int tempValue = tempQueue.deQueue();
		System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());

		for (int i = 0; i < 6; i++) {
			tempQueue.enQueue(i + 10);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i

		for (int i = 0; i < 3; i++) {
			tempValue = tempQueue.deQueue();
			System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
		} // Of for i

		for (int i = 0; i < 6; i++) {
			tempQueue.enQueue(i + 100);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i
	}// Of main
}

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值