隊列特點:先進先出
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、網(wǎng)站建設(shè)與策劃設(shè)計,峨邊彝族網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:峨邊彝族等地區(qū)。峨邊彝族做網(wǎng)站價格咨詢:13518219792
隊列可以使用數(shù)組或鏈表來實現(xiàn)
數(shù)組模擬隊列
隊列本身是有序列表,若使用數(shù)組的結(jié)構(gòu)來存儲隊列的數(shù)據(jù),則隊列數(shù)組的聲明如下,其中maxSize是該隊列的最大容量。
因為隊列的輸出、輸入是分別從前后端來處理,因此需要兩個變量front及rear分別記錄隊列前后端的下標,front會隨著數(shù)據(jù)輸出而改變,rear會隨著數(shù)據(jù)的輸入而改變。
使用數(shù)組實現(xiàn)隊列
front ?默認-1,隊首
rear 默認-1,隊尾
maxSize 隊列的最大容量
加數(shù)據(jù),在隊尾加,rear+1
取數(shù)據(jù),在隊首取,front+1
添加到隊列,分析
1,將rear+1,尾指針后移(當rear==front時,隊列為空,可存入;非空時,當rear=maxSize-1時,隊列已滿,無法存入)
代碼實現(xiàn):
package?com.datastack.datastack.queue; import?java.util.Scanner; /* ?*?隊列(數(shù)組實現(xiàn)) ?*/ public?class?ArrQueue?{ private?int?maxSize;//隊列最大值 private?int?front;//隊首,指向隊列首的前一個位置 private?int?rear;//隊尾,指向隊列尾的序號 private?int[]?arr;//存放隊列數(shù)據(jù)的數(shù)組 /** ?*?創(chuàng)建隊列 ?*?@param?maxSize ?*/ public?ArrQueue(int?maxSize){ this.maxSize?=?maxSize; this.arr?=?new?int[maxSize]; this.front?=?-1; this.rear?=?-1; } /** ?*?判斷隊列是否已滿 ?*?@return ?*/ public?boolean?isFull(){ return?rear?==?maxSize?-?1; } /** ?*?判斷隊列是否為空 ?*?@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; } rear++; arr[rear]?=?n; } /** ?*?出隊列 ?*?@param?args ?*/ public?int?getQueue(){ //判斷隊列是否為空 if(isEmpty()){ //通過拋出異常 throw?new?RuntimeException("隊列空,不能取數(shù)據(jù)"); } front++; return?arr[front]; } /** ?*?顯示隊列數(shù)據(jù) ?*?@param?args ?*/ public?void?showQueque(){ if(isEmpty()){ System.out.println("隊列為空。"); return; } for(int?i=0;i<arr.length;i++){ System.out.printf("arr[%d]=%d\t",i,arr[i]); } } /** ?*?顯示隊頭 ?*?@param?args ?*/ public?int?headQueue(){ if(isEmpty()){ throw?new?RuntimeException("隊列為空。"); } return?this.arr[front+1]; } public?static?void?main(String[]?args)?{ //創(chuàng)建一個隊列 ArrQueue?arrQueue?=?new?ArrQueue(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("程序退出"); } }
本文名稱:04-隊列實現(xiàn)思路
網(wǎng)頁路徑:http://muchs.cn/article22/isjejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、域名注冊、網(wǎng)站改版、搜索引擎優(yōu)化、網(wǎng)站收錄、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)