环形数组实现队列(Java)

package com.heiyu.queue;

import java.util.Scanner;

// 使用数组模拟队列,编写circleQueue类

class circleQueue{
	private int maxSize; //数组最大容量
	private int front; //队列头,对应队列开头位置,初始值为0
	private int rear; //队列尾,对应队列结尾后一位,初始值为0
	private int[] arr; //该数组用于存放数据,模拟队列
	
	//创建队列的构造器
	public circleQueue(int maxSize) {
		this.maxSize = maxSize;
		arr = new int[maxSize];
	}
	//判断队列是否满
	public boolean isFull() {
		return front == (rear+1)%maxSize;
	}
	//判断队列是否为空
	public boolean isEmpty() {
		return front == rear;
	}
	//添加数据到队列
	public void addQueue(int n) {
		if(isFull()) {
			System.out.println("队列已满,无法添加数据");
			return;
		}else {
			//添加数据
			arr[rear] = n;
			//rear后移,考虑取余
			rear = (rear+1)% maxSize;
		}
	}
	//数据出队列
	public int getQueue() {
		//先判断队列是否空
		if(isEmpty()) {
			//丢出异常,无法取出数据
			throw new RuntimeException("队列为空,不能取数据");
		}else {
			//front指向队列第一个元素
			//1.先把front对应的值保存到一个临时变量
			//2.front后移
			//3.返回临时变量
			int value = arr[front];
			front = (front+1) % maxSize;
			return value;
		}
	}
	//显示队列所有数据
	public void showQueue() {
		if(isEmpty()) {
			System.out.println("队列是空的,没有数据");
			return;
		}else {
			//环形遍历
			//有效元素个数(rear-front+maxSize) % maxSize
			for (int i = front; i<front+size(); i++) {
				System.out.printf("arr[%d]=%d\n",i % maxSize,arr[i % maxSize]);
			}
		}
	}
	// 求出当前队列有效数据的个数
	public int size() {
		return (rear+maxSize-front) % maxSize;
	}
	//显示队列的头部数据(!不是取数据!)
	public int headQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列是空的,没有数据");
		}else {
			return arr[front];
		}
	}
}

public class circleArrayDemo {

	public static void main(String[] args) {
		// 测试
		// 创建队列
		circleQueue queue1 = new circleQueue(5);//设置为5,其实队列最大数据为3
		char key = ' '; //接收用户输入
		Scanner scanner = new Scanner(System.in);
		boolean loop = true;
		// 输入一个菜单
		while(loop) {
			System.out.println("s(show):显示队列");
			System.out.println("e(exit):退出程序");
			System.out.println("a(add):添加数据到队列");
			System.out.println("g(get):从队列中取出数据");
			System.out.println("h(head):查看队列头的数据");
			key = scanner.next().charAt(0);//接收一个字符
			switch(key) {
			case 's':
				queue1.showQueue();break;
			case 'a':
				System.out.println("请输入一个数");
				int value = scanner.nextInt();
				queue1.addQueue(value);break;
			case 'g':
				// 可能抛出异常,使用try
				try {
					int res = queue1.getQueue();
					System.out.printf("取出的数据为%d\n",res);
				}catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'h'://查看队列头
				try {
					int res = queue1.headQueue();
					System.out.printf("队列头数据为%d\n",res);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'e'://退出
				scanner.close();
				System.out.println("程序退出");
				loop = false;
			default:
				break;
			}
		}

	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值