数据结构与算法学习笔记
线性结构和非线性结构
数据结构包括:线性结构和非线性结构。
- 线性结构
线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系
线性结构有两种不同的存储结构,即顺序存储结构和链式存储结构。顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的
链式存储的线性表称为链表,链表中的存储元素不一定是连续的,元素节点中存放数据元素以及相邻元素的地址信息
线性结构常见的有:数组、队列、链表和栈,后面我们会详细讲解.
- 非线性结构
非线性结构包括:二维数组,多维数组,广义表,树结构,图结构
第1行第1列记录共有几行;
第1行第2列记录工几列;
第1行第3列记录共有多少个不同的值;
第2行开始记录有值的元素位置及元素值;
public static void main(String[] args) {
// 创建一个原始的二维数组 11*11
// 0:表示没有棋子。1表示黑子。2表示白子。
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
// 输出原始的二维数组
System.out.println("原始的二维数组");
for (int[] row:chessArr1) {
for (int item:row) {
System.out.printf("%d\t",item);
}
System.out.println();
}
System.out.println("或者");
for (int i=0;i<11;i++) {
for (int j=0;j<11;j++) {
System.out.printf("%d\t",chessArr1[i][j]);
}
System.out.println();
}
// 将二维数组转换成稀疏数组
// 1.先遍历二维数组,得到非零数据的个数
int sum= 0;
for (int i=0;i<11;i++){
for (int j =0;j<11;j++) {
if (chessArr1[i][j] != 0) {
sum++;
}
}
}
System.out.println("sum:"+sum);
// 2.创建对应的稀疏数组
int sparseArr[][] = new int[sum+1][3];
// 3.给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
// 遍历二维数组,将非0的值存放到稀疏数组sparseArr
int count =0; // 用于记录是第几个非0数据
for (int i=0;i<11;i++){
for (int j =0;j<11;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\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
}
// 将稀疏数组恢复成原始的二维数组
// 先读取稀疏数组的第一行,创建原始的数组
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 item:row) {
System.out.printf("%d\t",item);
}
System.out.println();
}
}
}
//将稀疏数组保存到磁盘中
private static void saveSparseArr(int[][] sparseArr) {
FileWriter fileWriter = null;
try{
File file = new File("D://map.data");
if (!file.exists())
file.createNewFile();
fileWriter = new FileWriter(file);
for (int i = 0;i< sparseArr.length;i++) {
for (int j = 0;j < 2;j++){
fileWriter.write(sparseArr[i][j]+",");
}
fileWriter.write(sparseArr[i][2]+ "");
fileWriter.write("\n");
}
// 每次io都会影响性能,将需要写入的内容,放入缓冲区中,然后调用flush方法,将缓冲区内容写入文件中。
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//读取磁盘中的文件,转换为稀疏数组
private static int[][] readSparse() {
FileReader fileReader = null;
BufferedReader bf = null;
int[][] sparseArr = null;
try {
File file = new File("D://map.data");
fileReader = new FileReader(file);
bf = new BufferedReader(fileReader);
List<String> strList = new ArrayList<>();
String line = null;
while ((line = bf.readLine()) != null) {
strList.add(line);
}
int size = strList.size();
sparseArr = new int[size][3];
int count = 0;
for (String str:strList) {
String[] strs = str.split("\\,");
sparseArr[count][0] = Integer.valueOf(strs[0]);
sparseArr[count][1] = Integer.valueOf(strs[1]);
sparseArr[count][2] = Integer.valueOf(strs[2]);
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sparseArr;
}
运行结果如下: