尚硅谷图解Java数据结构和算法.pdf尚硅谷Java数据结构和算法【最新版】.pptx
图解.xlsx

1、稀疏数组

基本功能

当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。

处理方法

  • 记录数组一共有几行几列,有多少个不同的值
  • 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

一、稀疏数组和队列 - 图1
如图,把一个6X7的二维数组变为了一个9X3的稀疏数组。其中

  • 第一行保存的是原二维数组的行、列以及非0值的个数
  • 第二到九行保存的是每个非0值所在的位置及其数值

    转换思路

    二维数组转稀疏数组

  • 遍历二维数组,得到二维数组中有效值的个数sum

  • 创建稀疏数组,有sum+1行,3列(固定)
  • 将二维数组中的有效值存入稀疏数组中

稀疏数组转二维数组

  • 先读取稀疏数组的第一行(保存二维数组的行列信息),还原二维数组
  • 读取稀疏数组的其他行,将值赋给二维数组的对应位置上的数

代码

  1. public class Demo2 {
  2. public static void main(String[] args) {
  3. //创建一个二维数组
  4. int[][] arr1 = new int[11][11];
  5. //向二维数组里放值
  6. arr1[1][2] = 1;
  7. arr1[2][3] = 2;
  8. arr1[3][4] = 3;
  9. //打印二维数组
  10. System.out.println("遍历二维数组");
  11. for (int i = 0; i < arr1.length; i++) {
  12. for (int j = 0; j < arr1[0].length; j++) {
  13. System.out.print(arr1[i][j] + " ");
  14. }
  15. System.out.println();
  16. }
  17. //二位数组----->稀疏数组
  18. //遍历二维数组中有效值的个数,用sum来记录
  19. int sum = 0;
  20. for (int i = 0; i < arr1.length; i++) {
  21. for (int j = 0; j < arr1[0].length; j++) {
  22. if (arr1[i][j] != 0) {
  23. //二维数组中元素不为0即为有效值
  24. sum++;
  25. }
  26. }
  27. }
  28. //创建稀疏数组
  29. //行数为sum+1,第一行用于保存二维数组的行列及有效值个数,列数固定为3
  30. int[][] sparseArr = new int[sum + 1][3];
  31. //存入二维数组的行列及有效值个数
  32. sparseArr[0][0] = arr1.length;
  33. sparseArr[0][1] = arr1[0].length;
  34. sparseArr[0][2] = sum;
  35. //再次遍历二维数组,将有效值存入稀疏数组
  36. //用于保存稀疏数组的行数
  37. int count = 1;
  38. for (int i = 0; i < arr1.length; i++) {
  39. for (int j = 0; j < arr1[i].length; j++) {
  40. if (arr1[i][j] != 0) {
  41. //将值存入稀疏数组
  42. sparseArr[count][0] = i;
  43. sparseArr[count][1] = j;
  44. sparseArr[count][2] = arr1[i][j];
  45. count++;
  46. }
  47. }
  48. }
  49. //打印稀疏数组
  50. System.out.println("遍历稀疏数组");
  51. for (int i = 0; i < sparseArr.length; i++) {
  52. for (int j = 0; j < sparseArr[0].length; j++) {
  53. System.out.print(sparseArr[i][j] + " ");
  54. }
  55. System.out.println();
  56. }
  57. //稀疏数组------>二维数组
  58. //先得到二位数组的行列数
  59. int row = sparseArr[0][0];
  60. int col = sparseArr[0][1];
  61. int[][] arr2 = new int[row][col];
  62. //遍历稀疏数组,同时给二维数组赋值
  63. for (int i = 1; i < sparseArr.length; i++) {
  64. row = sparseArr[i][0];
  65. col = sparseArr[i][1];
  66. //该位置上对应的值
  67. int val = sparseArr[i][2];
  68. arr2[row][col] = val;
  69. }
  70. //打印二维数组
  71. System.out.println("遍历还原后的二维数组");
  72. for (int i = 0; i < arr2.length; i++) {
  73. for (int j = 0; j < arr2[0].length; j++) {
  74. System.out.print(arr2[i][j] + " ");
  75. }
  76. System.out.println();
  77. }
  78. }
  79. }
  80. 运行结果
  81. 遍历二维数组
  82. 0 0 0 0 0 0 0 0 0 0 0
  83. 0 0 1 0 0 0 0 0 0 0 0
  84. 0 0 0 2 0 0 0 0 0 0 0
  85. 0 0 0 0 3 0 0 0 0 0 0
  86. 0 0 0 0 0 0 0 0 0 0 0
  87. 0 0 0 0 0 0 0 0 0 0 0
  88. 0 0 0 0 0 0 0 0 0 0 0
  89. 0 0 0 0 0 0 0 0 0 0 0
  90. 0 0 0 0 0 0 0 0 0 0 0
  91. 0 0 0 0 0 0 0 0 0 0 0
  92. 0 0 0 0 0 0 0 0 0 0 0
  93. 遍历稀疏数组
  94. 11 11 3
  95. 1 2 1
  96. 2 3 2
  97. 3 4 3
  98. 遍历还原后的二维数组
  99. 0 0 0 0 0 0 0 0 0 0 0
  100. 0 0 1 0 0 0 0 0 0 0 0
  101. 0 0 0 2 0 0 0 0 0 0 0
  102. 0 0 0 0 3 0 0 0 0 0 0
  103. 0 0 0 0 0 0 0 0 0 0 0
  104. 0 0 0 0 0 0 0 0 0 0 0
  105. 0 0 0 0 0 0 0 0 0 0 0
  106. 0 0 0 0 0 0 0 0 0 0 0
  107. 0 0 0 0 0 0 0 0 0 0 0
  108. 0 0 0 0 0 0 0 0 0 0 0
  109. 0 0 0 0 0 0 0 0 0 0 0

