1.数组实现循环队列
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
CircleArray queue = 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'://展示数据
queue.showQueue();
break;
case 'a'://添加数据
System.out.println("输入一个数:");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g'://取出数据
try{
int res = queue.getQueue();
System.out.printf("取出的数据是:%d\n",res);
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
break;
case 'h'://查看队列头数据
try{
int res = queue.headQueue();
System.out.printf("队列头数据是:%d\n",res);
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出!!!");
}
}
class CircleArray {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleArray(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
}
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+1)%maxSize;
}
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列空,不能取数据");
}
int value = arr[front];
front = (front + 1)%maxSize;
return value;
}
public void showQueue(){
if (isEmpty()){
System.out.println("队列空,没有数据");
return;
}
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];
}
}
2.二维数组转稀疏数组
public class SparseArray {
public static void main(String[] args) {
int[][] chessArray = new int[11][11];
chessArray[1][2] = 1;
chessArray[2][3] = 2;
System.out.println("原始二维数组:");
for (int[] row : chessArray) {
for (int data : row) {
System.out.printf("%d\t",data);
}
System.out.println();
}
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArray[i][j]!=0){
sum++;
}
}
}
int[][] sparseArray = new int[sum+1][3];
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
int count = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArray[i][j]!=0){
count++;
sparseArray[count][0]=i;
sparseArray[count][1]=j;
sparseArray[count][2]=chessArray[i][j];
}
}
}
System.out.println();
System.out.println("得到的稀疏数组:");
for (int i = 0; i < sparseArray.length; i++) {
System.out.printf("%d\t%d\t%d\t\n",sparseArray[i][0],sparseArray[i][1],sparseArray[i][2]);
}
System.out.println();
int[][] chessArray2 = new int[sparseArray[0][0]][sparseArray[0][1]];
for (int i = 1; i < sparseArray.length; i++) {
chessArray2[sparseArray[i][0]][sparseArray[i][1]]=sparseArray[i][2];
}
System.out.println();
System.out.println("恢复后的二维数组:");
for (int[] row : chessArray2) {
for (int data : row) {
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}