数组模拟环形队列

队列是一个有序列表,可以用数组或是链表来实现。
遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取

思路加下:

  1. front变量的含义: front 就指向队列的第一个元素也就是说arr[front]就是队列的第一个元素
    front的初始值=0
  2. rear 变量的含义做一个调整: rear 指向队列的最后一个元素的后一个位置因为希望空出一个空间做为约定。
    rear的初始值=0
    3.当队列满时,条件是(rear +1) %maxsize=front [满]
    4.对队列为空的条件,rear== front空
    5.当我们这样分析,队列中有效的数据的个数(rear+ maxSize- front)% maxSize //rear=1 front=o
    代码如下
package com.queue;

import java.util.Scanner;

public class ArrayQueue {
	public static void main(String[] args) {
	 //环形队列
		Array array = new Array(3);//队列有效数组仅有两个 一个作为空间约定
		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':
				array.showQueue();
				break;
			case 'a':
				System.out.println("输出一个数");
				int value = scanner.nextInt();
				array.addQueue(value);
				break;
			case 'g':
				try {
					int res = array.getQueue();
					System.out.printf("取出的数据是%d\n",res);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'h':
				try {
					int res = array.headQueue();
					System.out.println("队列头的数据为"+res);
	
				} catch (Exception e) {
					System.out.println(e.getMessage());
}
				break;
			case 'e':
				scanner.close();
				loop = false;
				break;
				
			}
		}
		System.out.println("程序退出");
 }
}
class Array{
	private int maxSizes;//数组的最大容量
	private int front;//队列头 指向队列第一个元素初始值为0
	private int rear;//队列尾 指向队列最后一个元素的后一个元素 (要空出一个元素位置)初始值为0
	private int[] arr;//改数据用于存放数据 模拟队列
	//创建队列的构造器
	public Array(int arrayMaxSize) {
		maxSizes = arrayMaxSize;
		arr = new int[maxSizes];
		front =0;//指向队列头部的前一个位置
		rear = 0;//指向队列尾部的数据(队列最后一个数据)
	}
	//判断队列是否满
	public boolean ifFull() {
		return (rear +1)%maxSizes == front ;
	}
	//判断队列是否为空
	public boolean ifEmpty() {
		return rear == front;
	}
	//添加数据到队列
	public void addQueue(int n) {
		if(ifFull()) {
			System.out.println("队列满");
			return;
		}
		arr[rear] = n;
		rear = ( rear + 1 ) % maxSizes;
	}
	//获取队列的数据 出队列
	public int getQueue(){
		if(ifEmpty()){
			//通过抛出异常
			throw new RuntimeException("队列空不能取值");		
		}
		//这里需要分析出front就是指向队列的第一个元素
		//1.先把front对应的值保留到一个临时变量
		//2.将front后移 考虑取模
		//3.将临时保存的值返回
		int value = arr[front];
		front = (front + 1) % maxSizes;
		return value;
	}
	//显示队列所有数据
	public void showQueue() {
		if(ifEmpty()) {
			System.out.println("空队列无数组");
			return;			
		}
		for(int i = front;i< front + (rear + maxSizes - front)% maxSizes;i++) {
			System.out.printf("arr[%d]=%d\n",i%maxSizes,arr[i%maxSizes]);
			
		}
	}
	//显示队列的头数据 注意不是取出数据
	public int headQueue() {
		if(ifEmpty()){
			//通过抛出异常
			throw new RuntimeException("队列空 无数据");		
		}
		return arr[front];
	}
	
}


你可能感兴趣的:(java,数据结构)