- 順序隊列實現
//使用數組模擬隊列
class ArrayQue{
//表示數組最大容量
private int maxSize;
//隊列頭
private int front;
//隊列尾
private int rear;
//該數組用於存放數據
private int[] arr;
//創建隊列的構造器
public ArrayQue(int arrMaxSize){
maxSize = arrMaxSize;
arr = new int[maxSize];
//指向隊列頭部,分析出front指向隊列頭的前一個位置
front = -1;
//指向隊列尾,指向隊列尾的數據(就是隊列最後一個數據
rear = -1;
}
//判斷隊列是否滿
public boolean isFull(){
return rear == maxSize -1;
}
//判斷隊列是否爲空
public boolean isEmpty(){
return rear == front;
}
//添加數據到隊列
public void addQueue(int n){
//判斷隊列是否滿
if (isFull()){
System.out.println("隊列滿,不能加入數據");
return;
}
rear ++;//讓rear 後移
arr[rear] = n;
}
//數據出隊列
public int getQueue(){
//判斷隊列是否爲空
if (isEmpty()){
System.out.println("隊列爲空,沒有數據");
throw new RuntimeException("隊列空,沒有數據");
}
front ++;//讓front後移
return arr[front];
}
//顯示隊列所有數據
public void showQueue(){
//遍歷
if (isEmpty()){
System.out.println("隊列空,沒有數據");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n",i,arr[i] );
}
}
//顯示隊列的頭數據,不是取出數據
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("隊列空,沒有數據");
}
return arr[front + 1];
}
}
順序隊列的缺點是:只能前進,不能後退,一次數據加載完後,不能再重複利用
- 環形隊列
//使用數組模擬環形隊列
// 1. rear默認值爲0, 指向最後一個元素的後一個位置
// 2. front默認值爲0,指向隊列的第一個元素
class CircleArray{
//表示數組最大容量
private int maxSize;
//隊列頭
private int front;
//隊列尾
private int rear;
//該數組用於存放數據
private int[] arr;
//創建隊列的構造器
public CircleArray(int arrMaxSize){
maxSize = arrMaxSize;
arr = new int[maxSize];
//指向隊列頭部,分析出front指向隊列頭的前一個位置
front = 0;
//指向隊列尾,指向隊列尾的數據(就是隊列最後一個數據
rear = 0;
}
//判斷隊列是否滿
public boolean isFull(){
return (rear + 1) % maxSize == front;
}
//判斷隊列是否爲空
public boolean isEmpty(){
return rear == front;
}
//添加數據到隊列
public void addQueue(int n){
//判斷隊列是否滿
if (isFull()){
System.out.println("隊列滿,不能加入數據");
return;
}
//直接將數據加入就可以
arr[rear] = n;
//將rear 後移,這裏要考慮取模
rear = (rear + 1) % maxSize;
}
//數據出隊列
public int getQueue(){
//判斷隊列是否爲空
if (isEmpty()){
System.out.println("隊列爲空,沒有數據");
throw new RuntimeException("隊列空,沒有數據");
}
//front 是指向隊列的第一個元素
// 1. 先把front對應的值保存到臨時變量
// 2. 將front 後移
int res = arr[front];
front = (front + 1) % maxSize;
return res;
}
//顯示隊列所有數據
public void showQueue(){
//遍歷
if (isEmpty()){
System.out.println("隊列空,沒有數據");
return;
}
//從front 開始遍歷,遍歷多少個元素 也就是判斷數組中到底有多少個元素
int size = size();
for (int i = front; i < front + size; i++) {
System.out.printf("arr[%d]=%d\n",i % maxSize,arr[(i % maxSize)]);
}
}
//求出當前隊列有效數據個數
public int size(){
return (rear + maxSize - front) % maxSize;
}
//顯示隊列的頭數據,不是取出數據
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("隊列空,沒有數據");
}
return arr[front];
}
}
測試:
public class ArrayQueueCircle {
public static void main(String[] args) {
//創建一個隊列
ArrayQue arrayQue = new ArrayQue(3);
//創建一個環形隊列
CircleArray arrayCir = new CircleArray(4);
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("g(get):從隊列取出數據");
System.out.println("h(head):查看隊列頭的數據");
key = scanner.next().charAt(0);
switch (key){
case 's':
arrayQue.showQueue();
break;
case 'a':
System.out.println("請輸入一個數");
int value = scanner.nextInt();
arrayQue.addQueue(value);
break;
case 'g':
try {
int res = arrayQue.getQueue();
System.out.println("取出的數據是:"+res);
} catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = arrayQue.headQueue();
System.out.println("隊列頭的數據是:"+res);
} catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
loop = false;
scanner.close();
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
參考:尚硅谷Java數據結構