线性结构和非线性结构
线性结构
数据结构包括:线性结构和非线性结构
1.线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系
2.线性结构有两种不同的存储结构,即顺序存储结构和链式存储结构。
顺序存储的线性表称为顺序表,顺序表中存储的元素是连续的
3.链式存储的线性表称为链表,链表中存储的元素不一定是连续的,元素节点中存放数据元素以及相邻元素的地址信息
4.线性结构常见的有:数据、队列、链表和栈
非线性结构
非线性结构包括:二维数组、多维数组、广义表、树结构、图结构
稀疏数组
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组
稀疏数组的处理方法是:
1.记录数组一共有几行几列,有多少个不同的值
2.把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
举例 原始的二维数组 6 * 7 = 42
0 | 0 | 0 | 22 | 0 | 0 | 15 |
---|---|---|---|---|---|---|
0 | 11 | 0 | 0 | 0 | 17 | 0 |
0 | 0 | 0 | -6 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 39 | 0 |
91 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 28 | 0 | 0 | 0 | 0 |
将上列二维数组改为稀疏数组 9 * 3 = 27
行 (row) | 列 (col) | 值 (value) | |
---|---|---|---|
[0] | 6 | 7 | 8 |
[1] | 0 | 3 | 22 |
[2] | 0 | 6 | 15 |
[3] | 1 | 1 | 11 |
[4] | 1 | 5 | 17 |
[5] | 2 | 3 | -6 |
[6] | 3 | 5 | 39 |
[7] | 4 | 0 | 91 |
[8] | 5 | 2 | 28 |
二维数组转稀疏数组
1.遍历原始的二位数组,得到有效数据的个数sum
2.根据sum创建稀疏数组,int sparseArrsum+1
3.将二维数组的有效数据存入到稀疏数组中
稀疏数组转二维数组
1.先读取稀疏数组的第一行,根据第一行的数据,创建原始二维数组,比如上面的int arr6
2.在读取稀疏数组后几行的数据,并赋给原始的二维数组即可
二维数组转稀疏数组:还有一个过程稀疏数组保存到磁盘 属于IO
稀疏数组转二维数组:需要将磁盘上的文件恢复成稀疏数组,在恢复成原二维数组
package com.stx.sparsearray;
public class SparseArray {
public static void main(String[] args) {
/**
* 原始的二维数组
*/
//创建一个原始的二维数组 11*11
//0:表示没有棋子 1表示黑子 2表示白子
int chessArr1[][] = new int[11][11];
//chessArr1[0][0]=1;
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
//chessArr1[10][10] = 2;
//输出原始的二维数组
System.out.println("原始的二维数组");
for (int[] row : chessArr1) {
for (int data : row) {
System.out.print(data + "\t");
}
System.out.println();
}
/**
* 将原始的二维数组转换为稀疏数组
*/
//1.遍历原始的二维数组,得到有效的数据的个数sum
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 += 1;
}
}
}
//2.根据sum创建对应的稀疏数组
int sparseArr[][] = new int[sum + 1][3];
//3.将有效的原始二维数组的数据赋值给稀疏数组
//3.1首先将稀疏数组的第一行赋值
int row = chessArr1.length;
int col = chessArr1[0].length;
sparseArr[0][0] = row;
sparseArr[0][1] = col;
sparseArr[0][2] = sum;
//3.2将稀疏数组的其他有效数据值赋值
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("得到的稀疏数组");
for (int i = 0; i < sparseArr.length; i++) {
for (int j = 0; j < sparseArr[i].length; j++) {
System.out.print(sparseArr[i][j] + "\t");
}
System.out.println();
}
System.out.println("----------------------");
/**
* 将稀疏数组恢复成原始的二维数组
*/
//1.先读取稀疏数组的第一行数据,根据读取到的第一行的数据创建数组
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
//2.在读取稀疏数组的后几行的数据,并赋给原始的二维数组
for (int i = 1; i < sparseArr.length; i++) {
int j = 0;
chessArr2[sparseArr[i][j]][sparseArr[i][j + 1]] = sparseArr[i][j + 2];
//chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
System.out.println("遍历恢复后的二维数组");
for (int i = 0; i < chessArr2.length; i++) {
for (int j = 0; j < chessArr2[i].length; j++) {
System.out.print(chessArr2[i][j] + "\t");
}
System.out.println();
}
}
}
队列
队列是一个有序列表,可以用数组或是链表来实现
遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出
数组模拟队列的思路
队列本身是有序列表,若使用数组的结构来存储队列的数据,则队列数组的声明如下图,其中maxSize是该队列的最大容量。
因为队列的输出、输入分别是从前后端来处理,因此需要俩个变量front和rear分别记录队列前后端的下标,front会随着数据的输出而改变,而rear则是随着数据输入而改变,如果所示:
package com.stx.queue;
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
//测试
//创建一个队列对象
ArrayQueue arrayQueue = new ArrayQueue(3);
char key = ' '; //接收用户的输入
Scanner sc = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s(show):显示队列");
System.out.println("e(show):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列取出数据");
System.out.println("h(head):查看队列头的数据");
key = sc.next().charAt(0); //接收一个字符
switch (key) {
case 's': //
arrayQueue.showQueue();
break;
case 'a':
System.out.println("输入一个数");
int value = sc.nextInt();
arrayQueue.addQueue(value);
break;
case 'g': //取数据
try {
int result = arrayQueue.getQueue();
System.out.printf("取出的数据是:%d",result);
System.out.println();
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h': //查看队列头的数据
try {
int result = arrayQueue.headQueue();
System.out.println("队列头的数据是:" + result);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e': //退出
//sc.close();
loop = false;
break ;
default:
System.out.println("输入错误");
break;
}
}
System.out.println("程序退出");
}
}
//使用数组模拟队列 编写一个ArrayQueue类
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; //指向队列头部,分析出front是指向队列头的前一个位置
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++; //让rear后移
arr[rear] = n; //arr[++rear] = n;
}
//获取队列的数据,出队列
public int getQueue() {
//判断队列是否为空
if (isEmpty()) {
//不能return -1; 因为有可能数据就是-1
//通过抛出异常来处理
throw new RuntimeException("队列为空,不能取数据");
}
front++; //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()) {
throw new RuntimeException("队列空的,没有数据");
}
return arr[front + 1];
}
}
缺点:
1.目前数组只可以使用一次,不可以重复使用
2.将这个数组使用算法,改进成一个环形的队列 取模:%
使用数组模拟环形队列的思路分析
思路如下:
1.front变量的含义做一个调整:front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素,front的初始值=0
2.rear变量的含义也做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个空间做为约定,rear的初始值=0
3.当队列满时,条件是(rear + 1) % maxSize = front [队列满的条件]
4.当队列为空时,rear == front空
5.当我们这样分析,队列中有效的数据的个数 (rear + maxSize - front) % maxSize
6.我们就可以在原来的队列修改,得到环形队列
package com.stx.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//测试
//创建环形队列
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(3);
boolean flag = true;
Scanner sc = new Scanner(System.in);
char key = ' '; //接收用户的输入
while (flag) {
System.out.println("s(show):显示队列");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列取出数据");
System.out.println("e(exit):退出程序");
System.out.println("h(head):查看环形队列的头数据");
key = sc.next().charAt(0); //接收一个字符
switch (key) {
case 's': //显示队列
circleArrayQueue.showCircleQueue();
break;
case 'a': //添加
System.out.println("输入要添加的数据");
int value = sc.nextInt();
circleArrayQueue.addCircleQueue(value);
break;
case 'g': //取
try {
int result = circleArrayQueue.getCircleQueue();
System.out.println("取出的数据是:" + result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e': //退出
sc.close();
flag = false;
break;
case 'h': //显示头数据
try {
int result = circleArrayQueue.showHeadCircleQueue();
System.out.println("环形队列的头数据是:" + result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
System.out.println("输入错误");
break;
}
}
}
}
//使用数组模拟环形队列 编写一个CircleArrayQueue类
class CircleArrayQueue {
private int maxSize; //表示数组的最大容量
private int front; //环形队列的头指针
private int rear; //环形队列的尾指针
private int arr[]; //该数组用于存放数据,模拟环形队列
//创建环形队列的构造器
public CircleArrayQueue(int arrayMaxSiz) {
maxSize = arrayMaxSiz;
arr = new int[maxSize]; //创建一个多大的队列
front = 0; //指向环形队列的头部, 可以不写默认为0
rear = 0; //指向环形队列的尾部的前一个位置, 可以不写默认为0
}
//判断环形队列为空
public boolean isEmpty() {
return front == rear;
}
//判断队列为满
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
//添加数据到环形队列
public void addCircleQueue(int num) {
//先判断环形队列是否满
if ((rear + 1) % maxSize == front) {
System.out.println("环形队列已满");
return; //吐过已满无法存入数据,直接return
}
//直接将数据加入
arr[rear] = num;
//将rear后移,这里必须考虑取模
rear = (rear + 1) % maxSize;
}
//从环形队列获取数据
public int getCircleQueue() {
//判断是否为空
if (rear == front) {
throw new RuntimeException("环形队列为空,不能取数据");
}
//这里需要分析出front是指向队列的第一个元素
//1.先把front的值给一个临时变量
//2.在将front后移,考虑取模
//3.将临时变量返回
int temp = arr[front];
front = (front + 1) % maxSize;
return temp;
}
//遍历环形队列,显示队列的所有数据
public void showCircleQueue() {
//判断是否为空
if (front == rear) {
System.out.println("环形队列为空,没有数据");
return;
}
//思路:从front开始遍历,遍历多少个元素
//
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 - front + maxSize) % maxSize;
}
//显示环形队列的首数据,注意而不是取出数据
public int showHeadCircleQueue() {
//判断是否为空
if (rear == front) {
throw new RuntimeException("环形队列为空,没有数据");
}
//返回环形队列的首数据
return arr[front];
}
}
链表
链表是有序的列表,但是它在内存中数存储如下