尚硅谷图解Java数据结构和算法.pdf尚硅谷Java数据结构和算法【最新版】.pptx
图解.xlsx
1、稀疏数组
基本功能
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
处理方法
- 记录数组一共有几行几列,有多少个不同的值
- 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

如图,把一个6X7的二维数组变为了一个9X3的稀疏数组。其中
- 第一行保存的是原二维数组的行、列以及非0值的个数
-
转换思路
二维数组转稀疏数组
遍历二维数组,得到二维数组中有效值的个数sum
- 创建稀疏数组,有sum+1行,3列(固定)
- 将二维数组中的有效值存入稀疏数组中
稀疏数组转二维数组
- 先读取稀疏数组的第一行(保存二维数组的行列信息),还原二维数组
- 读取稀疏数组的其他行,将值赋给二维数组的对应位置上的数
代码
public class Demo2 {public static void main(String[] args) {//创建一个二维数组int[][] arr1 = new int[11][11];//向二维数组里放值arr1[1][2] = 1;arr1[2][3] = 2;arr1[3][4] = 3;//打印二维数组System.out.println("遍历二维数组");for (int i = 0; i < arr1.length; i++) {for (int j = 0; j < arr1[0].length; j++) {System.out.print(arr1[i][j] + " ");}System.out.println();}//二位数组----->稀疏数组//遍历二维数组中有效值的个数,用sum来记录int sum = 0;for (int i = 0; i < arr1.length; i++) {for (int j = 0; j < arr1[0].length; j++) {if (arr1[i][j] != 0) {//二维数组中元素不为0即为有效值sum++;}}}//创建稀疏数组//行数为sum+1,第一行用于保存二维数组的行列及有效值个数,列数固定为3int[][] sparseArr = new int[sum + 1][3];//存入二维数组的行列及有效值个数sparseArr[0][0] = arr1.length;sparseArr[0][1] = arr1[0].length;sparseArr[0][2] = sum;//再次遍历二维数组,将有效值存入稀疏数组//用于保存稀疏数组的行数int count = 1;for (int i = 0; i < arr1.length; i++) {for (int j = 0; j < arr1[i].length; j++) {if (arr1[i][j] != 0) {//将值存入稀疏数组sparseArr[count][0] = i;sparseArr[count][1] = j;sparseArr[count][2] = arr1[i][j];count++;}}}//打印稀疏数组System.out.println("遍历稀疏数组");for (int i = 0; i < sparseArr.length; i++) {for (int j = 0; j < sparseArr[0].length; j++) {System.out.print(sparseArr[i][j] + " ");}System.out.println();}//稀疏数组------>二维数组//先得到二位数组的行列数int row = sparseArr[0][0];int col = sparseArr[0][1];int[][] arr2 = new int[row][col];//遍历稀疏数组,同时给二维数组赋值for (int i = 1; i < sparseArr.length; i++) {row = sparseArr[i][0];col = sparseArr[i][1];//该位置上对应的值int val = sparseArr[i][2];arr2[row][col] = val;}//打印二维数组System.out.println("遍历还原后的二维数组");for (int i = 0; i < arr2.length; i++) {for (int j = 0; j < arr2[0].length; j++) {System.out.print(arr2[i][j] + " ");}System.out.println();}}}运行结果遍历二维数组0 0 0 0 0 0 0 0 0 0 00 0 1 0 0 0 0 0 0 0 00 0 0 2 0 0 0 0 0 0 00 0 0 0 3 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0遍历稀疏数组11 11 31 2 12 3 23 4 3遍历还原后的二维数组0 0 0 0 0 0 0 0 0 0 00 0 1 0 0 0 0 0 0 0 00 0 0 2 0 0 0 0 0 0 00 0 0 0 3 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0
2、队列
定义
- 队列是一个有序列表,可以用数组或是链表来实现。
遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
模拟思路
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量
因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front及 rear分别记录队列前后端的下标,front 会随着数据输出而改变,而 rear则是随着数据输入而改变,如图所示
入队出队操作模拟
当我们将数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:
将尾指针往后移:rear+1 , 当 front == rear 时,队列为空
- 若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear所指的数组元素中,否则无法存入数据。rear == maxSize - 1时,队列满
注意:front指向的是队列首元素的前一个位置
实现代码
package com.atguigu.queue;import java.util.Scanner;public class ArrayQueueDemo {public static void main(String[] args) {//测试一把//创建一个队列ArrayQueue queue = new ArrayQueue(3);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 (Exception e) {// TODO: handle exceptionSystem.out.println(e.getMessage());}break;case 'h': //查看队列头的数据try {int res = queue.headQueue();System.out.printf("队列头的数据是%d\n", res);} catch (Exception e) {// TODO: handle exceptionSystem.out.println(e.getMessage());}break;case 'e': //退出scanner.close();loop = false;break;default:break;}}System.out.println("程序退出~~");}}// 使用数组模拟队列-编写一个ArrayQueue类class ArrayQueue {private int maxSize; // 表示数组的最大容量private int front; // 队列头private int rear; // 队列尾private int[] arr; // 该数据用于存放数据, 模拟队列// 创建队列的构造器public ArrayQueue(int arrMaxSize) {maxSize = arrMaxSize;arr = new int[maxSize];front = -1; // 指向队列头部,分析出front是指向队列头的前一个位置.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()) {// 通过抛出异常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];}}
环形队列
思路:
- front变量指向队首元素,初值为0
- rear变量指向队尾元素的下一个元素,初值为0。规定空出一个位置
- 队列为空的判定条件:front == rear
- 队列为满的判定条件:(rear + 1) % maxSize == front
- 队列中有效元素的个数:(rear - front + maxSize) % maxSize
- 入队和出队时,都需要让标记对maxSize取模
代码
package com.atguigu.queue;import java.util.Scanner;public class CircleArrayQueueDemo {public static void main(String[] args) {//测试一把System.out.println("测试数组模拟环形队列的案例~~~");// 创建一个环形队列CircleArray queue = new CircleArray(4); //说明设置4, 其队列的有效数据最大是3char 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 (Exception e) {// TODO: handle exceptionSystem.out.println(e.getMessage());}break;case 'h': // 查看队列头的数据try {int res = queue.headQueue();System.out.printf("队列头的数据是%d\n", res);} catch (Exception e) {// TODO: handle exceptionSystem.out.println(e.getMessage());}break;case 'e': // 退出scanner.close();loop = false;break;default:break;}}System.out.println("程序退出~~");}}class CircleArray {private int maxSize; // 表示数组的最大容量//front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素//front 的初始值 = 0private int front;//rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.//rear 的初始值 = 0private int rear; // 队列尾private int[] arr; // 该数据用于存放数据, 模拟队列public CircleArray(int arrMaxSize) {maxSize = arrMaxSize;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 = (rear + 1) % maxSize;}// 获取队列的数据, 出队列public int getQueue() {// 判断队列是否空if (isEmpty()) {// 通过抛出异常throw new RuntimeException("队列空,不能取数据");}// 这里需要分析出 front是指向队列的第一个元素// 1. 先把 front 对应的值保留到一个临时变量// 2. 将 front 后移, 考虑取模// 3. 将临时保存的变量返回int value = arr[front];front = (front + 1) % maxSize;return value;}// 显示队列的所有数据public void showQueue() {// 遍历if (isEmpty()) {System.out.println("队列空的,没有数据~~");return;}// 思路:从front开始遍历,遍历多少个元素// 动脑筋for (int i = front; i < front + size() ; i++) {System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);}}// 求出当前队列有效数据的个数public int size() {// rear = 2// front = 1// maxSize = 3return (rear + maxSize - front) % maxSize;}// 显示队列的头数据, 注意不是取出数据public int headQueue() {// 判断if (isEmpty()) {throw new RuntimeException("队列空的,没有数据~~");}return arr[front];}}

