使用java数组模拟队列

一、普通队列

class ArrayQueue{
	private int maxNums;
	private int front; //指向当前队列的头元素的
	private int rear;	//指向当前队列的最后一个元素
	private int[] arr; //存储队列

	//创建队列对象
	public ArrayQueue(int maxNums) {
		this.maxNums = maxNums;
		this.front = 0;
		this.rear = 0;
		this.arr = new int[maxNums];
	}
	
	//判断队列是否为空
	public boolean isEmpty() {
		return this.front == this.rear;
	}
	
	//判断队列是否已满
	public boolean isFull() {
		return this.rear == this.maxNums;
	}
	
	//将数据加入到队列中
	public void addData(int n) {
		if (isFull()) {
			System.out.println("队列已经满了");
			return;
		}
		this.arr[rear++] = n;
	}
	
	//取出队列的数据
	public int getData() {
		if (isEmpty()) {
			throw new RuntimeException("队列为空");
		}
		return this.arr[front++];
	}
	
	//遍历
	public void findAll() {
		if (isEmpty()) {
			System.out.println("队列为空");
			return ;
		}
		for(int i=this.front; i<this.rear; i++) {
			System.out.print(this.arr[i] + " ");
		}
		System.out.print("\n");
	}

	public static void main(String[] args) {
		ArrayQueue queue = new ArrayQueue(10);
		Scanner scanner = new Scanner(System.in);
		boolean loop = true;
		char ch = ' ';
		while(loop) {
			System.out.println("1:添加");
			System.out.println("2:是否为空");
			System.out.println("3:是否已满");
			System.out.println("4:遍历");
			System.out.println("5:取出");
			//接受一个字符
			ch = scanner.next().charAt(0);
			if (ch == '1') {
				int val = scanner.nextInt();
				queue.addData(val);
			} else if (ch == '2') {
				System.out.println(queue.isEmpty());
			} else if (ch == '3') {
				System.out.println(queue.isFull());
			} else if (ch == '4') {
				queue.findAll();
			} else if (ch == '5') {
				System.out.println(queue.getData());
			} else {
				break;
			}
		}
		System.out.println("程序结束");
	}
}

二、环形队列

class CircleQueue{
	private int maxNums;
	private int front; //指向当前队列的头元素的
	private int rear;	//指向当前队列的最后一个元素的后一个元素,这里设置了一个预留空间
	private int[] arr; //存储队列

	//创建队列对象
	public CircleQueue(int maxNums) {
		this.maxNums = maxNums;
		this.front = 0;
		this.rear = 0;
		this.arr = new int[maxNums];
	}
	
	//判断队列是否为空
	public boolean isEmpty() {
		return this.front == this.rear;
	}
	
	//判断队列是否已满
	public boolean isFull() {
		return (this.rear+1)%this.maxNums == this.front;
	}
	
	//将数据加入到队列中
	public void addData(int n) {
		if (isFull()) {
			System.out.println("队列已经满了");
			return;
		}
		this.arr[rear] = n;
		this.rear = (this.rear+1)%maxNums;
	}
	
	//取出队列的数据
	public int getData() {
		if (isEmpty()) {
			throw new RuntimeException("队列为空");
		}
		int val = this.arr[this.front];
		this.front = (this.front+1)%maxNums;
		return val;
	}
	
	//遍历
	public void findAll() {
		if (isEmpty()) {
			System.out.println("队列为空");
			return ;
		}
		for(int i=this.front; i<this.front + size(); i++) {
			System.out.print(this.arr[i%maxNums] + " ");
		}
		System.out.print("\n");
	}
	
	//获取所有的数据的个数
	public int size() {
		return (this.rear+this.maxNums-this.front)%maxNums;
	}

	public static void main(String[] args) {
		CircleQueue queue = new CircleQueue(10);//实际的队列个数只有9个,因为队尾的指针预留了一个
		Scanner scanner = new Scanner(System.in);
		boolean loop = true;
		char ch = ' ';
		while(loop) {
			System.out.println("1:添加");
			System.out.println("2:是否为空");
			System.out.println("3:是否已满");
			System.out.println("4:遍历");
			System.out.println("5:取出");
			//接受一个字符
			ch = scanner.next().charAt(0);
			if (ch == '1') {
				int val = scanner.nextInt();
				queue.addData(val);
			} else if (ch == '2') {
				System.out.println(queue.isEmpty());
			} else if (ch == '3') {
				System.out.println(queue.isFull());
			} else if (ch == '4') {
				queue.findAll();
			} else if (ch == '5') {
				System.out.println(queue.getData());
			} else {
				break;
			}
		}
		System.out.println("程序结束");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值