日撸Java三百行 day18(循环队列)

文章详细介绍了循环队列的概念,为解决顺序队列的假溢出问题而提出。文中通过Java代码展示了循环队列的初始化、入队(enqueue)和出队(dequeue)操作,并提供了测试用例,包括整型和字符型队列的示例,以验证其功能。
摘要由CSDN通过智能技术生成

1. 循环队列及基本操作

1.1 循环队列

在顺序队列中,由于队列只能在一端插入在另一端删除的特性,会导致假溢出的情况,为了解决这个问题引入了循环队列。顺序队列的头尾指针实质上是顺序表的下标,利用整除取余就可以使头尾指针在队列范围内循环,从而产生了循环队列。但这时会产生一个问题,在队满和队空时tail与head都是指向同一位置,为了区分这里选择了牺牲一个空间的方法。

tail 等于 head                         队列为空

(tail +1) % 队长 等于 head     队列为满

循环队列初始化:

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 constructor

1.2 入队

循环队列在入队前需要判断队列是否已经满,不满则从队尾入队,尾指针循环后移一位,否则告知队满直接返回。

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

1.3 出队

循环队列在出队前需要判断队列是否为空,不为空则队头出队并赋值给临时变量,头指针循环后移一位,返回出队元素,否则告知无元素出队返回-1。

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

2. 循环队列的简单测试

2.1 整型队列

package datastructure.queue;

/**
 * Circle int queue.
 * 
 * @author Yunhua Hu yunhuahu0528@163.com.
 */
public class CircleIntQueue {
	
	/**
	 * The total space. One space can never be used.
	 */
	public static final int TOTAL_SPACE = 10;
	
	/**
	 * The data.
	 */
	int[] data;
	
	/**
	 * The index for calculating the head. The actual head is head % TOTAL_SPACE.
	 */
	int head;
	
	/**
	 * The index for calculating the tail.
	 */
	int tail;
	
	/**
	 *********************
	 * The constructor.
	 *********************
	 */
	public CircleIntQueue() {
		data = new int[TOTAL_SPACE];
		head = 0;
		tail = 0;
	}// Of the first constructor
	
	/**
	 *********************
	 * Enqueue.
	 *
	 * @param paraValue The value of the new node.
	 *********************
	 */
	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
	
	/**
	 *********************
	 * Dequeue.
	 *
	 * @return The value at the head. 
	 *********************
	 */
	public int dequeue() {
		if(head == tail) {
			System.out.println("No element in the queue.");
			return -1;
		} // Of if
		
		int resultValue = data[head % TOTAL_SPACE];
		
		head++;
		
		return resultValue;
	}// Of dequeue
	
	/**
	 *********************
	 * Overrides the method claimed n Object, the superclass of the any class.
	 *********************
	 */
	public String toString() {
		String resultString = "";
		
		if (head == tail) {
			return "empty";
		} // Of if
		
		for (int i = head; i < tail; i++) {
			resultString += data[i % TOTAL_SPACE] + ", ";
		} // Of for i
		
		return resultString;
	} // Of toString
	
	/**
	 *********************
	 * The entrance of the 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
}// Of CircleIntQueue

输出:

 2.2 字符队列

package datastructure.queue;

/**
 * Circle queue.
 * 
 * @author Yunhua Hu yunhuahu0528@163.com.
 */
public class CircleCharQueue {
	
	/**
	 * The total space. One space can never be used.
	 */
	public static final int TOTAL_SPACE = 10;
	
	/**
	 * The data.
	 */
	char[] data;
	
	/**
	 * The index for calculating the head. The actual head is head % TOTAL_SPACE.
	 */
	int head;
	
	/**
	 * The index for calculating the tail.
	 */
	int tail;
	
	/**
	 *********************
	 * The constructor.
	 *********************
	 */
	public CircleCharQueue() {
		data = new char[TOTAL_SPACE];
		head = 0;
		tail = 0;
	}// Of the first constructor
	
	/**
	 *********************
	 * Enqueue.
	 *
	 * @param paraValue The value of the new node.
	 *********************
	 */
	public void enqueue(char paraValue) {
		if((tail + 1) % TOTAL_SPACE == head) {
			System.out.println("Queue full.");
			return;
		} // Of if
		
		data[tail % TOTAL_SPACE] = paraValue;
		tail++;
	}// Of enqueue
	
	/**
	 *********************
	 * Dequeue.
	 *
	 * @return The value at the head. 
	 *********************
	 */
	public char dequeue() {
		if(head == tail) {
			System.out.println("No element in the queue.");
			return '\0';
		} // Of if
		
		char resultValue = data[head % TOTAL_SPACE];
		
		head++;
		
		return resultValue;
	}// Of dequeue
	
	/**
	 *********************
	 * Overrides the method claimed n Object, the superclass of the any class.
	 *********************
	 */
	public String toString() {
		String resultString = "";
		
		if (head == tail) {
			return "empty";
		} // Of if
		
		for (int i = head; i < tail; i++) {
			resultString += data[i % TOTAL_SPACE] + ", ";
		} // Of for i
		
		return resultString;
	} // Of toString
	
	/**
	 *********************
	 * The entrance of the program.
	 *
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		CircleCharQueue tempQueue = new CircleCharQueue();
		System.out.println("Initialized, the list is: " + tempQueue.toString());
		
		for (char i = '0'; i < '5'; i++) {
			tempQueue.enqueue(i);
		}// Of for i
		System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		
		char tempValue = tempQueue.dequeue();
		System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
		
		for (char i = 'a'; i < 'f'; i++) {
			tempQueue.enqueue(i);
			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 (char i = 'A'; i < 'F'; i++) {
			tempQueue.enqueue(i);
			System.out.println("Enqueue, the queue is: " + tempQueue.toString());
		} // Of for i
	}// Of main
}// Of CircleIntQueue

输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值