目录
一. 前言
1.数据结构(DataStructure)是一门研究组织数据方式的学科,有了编译语言就有了数据结构
2.学好数据结构可以编写出更有效率的代码
3.程序=数据结构+算法
4.数据结构是算法的基础
二. 线性结构和非线性结构
数据结构包括线性结构和非线性结构
2.1 线性结构
1.线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系
2.线性结构有两种不同的存储结构,即顺序存储结构(数组)和链式存储结构(链表)。
3.顺序存储结构的线性表称为顺序表,顺序表中的存储元素是连续的。
4.链式存储的线性表称为链表,链表中的存储元素不一定是连续的,元素节点中存放数据元素以及相邻元素的地址信息
5.线性结构常见的有:数组、队列、链表和栈。
2.2 非线性结构
非线性结构中各个数据元素不再保持在一个线性序列中,每个数据元素可能与零个或者多个其他数据元素发生联系。
非线性结构包括;二维数组、多维数组、广义表、树结构、图结构。
三.稀疏数组和队列
3.1稀疏数组
基本介绍:当一个数组中大部分元素为零,或者为同一个值的数组时,可以用稀疏数组来保存该数组
稀疏数组的处理方法是:
(1)记录数组一共有几行几列,有多少个不同的值。
(2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模。
二维数组转稀疏数组的思路:
1.遍历原始的二维数组,得到有效数据的个数(sum)
2.根据有效数据(sum)的个数就可以创建稀疏数组sparseArr int[sum+1][3]
3.将二维数组的有效数据存入到稀疏数组
稀疏数组转二维数组的思路:
1.先读取稀疏数组的第一行, 根据第一行的数据,创建原始的二维数组
2.再读取稀疏数组的后几行数据,并赋给原始的二维数组即可。
代码实现
public class SparesArrayExercise {
public static void main(String[] args) {
int[][] chessArr = new int[11][11];//创建一个11*11的二维数组
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[4][5] = 2;
System.out.println("原始的数组");
for (int[] row : chessArr) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
//遍历二维数组得到非0数据的个数
int sum = 0;
for (int i = 0; i < chessArr.length; i++) {
for (int j = 0; j < chessArr.length; j++) {
if (chessArr[i][j] != 0) {
sum++;
}
}
}
//创建对应的稀疏数组
int[][] sparesArr = new int[sum + 1][3];
//给稀疏数组赋值
sparesArr[0][0] = 11;
sparesArr[0][1] = 11;
sparesArr[0][2] = sum;
//遍历二维数组,将非0的值存放到sparseArr
int count = 0;//用于记录是第几个非0数据
for (int i = 0; i < chessArr.length; i++) {
for (int j = 0; j < chessArr.length; j++) {
if (chessArr[i][j] != 0) {
count++;
sparesArr[count][0] = i;
sparesArr[count][1] = j;
sparesArr[count][2] = chessArr[i][j];
}
}
}
System.out.println("得到的稀疏数组为");
for (int i = 0; i < sparesArr.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparesArr[i][0], sparesArr[i][1], sparesArr[i][2]);
}
System.out.println("恢复后的数组为");
int[][] chessArr2 = new int[sparesArr[0][0]][sparesArr[0][1]];
//从第二行开始,赋给原始的二维数组
for (int i = 1; i < sparesArr.length; i++) {
chessArr2[sparesArr[i][0]][sparesArr[i][1]] = sparesArr[i][2];
}
for (int[] row : chessArr2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}
3.2 队列
队列介绍:
(1)队列是一个有序列表,可以用数组或是链表来实现
(2)遵循先入先出的原则,即:先存入队列的数据先取出,后存入的后取出。
(3)示意图(使用数组模拟队列)
使用数组模拟队列思路
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如上图,其中maxSize是该队列的最大容量
front会随着数据的输出而改变, rear随着数据的输入而改变。
当我们存入队列时(addQueue),有两个步骤
(1)rear + 1
(2)若rear==maxSize-1 队列满不能再addQueue
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = 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':
arrayQueue.showQueue();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try {
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是%d\n", res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = arrayQueue.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("程序退出");
}
}
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 = 0; i < arr.length; i++) {
System.out.printf("arr[%d]=%d\n", i, arr[i]);
}
}
public int headQueue() {
if (isEmpty()) {
System.out.println("队列空");
throw new RuntimeException();
}
return arr[front + 1];
}
}
问题分析并优化:
(1)目前数组使用一次就不能用,没有达到复用的效果
(2)将这个数组使用算法,改进成一个环形的队列,取模%。
数组模拟环形队列