循环队列解决空数组不能添加数据的问题

public class CircleArrayQueueDemo {

	public static void main(String[] args) {
		System.out.println("测试模拟环形队列的案例~");
		//有效个数3个,因为有一个空位
		CircleArrayQueue queue = new CircleArrayQueue(4);
		char key = ' ';
		Scanner scanner = new Scanner(System.in);
		boolean loop = true;
		while(loop){
			System.out.println("s:显示队列");
			System.out.println("e:退出程序");
			System.out.println("a:添加数据到队列");
			System.out.println("g:从队列取出数据");
			System.out.println("h:查看队列头的数据");
			key = scanner.next().charAt(0);
			switch (key) {
			case 's':
				queue.showQueue();
				break;
			case 'a':
				System.out.println("请输入一个数:");
				int num = scanner.nextInt();
				queue.addQueue(num);
				break;
			case 'g':
				try {
					int result = queue.getQueue();
					System.out.printf("取出的数据是%d\n",result);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'h':
				try {
					int result = queue.headQueue();
					System.out.printf("队列头的数据是%d\n",result);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'e':
				scanner.close();
				loop = false;
				break;
			default:
				break;
			}
		}
		System.out.println("程序退出");
	}
}
class CircleArrayQueue{
	//队列的最大容量
	private int maxSize;
	//front变量的含义做一个调整:front就指向队列的第一个元素,
	//也就是说arr[front]是队列的第一个元素
	//front的初始值是0
	private int front;
	//rear变量的含义做一个调整:rear就指向队列的最后一个元素的后一个位置,
	//因为希望空出一个位置做为约定
	//rear的初始值是0
	private int rear;
	//该数组用于存放数据,模拟队列
	private int[] arr;
	//初始化队列数据
	public CircleArrayQueue(int maxSize){
		this.maxSize = maxSize;
		this.arr = new int[maxSize];
		this.front = 0;
		this.rear = 0;
	}
	//判断队列是否满
	public boolean isFull(){
		return (this.rear + 1) % this.maxSize == this.front;
	}
	//判断队列是否为空
	public boolean isEmpty(){
		return this.rear == this.front;
	}
	//添加数据到队列
	public void addQueue(int num){
		if(isFull()){
			System.out.println("队列已满,请稍后再添加。");
			return;
		}
		this.arr[this.rear] = num;
		//rear后移,这里必须考虑取模,因为比如说已经到最后一个位置,
		//直接+1复制的话,那么就越界了,所以需要对maxSize取模
		this.rear = (this.rear + 1) % this.maxSize;
	}
	//获取队列数据,出队列
	public int getQueue(){
		if(isEmpty()){
			throw new RuntimeException("队列为空,不能取数据。");
//			System.out.println("队列为空。");
//			return -1;
		}
		//这里需要分析出:front是指向队列的第一个元素
		//1、先把front对应的值保留到一个临时变量;
		//2、将front后移;
		//3、将临时保存的变量返回
		int value = this.arr[this.front];
		this.front = (this.front + 1) % this.maxSize;
		return value;
	}
	//显示队列的所有数据
	public void showQueue(){
		if(isEmpty()){
			System.out.println("队列为空。");
			return;
		}
		for (int i = this.front; i < this.front + size(); i++) {
			System.out.printf("arr[%d]=%d\n", i % this.maxSize,arr[i % this.maxSize]);
		}
	}
	
	//求出当前队列的有效数据个数
	public int size(){
		//rear = 2
		//front = 1
		//maxSize = 3
		return (this.rear + this.maxSize - this.front) % this.maxSize;
	}
	
	//显示队列的头数据,不是取数据
	public int headQueue(){
		if(isEmpty()){
			throw new RuntimeException("队列为空。");
		}
		return this.arr[this.front];
	}
}
posted @ 2021-08-23 16:09  卡卡罗特琪琪  阅读(107)  评论(0编辑  收藏  举报