2、队列

定义

  • 队列是一个有序列表,可以用数组或是链表来实现。
  • 遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出

    模拟思路

  • 队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量

  • 因为队列的输出、输入是分别从前后端来处理,因此需要两个变量 front及 rear分别记录队列前后端的下标,front 会随着数据输出而改变,而 rear则是随着数据输入而改变,如图所示

    一、稀疏数组和队列 - 图2

    入队出队操作模拟

    当我们将数据存入队列时称为”addQueue”,addQueue 的处理需要有两个步骤:

  • 将尾指针往后移:rear+1 , 当 front == rear 时,队列为空

  • 若尾指针 rear 小于队列的最大下标 maxSize-1,则将数据存入 rear所指的数组元素中,否则无法存入数据。rear == maxSize - 1时,队列满

注意:front指向的是队列首元素的前一个位置
实现代码

  1. package com.atguigu.queue;
  2. import java.util.Scanner;
  3. public class ArrayQueueDemo {
  4. public static void main(String[] args) {
  5. //测试一把
  6. //创建一个队列
  7. ArrayQueue queue = new ArrayQueue(3);
  8. char key = ' '; //接收用户输入
  9. Scanner scanner = new Scanner(System.in);//
  10. boolean loop = true;
  11. //输出一个菜单
  12. while(loop) {
  13. System.out.println("s(show): 显示队列");
  14. System.out.println("e(exit): 退出程序");
  15. System.out.println("a(add): 添加数据到队列");
  16. System.out.println("g(get): 从队列取出数据");
  17. System.out.println("h(head): 查看队列头的数据");
  18. key = scanner.next().charAt(0);//接收一个字符
  19. switch (key) {
  20. case 's':
  21. queue.showQueue();
  22. break;
  23. case 'a':
  24. System.out.println("输出一个数");
  25. int value = scanner.nextInt();
  26. queue.addQueue(value);
  27. break;
  28. case 'g': //取出数据
  29. try {
  30. int res = queue.getQueue();
  31. System.out.printf("取出的数据是%d\n", res);
  32. } catch (Exception e) {
  33. // TODO: handle exception
  34. System.out.println(e.getMessage());
  35. }
  36. break;
  37. case 'h': //查看队列头的数据
  38. try {
  39. int res = queue.headQueue();
  40. System.out.printf("队列头的数据是%d\n", res);
  41. } catch (Exception e) {
  42. // TODO: handle exception
  43. System.out.println(e.getMessage());
  44. }
  45. break;
  46. case 'e': //退出
  47. scanner.close();
  48. loop = false;
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. System.out.println("程序退出~~");
  55. }
  56. }
  57. // 使用数组模拟队列-编写一个ArrayQueue类
  58. class ArrayQueue {
  59. private int maxSize; // 表示数组的最大容量
  60. private int front; // 队列头
  61. private int rear; // 队列尾
  62. private int[] arr; // 该数据用于存放数据, 模拟队列
  63. // 创建队列的构造器
  64. public ArrayQueue(int arrMaxSize) {
  65. maxSize = arrMaxSize;
  66. arr = new int[maxSize];
  67. front = -1; // 指向队列头部,分析出front是指向队列头的前一个位置.
  68. rear = -1; // 指向队列尾,指向队列尾的数据(即就是队列最后一个数据)
  69. }
  70. // 判断队列是否满
  71. public boolean isFull() {
  72. return rear == maxSize - 1;
  73. }
  74. // 判断队列是否为空
  75. public boolean isEmpty() {
  76. return rear == front;
  77. }
  78. // 添加数据到队列
  79. public void addQueue(int n) {
  80. // 判断队列是否满
  81. if (isFull()) {
  82. System.out.println("队列满,不能加入数据~");
  83. return;
  84. }
  85. rear++; // 让rear 后移
  86. arr[rear] = n;
  87. }
  88. // 获取队列的数据, 出队列
  89. public int getQueue() {
  90. // 判断队列是否空
  91. if (isEmpty()) {
  92. // 通过抛出异常
  93. throw new RuntimeException("队列空,不能取数据");
  94. }
  95. front++; // front后移
  96. return arr[front];
  97. }
  98. // 显示队列的所有数据
  99. public void showQueue() {
  100. // 遍历
  101. if (isEmpty()) {
  102. System.out.println("队列空的,没有数据~~");
  103. return;
  104. }
  105. for (int i = 0; i < arr.length; i++) {
  106. System.out.printf("arr[%d]=%d\n", i, arr[i]);
  107. }
  108. }
  109. // 显示队列的头数据, 注意不是取出数据
  110. public int headQueue() {
  111. // 判断
  112. if (isEmpty()) {
  113. throw new RuntimeException("队列空的,没有数据~~");
  114. }
  115. return arr[front + 1];
  116. }
  117. }

环形队列

思路:

  • front变量指向队首元素,初值为0
  • rear变量指向队尾元素的下一个元素,初值为0。规定空出一个位置
  • 队列为空的判定条件:front == rear
  • 队列为满的判定条件:(rear + 1) % maxSize == front
  • 队列中有效元素的个数:(rear - front + maxSize) % maxSize
  • 入队和出队时,都需要让标记对maxSize取模

代码

  1. package com.atguigu.queue;
  2. import java.util.Scanner;
  3. public class CircleArrayQueueDemo {
  4. public static void main(String[] args) {
  5. //测试一把
  6. System.out.println("测试数组模拟环形队列的案例~~~");
  7. // 创建一个环形队列
  8. CircleArray queue = new CircleArray(4); //说明设置4, 其队列的有效数据最大是3
  9. char key = ' '; // 接收用户输入
  10. Scanner scanner = new Scanner(System.in);//
  11. boolean loop = true;
  12. // 输出一个菜单
  13. while (loop) {
  14. System.out.println("s(show): 显示队列");
  15. System.out.println("e(exit): 退出程序");
  16. System.out.println("a(add): 添加数据到队列");
  17. System.out.println("g(get): 从队列取出数据");
  18. System.out.println("h(head): 查看队列头的数据");
  19. key = scanner.next().charAt(0);// 接收一个字符
  20. switch (key) {
  21. case 's':
  22. queue.showQueue();
  23. break;
  24. case 'a':
  25. System.out.println("输出一个数");
  26. int value = scanner.nextInt();
  27. queue.addQueue(value);
  28. break;
  29. case 'g': // 取出数据
  30. try {
  31. int res = queue.getQueue();
  32. System.out.printf("取出的数据是%d\n", res);
  33. } catch (Exception e) {
  34. // TODO: handle exception
  35. System.out.println(e.getMessage());
  36. }
  37. break;
  38. case 'h': // 查看队列头的数据
  39. try {
  40. int res = queue.headQueue();
  41. System.out.printf("队列头的数据是%d\n", res);
  42. } catch (Exception e) {
  43. // TODO: handle exception
  44. System.out.println(e.getMessage());
  45. }
  46. break;
  47. case 'e': // 退出
  48. scanner.close();
  49. loop = false;
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. System.out.println("程序退出~~");
  56. }
  57. }
  58. class CircleArray {
  59. private int maxSize; // 表示数组的最大容量
  60. //front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素
  61. //front 的初始值 = 0
  62. private int front;
  63. //rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
  64. //rear 的初始值 = 0
  65. private int rear; // 队列尾
  66. private int[] arr; // 该数据用于存放数据, 模拟队列
  67. public CircleArray(int arrMaxSize) {
  68. maxSize = arrMaxSize;
  69. arr = new int[maxSize];
  70. }
  71. // 判断队列是否满
  72. public boolean isFull() {
  73. return (rear + 1) % maxSize == front;
  74. }
  75. // 判断队列是否为空
  76. public boolean isEmpty() {
  77. return rear == front;
  78. }
  79. // 添加数据到队列
  80. public void addQueue(int n) {
  81. // 判断队列是否满
  82. if (isFull()) {
  83. System.out.println("队列满,不能加入数据~");
  84. return;
  85. }
  86. //直接将数据加入
  87. arr[rear] = n;
  88. //将 rear 后移, 这里必须考虑取模
  89. rear = (rear + 1) % maxSize;
  90. }
  91. // 获取队列的数据, 出队列
  92. public int getQueue() {
  93. // 判断队列是否空
  94. if (isEmpty()) {
  95. // 通过抛出异常
  96. throw new RuntimeException("队列空,不能取数据");
  97. }
  98. // 这里需要分析出 front是指向队列的第一个元素
  99. // 1. 先把 front 对应的值保留到一个临时变量
  100. // 2. 将 front 后移, 考虑取模
  101. // 3. 将临时保存的变量返回
  102. int value = arr[front];
  103. front = (front + 1) % maxSize;
  104. return value;
  105. }
  106. // 显示队列的所有数据
  107. public void showQueue() {
  108. // 遍历
  109. if (isEmpty()) {
  110. System.out.println("队列空的,没有数据~~");
  111. return;
  112. }
  113. // 思路:从front开始遍历,遍历多少个元素
  114. // 动脑筋
  115. for (int i = front; i < front + size() ; i++) {
  116. System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
  117. }
  118. }
  119. // 求出当前队列有效数据的个数
  120. public int size() {
  121. // rear = 2
  122. // front = 1
  123. // maxSize = 3
  124. return (rear + maxSize - front) % maxSize;
  125. }
  126. // 显示队列的头数据, 注意不是取出数据
  127. public int headQueue() {
  128. // 判断
  129. if (isEmpty()) {
  130. throw new RuntimeException("队列空的,没有数据~~");
  131. }
  132. return arr[front];
  133. }
  134. }