欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java應用數(shù)組實現(xiàn)環(huán)形隊列的方法-創(chuàng)新互聯(lián)

思路分析:

成都創(chuàng)新互聯(lián)是專業(yè)的龍州網(wǎng)站建設公司,龍州接單;提供成都做網(wǎng)站、網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行龍州網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

java應用數(shù)組實現(xiàn)環(huán)形隊列的方法

1.  front 變量的含義做一個調整: front 就指向隊列的第一個元素, 也就是說 arr[front] 就是隊列的第一個元素

front 的初始值 = 0

2.  rear 變量的含義做一個調整:rear 指向隊列的最后一個元素的后一個位置. 因為希望空出一個空間做為約定

rear 的初始值 = 0

3. 當隊列滿時,條件是  (rear  + 1) % maxSize == front 【滿】

4.隊列為空的條件, rear == front 空

5. 當我們這樣分析, 隊列中有效的數(shù)據(jù)的個數(shù)   (rear + maxSize - front) % maxSize   // rear = 1 front = 0

6. 我們就可以在原來的隊列上修改得到,一個環(huán)形隊列

代碼實現(xiàn):

import java.util.Scanner;
public class CircleArrayQueueDemo {
	public static void main(String[] args) {
		//測試一把
		System.out.println("測試數(shù)組模擬環(huán)形隊列的案例~~~");
		// 創(chuàng)建一個環(huán)形隊列
		CircleArray queue = new CircleArray(4); //說明設置4, 其隊列的有效數(shù)據(jù)大是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':
				queue.showQueue();
				break;
			case 'a':
				System.out.println("輸出一個數(shù)");
				int value = scanner.nextInt();
				queue.addQueue(value);
				break;
			case 'g': // 取出數(shù)據(jù)
				try {
					int res = queue.getQueue();
					System.out.printf("取出的數(shù)據(jù)是%d\n", res);
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				break;
			case 'h': // 查看隊列頭的數(shù)據(jù)
				try {
					int res = queue.headQueue();
					System.out.printf("隊列頭的數(shù)據(jù)是%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 CircleArray {
	private int maxSize; // 表示數(shù)組的大容量
	//front 變量的含義做一個調整: front 就指向隊列的第一個元素, 也就是說 arr[front] 就是隊列的第一個元素 
	//front 的初始值 = 0
	private int front; 
	//rear 變量的含義做一個調整:rear 指向隊列的最后一個元素的后一個位置. 因為希望空出一個空間做為約定.
	//rear 的初始值 = 0
	private int rear; // 隊列尾
	private int[] arr; // 該數(shù)據(jù)用于存放數(shù)據(jù), 模擬隊列
	public CircleArray(int arrMaxSize) {
		maxSize = arrMaxSize;
		arr = new int[maxSize];
	}
	// 判斷隊列是否滿
	public boolean isFull() {
		return (rear  + 1) % maxSize == front;
	}
	// 判斷隊列是否為空
	public boolean isEmpty() {
		return rear == front;
	}
	// 添加數(shù)據(jù)到隊列
	public void addQueue(int n) {
		// 判斷隊列是否滿
		if (isFull()) {
			System.out.println("隊列滿,不能加入數(shù)據(jù)~");
			return;
		}
		//直接將數(shù)據(jù)加入
		arr[rear] = n;
		//將 rear 后移, 這里必須考慮取模
		rear = (rear + 1) % maxSize;
	}
	// 獲取隊列的數(shù)據(jù), 出隊列
	public int getQueue() {
		// 判斷隊列是否空
		if (isEmpty()) {
			// 通過拋出異常
			throw new RuntimeException("隊列空,不能取數(shù)據(jù)");
		}
		// 這里需要分析出 front是指向隊列的第一個元素
		// 1. 先把 front 對應的值保留到一個臨時變量
		// 2. 將 front 后移, 考慮取模
		// 3. 將臨時保存的變量返回
		int value = arr[front];
		front = (front + 1) % maxSize;
		return value;
	}
	// 顯示隊列的所有數(shù)據(jù)
	public void showQueue() {
		// 遍歷
		if (isEmpty()) {
			System.out.println("隊列空的,沒有數(shù)據(jù)~~");
			return;
		}
		// 思路:從front開始遍歷,遍歷多少個元素
		for (int i = front; i < front + size() ; i++) {
			System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
		}
	}
	// 求出當前隊列有效數(shù)據(jù)的個數(shù)
	public int size() {
		// rear = 2
		// front = 1
		// maxSize = 3 
		return (rear + maxSize - front) % maxSize;   
	}
	// 顯示隊列的頭數(shù)據(jù), 注意不是取出數(shù)據(jù)
	public int headQueue() {
		// 判斷
		if (isEmpty()) {
			throw new RuntimeException("隊列空的,沒有數(shù)據(jù)~~");
		}
		return arr[front];
	}
}

以上就是java中使用數(shù)組實現(xiàn)環(huán)形隊列的詳細內容,更多請關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司其它相關文章!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

新聞標題:java應用數(shù)組實現(xiàn)環(huán)形隊列的方法-創(chuàng)新互聯(lián)
轉載注明:http://chinadenli.net/article24/cdpjce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內鏈、定制網(wǎng)站、網(wǎng)站改版、網(wǎng)站設計公司、網(wǎng)站建設、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設