1.稀疏矩阵
2.队列
文章目录
1.稀疏数组
ublic class demo01 {
public static void main(String[] args) {
int chew[][] = new int[11][11];
chew[1][1] = 21;
chew[2][1] = 1;
for(int[] row : chew) {
for(int data : row) {
System.out.print(data+ "\t");
}
System.out.println();
}
int sum = 0;
for(int i = 0;i < chew.length;i++) {
for(int j = 0;j < chew[i].length;j++) {
if(chew[i][j] != 0) {
sum++;
}
}
}
System.out.println("sum = "+ 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 < chew.length;i++) {
for(int j = 0;j < chew[i].length;j++) {
if(chew[i][j]!=0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chew[i][j];
}
}
}
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 chew2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
for(int i = 1;i < sparseArray.length;i++) {
chew2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
for(int[] row : chew) {
for(int data : row) {
System.out.print(data+ "\t");
}
System.out.println();
}
}
}
2.队列-单向
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
ArrayQueue q = new ArrayQueue(3);
char key = ' ';
Scanner scan = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("s(show):显示队列");
System.out.println("a(add):添加队列");
System.out.println("g(get):得到数据");
System.out.println("h(head):头部数据");
System.out.println("e(exit):退出");
key = scan.next().charAt(0);
switch(key) {
case 's':
q.showQ();
break;
case 'a':
int value = scan.nextInt();
q.addQ(value);
break;
case 'g':
try {
int res = q.getq();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'h':
try {
int res = q.headQ();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'e':
scan.close();
loop = false;
break;
}
}
}
}
class ArrayQueue{
private int max;
private int front;
private int rear;
private int[] arr;//存放的数据,模拟的队列
public ArrayQueue(int maxsize) {
max = maxsize;
arr = new int[max];
front = -1;
rear = -1;
}
public boolean isFull() {
return rear == max - 1;
}
public boolean isE() {
return rear == front;
}
public void addQ(int n) {
if(isFull()) {
System.out.println("队列满");
}else {
rear++;
arr[rear] = n;
}
}
public int getq() {
if(isE()) {
throw new RuntimeException("异常");
}
front++;
return arr[front];
}
public void showQ() {
if(isE()) {
System.out.println("空");
return;
}
for(int i = 0;i < arr.length;i++) {
System.out.printf("arr[%d] = %d\n",i,arr[i]);
}
}
public int headQ() {
if(isE()) {
throw new RuntimeException("空");
}
return arr[front + 1];
}
}
单向会存在假溢出的问题。
3.队列循环
import java.util.Scanner;
public class demo03 {
public static void main(String[] args) {
ArrayQueue1 q = new ArrayQueue1(3);
char key = ' ';
Scanner scan = new Scanner(System.in);
boolean loop = true;
while(loop) {
System.out.println("s(show):显示队列");
System.out.println("a(add):添加队列");
System.out.println("g(get):得到数据");
System.out.println("h(head):头部数据");
System.out.println("e(exit):退出");
key = scan.next().charAt(0);
switch(key) {
case 's':
q.showQ();
break;
case 'a':
int value = scan.nextInt();
q.addQ(value);
break;
case 'g':
try {
int res = q.getq();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'h':
try {
int res = q.headQ();
System.out.println(res);
} catch (Exception e) {
System.out.println(e.getMessage());
// TODO: handle exception
}
break;
case 'e':
scan.close();
loop = false;
break;
}
}
}
}
class ArrayQueue1{
private int max;
private int front;
private int rear;
private int[] arr;//存放的数据,模拟的队列
public ArrayQueue1(int maxsize) {
max = maxsize;
arr = new int[max];
}
public boolean isFull() {
return (rear + 1) % max == front;
}
public boolean isE() {
return rear == front;
}
public void addQ(int n) {
if(isFull()) {
System.out.println("队列满");
return;
}
arr[rear] = n;
rear = (rear + 1)%max;
}
public int getq() {
if(isE()) {
throw new RuntimeException("异常");
}
int value = arr[front];
front = (front + 1) % max;
return value;
}
public void showQ() {
if(isE()) {
System.out.println("空");
return;
}
for(int i = front;i < front + size();i++) {
System.out.printf("arr[%d] = %d\n",i%max,arr[i%max]);
}
}
public int size() {
return (rear + max - front) % max;
}
public int headQ() {
if(isE()) {
throw new RuntimeException("空");
}
return arr[front];
}
}
注意单向与循环的区别:
循环的一些语句要带有%maxsize