数据结构之稀疏数组队列

1、稀疏数组

1.1、实际需求

  • 编写的五子棋程序中,有存盘退出和续上盘的功能
  • 因为该二维数组的很多值是默认值 0 ,因此 记录了很多没有意义的数据,我们将其转为 稀疏数组进行存储

1.2、稀疏数组应用

1.2.1、稀疏数组处理方法

  • 稀疏数组把具有不同值的元素的 行列及值记录在一个小规模的数组中,从而缩小程序的规模
  • 稀疏数组也是二维数组,行数由原数组的数据决定,列数一般为 3 列
  • 稀疏数组的 第一行记录原数组一共有几行几列,有多少个不为零的值
    • 第一列:原数组的行数
    • 第二列:原数组的列数
    • 第三列:原数组有多少个不为零的值
  • 之后的行记录原数组中 不为零(x)的值所在的行数、列数以及 x 的值
    • 第一列:x 在原数组中的行数
    • 第二列:x 在原数组中的列数
    • 第三列:x 的值

1.2.2、举例说明

  • 原始二维数组较大,压缩后占用空间减少

1.3、应用实例

1.3.1、思路分析

  • 使用稀疏数组, 来保留类似前面的二维数组(棋盘、 地图等等)
  • 把稀疏数组存盘, 并且可以从新恢复原来的二维数组数

1.3.2、代码实现

  • 代码
public class SparseArray {
    
    

	public static void main(String[] args) {
    
    

		int chessArr1[][] = new int[11][11];
		chessArr1[1][2] = 1;
		chessArr1[2][3] = 2;
		chessArr1[4][5] = 2;

		System.out.println("原始的二维数组~~");
		for (int[] row : chessArr1) {
    
    
			for (int data : row) {
    
    
				System.out.printf("%d\t", data);
			}
			System.out.println();
		}

		int sum = 0;
		for (int i = 0; i < chessArr1.length; i++) {
    
    
			for (int j = 0; j < chessArr1[i].length; j++) {
    
    
				if (chessArr1[i][j] != 0) {
    
    
					sum++;
				}
			}
		}

		int sparseArr[][] = new int[sum + 1][3];

		sparseArr[0][0] = chessArr1.length;
		sparseArr[0][1] = chessArr1[0].length;
		sparseArr[0][2] = sum;

		int count = 0;
		for (int i = 0; i < chessArr1.length; i++) {
    
    
			for (int j = 0; j < chessArr1[i].length; j++) {
    
    
				if (chessArr1[i][j] != 0) {
    
    
					count++;
					sparseArr[count][0] = i;
					sparseArr[count][1] = j;
					sparseArr[count][2] = chessArr1[i][j];
				}
			}
		}

		System.out.println();
		System.out.println("得到稀疏数组为~~~~");
		for (int i = 0; i < sparseArr.length; i++) {
    
    
			System.out.printf("%d\t%d\t%d\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
		}
		System.out.println();

		int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

		for (int i = 1; i < sparseArr.length; i++) {
    
    
			chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
		}

		System.out.println();
		System.out.println("恢复后的二维数组");

		for (int[] row : chessArr2) {
    
    
			for (int data : row) {
    
    
				System.out.printf("%d\t", data);
			}
			System.out.println();
		}
	}

}

  • 程序运行结果
&#x539F;&#x59CB;&#x7684;&#x4E8C;&#x7EF4;&#x6570;&#x7EC4;~~
0	0	0	0	0	0	0	0	0	0	0
0	0	1	0	0	0	0	0	0	0	0
0	0	0	2	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	2	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0

&#x5F97;&#x5230;&#x7A00;&#x758F;&#x6570;&#x7EC4;&#x4E3A;~~~~
11	11	3
1	2	1
2	3	2
4	5	2

&#x6062;&#x590D;&#x540E;&#x7684;&#x4E8C;&#x7EF4;&#x6570;&#x7EC4;
0	0	0	0	0	0	0	0	0	0	0
0	0	1	0	0	0	0	0	0	0	0
0	0	0	2	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	2	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0

1.4、课后练习

  • 在前面的基础上, 将稀疏数组保存到磁盘上, 比如 map.data
  • 恢复原来的数组时, 读取 map.data 进行恢复

2、队列

2.1、队列使用场景

  • 银行排队的案例:

2.2、队列介绍

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

2.3、数组模拟队列

2.3.1、思路分析

  • maxSize :队列容量(数组的长度)
  • arr :模拟队列的数组
  • front :指向队列头部元素的前一个元素,初始值为 -1
  • rear :指向队列尾部元素,初始值为 -1

  • 基本操作
    • 队列判空: front == rear
    • 队列判满: rear == (maxSize - 1) ,即 rear 是否已经指向了数组的最后一个位置
    • 队列元素个数:rear - front
    • 队列入队:队列不满才能入队,arr[++rear] = value
    • 队列出队:队列不空才能出队,return arr[front++]

2.3.2、代码实现

  • 队列的定义

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;
		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++;
		arr[rear] = n;
	}

	public int getQueue() {
    
    

		if (isEmpty()) {
    
    

			throw new RuntimeException("队列空,不能取数据");
		}
		front++;
		return arr[front];

	}

	public void showQueue() {
    
    

		if (isEmpty()) {
    
    
			System.out.println("队列空的,没有数据~~");
			return;
		}
		for (int i = front + 1; i  rear; i++) {
    
    

			System.out.printf("arr[%d]=%d\n", i, arr[i]);
		}
	}

	public int headQueue() {
    
    

		if (isEmpty()) {
    
    
			throw new RuntimeException("队列空的,没有数据~~");
		}
		return arr[front + 1];
	}
}
  • 测试代码
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): 查看队列头的数据");
			System.out.println();
			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) {
    
    
					System.out.println(e.getMessage());
				}
				break;
			case 'h':
				try {
    
    
					int res = queue.headQueue();
					System.out.printf("队列头的数据是%d\n", res);
				} catch (Exception e) {
    
    
					System.out.println(e.getMessage());
				}
				break;
			case 'e':
				scanner.close();
				loop = false;
				break;
			default:
				break;
			}
		}

		System.out.println("程序退出~~");
	}

}
  • 程序运行结果
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

