02java演算法與資料結構------稀疏陣列程式碼實現
阿新 • • 發佈:2021-11-13
package com.java.test;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
//佇列用陣列來實現
Scanner sc = new Scanner(System.in);
System.out.println("請你輸入佇列的最大值");
int maxSize=sc.nextInt();
int[] arr = new int[maxSize];
//佇列的尾;
int rear=-1;
//佇列的頭;
int front=-1;
//佇列用陣列來實現 ---給陣列新增值
while (true){
System.out.println("請你新增陣列的值");
int count=sc.nextInt();
rear++;
arr[rear]=count;
if (rear == maxSize-1) {
System.out.println( "請你新增陣列的值已經滿了");
break;
}
}
//佇列用陣列來實現 ---給陣列新增值-----遍歷
for (int i = 0; i <arr.length ; i++) {
if (front == rear) {
System.out.println("該陣列為空");
break;
}
System.out.println(arr[i]);
}
}
}