Java之循环队列实现

一.循环列表的组成

1.一个头指针,一个尾指针,一个数组存放数据

2.在Java里面我们时常要有对象概念,所以循环队列里面还具有相应的操作方法,增、删、查、改、打印等等。

二.循环队列的作用

循环不就是兜圈圈嘛?有啥作用呢?最主要的作用相对于链表而言就是空间利用率高

单队列如下图,很多空间浪费了,循环队列形成环,可以利用之前出队的空间。

 此时判断队列为空和满时,就不能简单的rear = =front,多剩一个空间

  入队: base[rear]=e;      rear=(rear+1)%M;
  出队: e=base[front];    front=(front+1)%M;   

                    队空:front == rear

                    队满:(rear+1) % M == front

代码展示:一定要细看,里面有注释,不懂品论区就问注意判断条件以及front,rear操作

入队:

	public void enqueue(int paraValue) {
		if ((rear + 1) % TOTAL_SPACE == front) {
			System.out.println("Queue full.");
			return;
		} // Of if
		data[rear % TOTAL_SPACE] = paraValue;
		rear = (rear + 1) % TOTAL_SPACE;
	}// Of enqueue

 出队:

	public int dequeue() {
		if(rear==front) {
			System.out.println("No element in the queue");
			return -1;
		}// Of if
		
		int resultValue = data[front%TOTAL_SPACE];
		front=(front+1)%TOTAL_SPACE;
		return resultValue;
	}// Of dequeue

toString重写:

	public String toString() {
		String resulString = "";
		if(rear==front) {
			return "empty";
		}
		//设置遍历标记
		int rear1=rear,front1=front;
		while(rear1!=front1) {
			resulString+=data[front1%TOTAL_SPACE]+",";
			front1=(front1+1)%TOTAL_SPACE;
		}
		return resulString;
	}

总代码:

/**
 * 
 */
package datastructure.queue;

import javax.naming.spi.DirStateFactory.Result;

/**
	 ****************************
 	 * TODO
 	 * @author Chen Fan
 	 * @version 1.0
 	 * time 2021年12月26日
 	 ****************************
 	 */
	
public class CircleIntQueue {
	public static final int TOTAL_SPACE = 10;
	int[] data;
	int front;
	int rear;
	
	//构造函数,构造循环队列
	public CircleIntQueue() {
		data = new int[TOTAL_SPACE];
		front = 0;
		rear = 0;
	}// Of construct
	
	//入队操作
	public void enqueue(int paraValue) {
		if ((rear + 1) % TOTAL_SPACE == front) {
			System.out.println("Queue full.");
			return;
		} // Of if
		data[rear % TOTAL_SPACE] = paraValue;
		rear = (rear + 1) % TOTAL_SPACE;
	}// Of enqueue
	
	// 出队
	public int dequeue() {
		if(rear==front) {
			System.out.println("No element in the queue");
			return -1;
		}// Of if
		
		int resultValue = data[front%TOTAL_SPACE];
		front=(front+1)%TOTAL_SPACE;
		return resultValue;
	}// Of dequeue
	
	//重写函数
	public String toString() {
		String resulString = "";
		if(rear==front) {
			return "empty";
		}
		//设置遍历标记
		int rear1=rear,front1=front;
		while(rear1!=front1) {
			resulString+=data[front1%TOTAL_SPACE]+",";
			front1=(front1+1)%TOTAL_SPACE;
		}
		return resulString;
	}
	
	//测试函数
	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
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值