笔记依照[尚硅谷·数据结构]
第一节
稀疏矩阵的转换
public class SpraseMatrixMain {
public static void main(String[] args) {
int chessArr1[][] = new int[11][11];
// 1黑 2蓝
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
System.out.println("输出原矩阵:");
for (int[] x : chessArr1) {
for (int y : x) {
System.out.printf("%d\t", y);
}
System.out.println();
}
int sum = 0;
for (int i = 0; i < chessArr1.length; i++) {
for (int j = 0; j < chessArr1[0].length; j++) {
if (chessArr1[i][j] != 0) {
sum++;
}
}
}
System.out.println(sum);
//将原矩阵转压缩为稀疏矩阵
int[][] sparseMatrix = new int[sum + 1][3];
sparseMatrix[0][0] = 11;
sparseMatrix[0][1] = 11;
sparseMatrix[0][2] = sum;
int x = 1;
for (int i = 0; i < chessArr1.length; i++) {
for (int j = 0; j < chessArr1[0].length; j++) {
int y = 0;
if (chessArr1[i][j] != 0) {
sparseMatrix[x][y] = i;
y++;
sparseMatrix[x][y] = j;
y++;
sparseMatrix[x][y] = chessArr1[i][j];
x++;
}
y = 0;
}
}
System.out.println("打印压缩矩阵");
for (int[] sparses : sparseMatrix) {
for (int sparse : sparses) {
System.out.printf("%d\t", sparse);
}
System.out.println();
}
//将稀疏矩阵转还原成信息矩阵
int[][] chessArray2 = new int[sparseMatrix[0][0]][sparseMatrix[0][1]];
for (int i = 1; i < sparseMatrix.length; i++) {
int j = 0;
chessArray2[sparseMatrix[i][j]][sparseMatrix[i][j + 1]] = sparseMatrix[i][j + 2];
}
System.out.println("输出还原矩阵");
for (int[] chesses : chessArray2) {
for (int chess : chesses) {
System.out.printf("%d\t", chess);
}
System.out.println();
}
}
}
队列的基本知识
基本队列
public class ArrayQueueDemo {
public static void main(String[] args) {
}
}
class ArrayQueue {
private int front;
private int rear;
private int MaxSize;
private int[] arr;
public ArrayQueue(int maxSize) {
this.front = -1;
this.rear = -1;
MaxSize = maxSize;
this.arr = new int[maxSize];
}
//判断栈是否满了
public Boolean IsFill() {
if (this.rear == this.MaxSize - 1)
return true;
else
return false;
}
//判断栈是否为空
public Boolean IsEmpty() {
if (this.front == this.rear)
return true;
else
return false;
}
//向栈中加入数据
public void addData(int data) {
if (IsFill()) {
throw new RuntimeException("这个栈已经满了,不能再加入了");
}
this.rear += 1;
this.arr[this.rear] = data;
}
//获取栈顶元素
public int pullData() {
if (IsEmpty()) {
throw new RuntimeException("这个队列是空的哦.");
}
this.front += 1;
return arr[this.front];
}
}
环形队列
使用数组模拟环形队列的思路分析:
-
将front变量的含义做一个调整: 指向队列的第一个元素 也就是 rear,front是队列的第一个元素
-
将rear变量的含义也做一个调整: 指向队列的最后一个元素的后一个位置 (因为希望空出一个空间作为一个约定:
-
环形队列判满条件是: (rear+1)%maxsize == front
-
环形队列的判空条件是: rear == front
-
环形队列中的数据个数: ( rear + maxSize - front ) % maxSize
这里的预留一个空的位置,不是说rear 不能指到那上面去,而是说可以指到上面去,但是Arr[rear] = null
代码实现:
import java.util.Scanner; public class CircleArrayQueueDemo { public static void main(String[] args) { System.out.println("=============测试数组模拟环形队列的案例============="); //创建一个队列 CircleArray circleArray = new CircleArray(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': circleArray.showQueue(); break; case 'a': System.out.println("请输入一个int数据"); int in = scanner.nextInt(); circleArray.addQueue(in); break; case 'e': loop = false; break; case 'h': System.out.println("队列头数据:" + circleArray.headQueue()); break; case 'g': System.out.println("取出队列头数据:" + circleArray.getQueue()); break; } System.out.println("=============测试数组模拟环形队列的案例 IS END============="); } } } class CircleArray { private int front; private int rear; private int maxSize; private int[] arr; public CircleArray(int maxSize) { this.maxSize = maxSize; this.arr = new int[maxSize]; } //判断队列是否满 public Boolean IsFill() { return (this.rear + 1) % this.maxSize == front; } //判断队列是否为空 public Boolean IsEmpty() { return this.rear == this.front; } //添加数据 public void addQueue(int data) { if (IsFill()) { System.out.println("队列已经满了,不能再添加了"); return; } arr[rear] = data; this.rear = (this.rear + 1) % this.maxSize; } //获取队列的数据,出队列 public int getQueue() { if (IsEmpty()) { throw new RuntimeException("队列已经空了,不能再取"); } //这里需要分析front是指向队列的第一个元素 //先把front的对应的值保存到一个临时变量 //将front后移,考虑取模 //降临时保存的变量返回 int value = arr[front]; front = (front + 1) % maxSize; return value; } //求得队列中的有效数据的总和 public int size() { return (this.rear + this.maxSize - this.front) % this.maxSize; } //显示队列的数据 public void showQueue() { if (IsEmpty()) { System.out.println("队列是空的,没有数据"); return; } for (int i = front; i < front + this.size(); i++) { System.out.printf("arr[%d] = %d\n", i % maxSize, arr[i % maxSize]); } } //显示队列的头数据, 不是取出数据 public int headQueue() { if (IsEmpty()) { throw new RuntimeException("队列是空的,没有数据"); } return arr[front]; } }
链表的基本知识
基础单链表
在Java的单链表的基础中 , 涉及一个基础知识: [x.equals(y) == true]
两个对象的地址值相同则他们的hashCode则也是相同的"=="比较的是两个变量的地址值是否相同, 在后面的测试中可知temp.hashCode() == temp.getNext().hashCode() 我们可知他们的两个地址是相同的;
从而得到了C++中指针的效果
package linkList;
public class SingleLinkedListDemo {
public static