数据结构与算法 循环队列的数组实现

Java数据结构和算法
上一篇主目录 下一篇
package queue;

import java.util.Scanner;


public class CircleArrayQueue {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//测试一把
		System.out.println("测试数组模拟环形队列的案例~~~");
		
		// 创建一个环形队列
		CircleArrayQueueClass queue = new CircleArrayQueueClass(5); //说明设置4, 其队列的有效数据最大是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("o(out): 从队列取出数据");
			System.out.println("g(get): 查看队列头的数据");
			key = scanner.next().charAt(0);// 接收一个字符
			switch (key) {
			case 's':
				queue.showQueue();
				break;
			case 'a':
				System.out.println("输出一个数");
				int value = scanner.nextInt();
				queue.addQueue(value);
				break;
			case 'o': // 取出数据
				try {
					int res = queue.outQueue();
					System.out.printf("取出的数据是%d\n", res);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				break;
			case 'g': // 查看队列头的数据
				try {
					int res = queue.getQueue();
					System.out.printf("队列头的数据是%d\n", res);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				break;
			case 'e': // 退出
				scanner.close();
				loop = false;
				break;
			default:
				break;
			}
		}
		System.out.println("程序退出~~");

	}

}

class CircleArrayQueueClass{
	private int[] arr;
	private int front;// front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素 
	private int rear;//rear 变量的含义:rear 指向队列的最后一个元素的后一个位置.
	private int MAXSIZE;
	
	//构造函数
	public CircleArrayQueueClass(int MAXSIZE) {
		this.MAXSIZE=MAXSIZE;
		arr=new int[MAXSIZE];
		front=0;
		rear=0;//初始值设置为0
	}
	// 判断队列是否满
	public boolean isFull() {
		return (rear+1)%MAXSIZE==front;
	}
		
	// 判断队列是否为空
	public boolean isNull() {
		return rear==front;
	}
		
	// 添加数据到队列
	public void addQueue(int x) {
		if(isFull()) {
			throw new RuntimeException("full");
		}
		arr[rear++]=x;
		System.out.println("add sucess");
	}
		
	// 获取队列的数据, 出队列
	public int outQueue() {
		if(isNull()) {
			throw new RuntimeException("null");
		}
		return arr[front++];
	}
		
	// 显示队列的所有数据
	public void showQueue() {
		if(isNull()) {
			System.out.println("null");
		}else {
			if(front>rear) {
				for(int i=front;(i+1)%MAXSIZE<=rear;) {
					System.out.printf("%d\t",arr[i]);
					i=(i+1)%MAXSIZE;
				}
			}else if(front<rear) {
				for(int i=front;i<rear;i++) {
					System.out.printf("%d\t",arr[i]);
				}
			}
			
		}
		System.out.println();
	}
		
	// 求出当前队列有效数据的个数
	public int numQueue() {
		return (rear-front+MAXSIZE)%MAXSIZE;
	}
		
	// 显示队列的头数据, 注意不是取出数据
	public int getQueue() {
		return arr[front];
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值