Java数据结构练习-队列操作

队列操作

现在是一次性队列,改进版以后发

package SJJG;

import java.util.Scanner;

public class ArrayQueueDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayQueue queue = new ArrayQueue(3);			//创建队列queu,包含5个数据
		char key = ' ';		//接收用户输入数据
		Scanner scanner = new Scanner(System.in);		//定义输入
		boolean loop = true;		//设置循环条件
		
		while(loop) {			//做一个简单菜单
			
			System.out.println("输入——s(show)——以显示队列\n");
			System.out.println("输入——a(add)——以往队列中填充数据(入队)\n");
			System.out.println("输入——o(out)——以导出队列中数据(出队)\n");
			System.out.println("输入——l(list)——以显示队列首位置数据\n");
			System.out.println("输入——e(exit)——以退出队列\n");
			
			key = scanner.next().charAt(0);
			switch(key){
				
				case's':	//展示队列数据
					queue.showArrayQueue();
					break;
					
				case'a':
					System.out.println("填入一个数据\n");
					int value = scanner.nextInt();		//接收输入数据
					queue.addArrayQueue(value);			
					break;
					
				case'o':
					try {
						int res = queue.outArrayQueue();
						System.out.printf("取出是数据为%d\n",res);
					}catch(Exception e){		//抛出捕获的错误并定义错误变量e
						System.out.println(e.getMessage());		//显示错误信息
					}
					break;
				
				case'l':
					try {
						int res = queue.listheadArrayQueue();
						System.out.printf("展示的队首元素为%d\n", res);
					}catch(Exception e) {
						System.out.printf(e.getMessage());
					}
					break;
					
				case'e':
					scanner.close();		//关闭输入
					loop = false;			//结束循环
					break;
				default:					//结束switch语句
					break;
							
			}
		}
		
		System.out.println("程序退出~~~");//主函数结束时!!
				
	}

}


/*模块一:
 * 创建数组队列类,定义数组及操作*/

class ArrayQueue{
	private int maxSize;	//最大数组容量
	private int front;		//队列头
	private int rear;		//队列尾
	private int []arr;		//原始队列
	
	
	/*创建队列*/
	
	public ArrayQueue(int arrmaxSize) {		//传入形参
		maxSize = arrmaxSize;
		arr = new int[maxSize];		//创建数组
		front = -1;
		rear = -1;
		
	}
	
	
	/*模块二:
	 * 判断队列是否已满
	 * */
	public boolean isFull() {
		return rear == maxSize-1;
	}
	
	
	/*模块三:
	 * 判断队列是否为空
	 * */
	public boolean isEmpty() {
		return front == rear;
	}
	
	/*模块三:
	 * 添加数据进队列
	 * */
	public void addArrayQueue(int n) {
		if(isFull()) {
			System.out.println("队列已经满员,不能入队列~~~~~~");
			return;
		}
			rear++; 	//队列上部向上移动
			arr[rear]=n;
		}
	
	/*模块四:
	 * 拿出数据,出队列
	 * */
	
	public int outArrayQueue() {	//出队列,取出数据,设int型
		if(isEmpty()) {
			throw new RuntimeException("当前队列为空,无法取出数据");	//若队列无数据则抛出异常
		}
		front++;
		return arr[front];
	}
	
	/*模块五:
	 * 展示队列内数据(不取)
	 * */
	
	public void showArrayQueue() {
		if(isEmpty()) {		//若为空队列,抛出异常信息
			System.out.println("队列为空,无数据\n");
			return;
		}
		for(int i=front+1;i<arr.length;i++) {
			//front++;
			System.out.printf("arr[%d] = %d\n",i,arr[i]);
		}
	}
	
	
	/*模块五:
	 * 显示队列头元素(不取)
	 * */
	public int listheadArrayQueue() {
		if(isEmpty()) {
			throw new RuntimeException("当前队列为空,无法显示头数据\n");	//若队列无数据则抛出异常
		}
		return arr[front+1];
	}
	
}
	
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值