s
&#x961F;&#x5217;&#x7A7A;&#x7684;&#xFF0C;&#x6CA1;&#x6709;&#x6570;&#x636E;~~
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

a
&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x6570;
1
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

a
&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x6570;
2
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

a
&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x6570;
3
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

s
arr[0]=1
arr[1]=2
arr[2]=3
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

a
&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x6570;
4
&#x961F;&#x5217;&#x6EE1;&#xFF0C;&#x4E0D;&#x80FD;&#x52A0;&#x5165;&#x6570;&#x636E;~
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

g
&#x53D6;&#x51FA;&#x7684;&#x6570;&#x636E;&#x662F;1
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

g
&#x53D6;&#x51FA;&#x7684;&#x6570;&#x636E;&#x662F;2
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

g
&#x53D6;&#x51FA;&#x7684;&#x6570;&#x636E;&#x662F;3
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

g
&#x961F;&#x5217;&#x7A7A;&#xFF0C;&#x4E0D;&#x80FD;&#x53D6;&#x6570;&#x636E;
s(show): &#x663E;&#x793A;&#x961F;&#x5217;
e(exit): &#x9000;&#x51FA;&#x7A0B;&#x5E8F;
a(add): &#x6DFB;&#x52A0;&#x6570;&#x636E;&#x5230;&#x961F;&#x5217;
g(get): &#x4ECE;&#x961F;&#x5217;&#x53D6;&#x51FA;&#x6570;&#x636E;
h(head): &#x67E5;&#x770B;&#x961F;&#x5217;&#x5934;&#x7684;&#x6570;&#x636E;

2.4、数组模型环形队列

2.4.1、提出问题

  • 目前数组 使用一次就不能用, 没有达到复用的效果,造成 内存空间的浪费
  • 将这个数组使用算法, 改进成一个 环形的队列( 取模: %

2.4.2、思路分析

  • 对前面的队列进行优化,改造为环形队列(通过 取模实现)
  • maxSize :队列容量(数组的长度)
  • arr :模拟队列的数组
  • front :指向队列头部元素,初始值为 0
  • rear :指向队列尾部元素的后一个元素,初始值为 0

  • 基本操作
    • 队列判空: front == rear
    • 队列判满:
      • 为何要在 rear 之后,front 之前空出一个元素的空间?因为如果不空出一个元素,队列判空条件为:front == rear ,队列判满的条件也是:front == rear , 有歧义
      • 队列容量:因为空出了一个元素,所以队列容量就变成了 (maxSize - 1)
      • 当空出一个元素的空间,如何判满?当还剩一个元素时,队列就已经满了,所以判断条件为 (rear + 1) % maxSize == front
    • 队列元数个数:
      • 计算公式: (rear + maxSize - front) % maxSize ,这样来思考:
      • 当 rear 比 front 大时,即 (rear -front) > 0 ,这时还没有形成环形结构, (rear -front) 即是队列元素个数
      • 当 rear 比 front 小时,即 (rear -front) < 0 ,这时已经形成了环形结构, (rear -front) 表示数组还差多少个元素存满(负数), (rear + maxSize - front) 即是队列元素个数
      • 综上: (rear + maxSize - front) % maxSize
    • 队列入队:
      • 首先,队列不满才能入队
      • 由于 rear 指向 队列尾部元素的后一个元素,所以直接设置即可: arr[rear] = value
      • 接下来,rear 应该向后移动一个位置: rear = (rear + 1) % maxSize
      • 取模是为了 防止数组越界,让指针从新回到数组第一个元素
    • 队列出队:
      • 首先,队列不空才能出队
      • 由于 front 直接指向队列头部元素,所以直接返回该元素即可:int value = arr[front ]
      • 接下来,front 应该向后移动一个位置: front = (front + 1) % maxSize
      • 取模是为了 *防止数组越界,让指针从新回到数组第一个元素

2.4.3、代码实现

  • 环形队列的实现
class CircleArray {
    
    
	private int maxSize;

	private int front;

	private 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 + 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];
	}
}
  • 测试代码
public class CircleArrayQueueDemo {
    
    

	public static void main(String[] args) {
    
    

		System.out.println("测试数组模拟环形队列的案例~~~");

		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): 查看队列头的数据");
			System.out.println();
			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) {
    
    

					System.out.println(e.getMessage());
				}
				break;
			case 'h':
				try {
    
    
					int res = queue.headQueue();
					System.out.printf("队列头的数据是%d\n", res);
				} catch (Exception e) {
    
    

					System.out.println(e.getMessage());
				}
				break;
			case 'e':
				scanner.close();
				loop = false;
				break;
			default:
				break;
			}
		}
		System.out.println("程序退出~~");
	}

}
  • 程序运行结果
测试数组模拟环形队列的案例~~~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

s
arr[0]=1
arr[1]=2
arr[2]=3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

a
输出一个数
4
队列满,不能加入数据~
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是1
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是2
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

s
arr[2]=3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
取出的数据是3
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

g
队列空,不能取数据
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

猜你喜欢

转载自blog.csdn.net/weixin_42145856/article/details/114007009
今日推荐