溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

05-環(huán)形隊列

發(fā)布時間:2020-07-19 21:59:57 來源:網(wǎng)絡 閱讀:393 作者:qq58ca2992a1bde 欄目:編程語言

實現(xiàn)思路

1,調(diào)整front指向隊列的第一個元素,front初始值=0

2,調(diào)整rear指向隊列的最后一個元素的后一個位置,希望空出一個空間作為約定,rear的初始值=0

3,隊滿,條件: (rear+1) % maxSize = front ,則隊滿,隊列最多可存?maxSize-1個數(shù)

4,隊空,條件:rear == front空

5,隊列中有效的數(shù)據(jù)的個數(shù) (rear-front+maxSize) % maxSize


代碼實現(xiàn)

package?com.datastack.datastack.queue;

import?java.util.Scanner;

/*
?*?環(huán)形隊列(數(shù)組實現(xiàn))
?*/
public?class?CircleQueue?{
	private?int?maxSize;//隊列最大值
	private?int?front;//隊首,指向隊列首的前一個位置
	private?int?rear;//隊尾,指向隊列尾的序號
	private?int[]?arr;//存放隊列數(shù)據(jù)的數(shù)組
	
	/**
	?*?創(chuàng)建隊列
	?*?@param?maxSize
	?*/
	public?CircleQueue(int?maxSize){
		this.maxSize?=?maxSize;
		this.arr?=?new?int[maxSize];
		this.front?=?0;
		this.rear?=?0;
	}
	
	/**
	?*?判斷隊列是否已滿
	?*?@return
	?*/
	public?boolean?isFull(){
		return?(rear+1)?%?maxSize?==?front;
	}
	
	/**
	?*?判斷隊列是否為空
	?*?@param?args
	?*/
	public?boolean?isEmpty(){
		return?rear?==?front;
	}
	
	/**
	?*?添加數(shù)據(jù)到隊列
	?*?@param?args
	?*/
	public?void?addQueue(int?n){
		//判斷隊列是否滿
		if(isFull()){
			System.out.println("隊列已滿,不能加入數(shù)據(jù)。");
			return;
		}
		//直接將數(shù)據(jù)加入
		arr[rear]?=?n;
		//將rear后移,需考慮取模
		rear?=?(rear+1)?%?maxSize;
	}
	
	/**
	?*?出隊列
	?*?@param?args
	?*/
	public?int?getQueue(){
		//判斷隊列是否為空
		if(isEmpty()){
			//通過拋出異常
			throw?new?RuntimeException("隊列空,不能取數(shù)據(jù)");
		}
		//需要分析front是指向隊列的第一個元素
		//1,先把front的值保存到變量
		//2,將front后移,去摸
		int?res?=?this.arr[front];
		front?=?(front+1)?%?maxSize;
		return?res;
	}
	
	/**
	?*?顯示隊列數(shù)據(jù)
	?*?@param?args
	?*/
	public?void?showQueque(){
		if(isEmpty()){
			System.out.println("隊列為空。");
			return;
		}
		//從front開始遍歷
		for(int?i=front;i<front+size();i++){
			System.out.printf("arr[%d]=%d\t",i?%?maxSize,arr[i?%?maxSize]);
		}
	}
	
	/**
	?*?求出當前隊列有效個數(shù)
	?*/
	public?int?size(){
		return?(rear-front+maxSize)?%?maxSize;
	}
	
	/**
	?*?顯示隊頭
	?*?@param?args
	?*/
	public?int?headQueue(){
		if(isEmpty()){
			throw?new?RuntimeException("隊列為空。");
		}
		return?this.arr[front];
	}
	
	public?static?void?main(String[]?args)?{
		//創(chuàng)建一個隊列
		CircleQueue?arrQueue?=?new?CircleQueue(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):添加數(shù)據(jù)到隊列");
			System.out.println("g(get):從隊列取出數(shù)據(jù)");
			System.out.println("h(head):查看隊列頭的數(shù)據(jù)");
			key?=?scanner.next().charAt(0);
			switch?(key)?{
			case?'s'://顯示隊列值
				arrQueue.showQueque();
				break;
			case?'a'://入隊
				System.out.println("請輸入一個數(shù)");
				int?value?=?scanner.nextInt();
				arrQueue.addQueue(value);
				break;
			case?'g'://出隊
				try?{
					int?res?=?arrQueue.getQueue();
					System.out.println(res);
				}?catch?(Exception?e)?{
					System.out.println(e.getMessage());
				}
				break;
			case?'h'://打印對首
				try?{
					int?res?=?arrQueue.headQueue();
					System.out.println(res);
				}?catch?(Exception?e)?{
					System.out.println(e.getMessage());
				}
				break;
			case?'e'://退出程序
				scanner.close();
				loop?=?false;
				break;

			default:
				break;
			}
		}
		System.out.println("程序退出");
		
	}
}



向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI