隊(duì)列:有序列表,可以用數(shù)組和鏈表實(shí)現(xiàn),先進(jìn)先出原則,

尾部加數(shù)據(jù)(要判斷隊(duì)列是否滿),隊(duì)首取數(shù)據(jù)
1.front含義,指向隊(duì)首數(shù)據(jù)的前一個(gè)位置;
front的初始值=-1
2.rear含義,指向隊(duì)尾數(shù)據(jù)位置;
rear初始值=-1
3.隊(duì)列滿是rear == maxSize-1
4.隊(duì)列空是front==rear
5.有效個(gè)數(shù)是rear-front
代碼實(shí)現(xiàn):
1.隊(duì)列類public class ArrayQueue {// 使用數(shù)組模擬隊(duì)列
private int maxSize;//數(shù)組的大容量
private int front;//隊(duì)列頭
private int rear;//隊(duì)列尾
private int[] arr;//存放數(shù)據(jù),模擬隊(duì)列
public ArrayQueue(int arrMaxSize) {this.maxSize=arrMaxSize;
arr = new int[maxSize];
front = -1;//指向隊(duì)列頭的前一個(gè)位置
rear = -1;//指向隊(duì)列尾的位置
}
// 判斷隊(duì)列是否滿
public boolean isFull(){return rear==maxSize-1;
}
// 判斷隊(duì)列是否為空
public boolean isEmety(){return rear==front;
}
// 添加數(shù)據(jù)到隊(duì)列中
public void addQueue(int ele){if (!isFull()){arr[++rear]=ele;
return;
}else {System.out.println("隊(duì)列已滿");
}
}
// 取出隊(duì)列的數(shù)據(jù)
public int getQueue(){if (isEmety()){throw new RuntimeException("沒有數(shù)據(jù),無法取出");
}
return arr[++front];
}
// 獲取隊(duì)首數(shù)據(jù),不取出
public int headQueue(){if (isEmety()){throw new RuntimeException("沒有數(shù)據(jù),無法取出");
}
return arr[front+1];
}
// 顯示隊(duì)列所有數(shù)據(jù)
public void allQueue(){if (isEmety()){System.out.println("沒有數(shù)據(jù),無法取出");
return;
}
for (int i = front+1; i< rear+1; i++) {System.out.println(arr[i]);
}
}
}2.測(cè)試隊(duì)列public class ArrayQueueDemo {public static void main(String[] args) {ArrayQueue arrayQueue = new ArrayQueue(3);
boolean flag = true;
Scanner sc = new Scanner(System.in);
while (flag){System.out.println("s(show):顯示隊(duì)列中的全部數(shù)據(jù)");
System.out.println("h(head):獲取隊(duì)列中隊(duì)首數(shù)據(jù)");
System.out.println("a(add):向隊(duì)列添加一個(gè)數(shù)據(jù)");
System.out.println("g(get):取出隊(duì)列中的一個(gè)元素");
System.out.println("e(exit):退出程序");
System.out.print("請(qǐng)輸入操作:");
char c = sc.next().charAt(0);
switch (c){case 's':
arrayQueue.allQueue();
break;
case 'h':
try {int i = arrayQueue.headQueue();
System.out.println("隊(duì)首的數(shù)據(jù)是:"+i);
} catch (Exception e) {System.out.println(e.getMessage());
}
break;
case 'a':
System.out.print("請(qǐng)輸入要添加的數(shù)據(jù):");
int add = sc.nextInt();
arrayQueue.addQueue(add);
break;
case 'g':
try {int get = arrayQueue.getQueue();
System.out.println("取出的數(shù)據(jù)是:"+get);
} catch (Exception e) {System.out.println(e.getMessage());
}
break;
case 'e':
sc.close();
flag = false;
break;
default:
System.out.println("輸入錯(cuò)誤");
break;
}
}
System.out.println("程序退出!");
}
}
3.問題分析:(環(huán)形隊(duì)列可復(fù)用)1.目前數(shù)組只能使用一次,沒有達(dá)到復(fù)用的效果,將這個(gè)數(shù)組用一個(gè)算法,改進(jìn)為環(huán)形隊(duì)列(取模);
1.解決:使用數(shù)組模擬環(huán)形隊(duì)列1.front含義改變,由指向隊(duì)首數(shù)據(jù)的前一個(gè)位置,變成隊(duì)首數(shù)據(jù)位置
front的初始值=0
2.rear含義改變,由指向隊(duì)尾數(shù)據(jù)位置,變成隊(duì)尾數(shù)據(jù)的后一個(gè)位置
rear初始值=0
空出一個(gè)空間作為約定;
3.隊(duì)列滿是由rear == maxSize-1變成(rear+1)%maxSize==front;
4.隊(duì)列空不改變,還是front==rear
5.有效個(gè)數(shù)是(rear+max-front)%max
public class CircleArrayQueue {private int[] arr;
private int front;
private int rear;
private int maxSize;
public CircleArrayQueue(int maxSize) {this.maxSize = maxSize;
arr = new int[maxSize];
front = 0;
rear = 0;
}
public boolean isFull(){return (rear+1)%maxSize==front;
}
public boolean isEmepty(){return rear==front;
}
//隊(duì)列添加元素
public void addQueue(int ele){if (isFull()){System.out.println("隊(duì)列滿了,無法添加元素");
return;
}
arr[rear]=ele;
rear=(rear+1)%maxSize;
}
//取出隊(duì)首元素
public int getQueue(){if (isEmepty()){throw new RuntimeException("隊(duì)列沒有元素,無法取出數(shù)據(jù)");
}
int a = arr[front];
arr[front]=0;
front = (front+1) % maxSize;
return a;
}
//顯示隊(duì)首元素
public int headQueue(){if (isEmepty()){throw new RuntimeException("隊(duì)列沒有元素,無法取出數(shù)據(jù)");
}
return arr[front];
}
//顯示隊(duì)列所有數(shù)據(jù)
public void allQueue(){if (isEmepty()){System.out.println("沒有數(shù)據(jù)");
return;
}
int count = size();
for (int i = front; i< front+count; i++) {System.out.println(arr[i%maxSize]);
}
}
//有效數(shù)據(jù)個(gè)數(shù)
public int size(){return (rear+maxSize-front)%maxSize;
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
名稱欄目:隊(duì)列數(shù)組實(shí)現(xiàn)-環(huán)形隊(duì)列-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://www.chinadenli.net/article8/iieop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、企業(yè)建站、云服務(wù)器、網(wǎng)站制作、網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容