1、线性结构
1、线性结构作为最常用的数据结构,其特点是数据原神之间存在一对一的线性关系
2、线性结构有两种不同的存储结构,即顺序存储结构和链式存储结构。顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的。
3、链式存储的线性表称为链表,链表中的存储元素不一定是连续的,元素节点中存放数据元素以及相邻元素的地址信息。
4、线性结构常见有:数组、队列、链表和栈。
数据结构可视化网站:Data Structure Visualization
2、非线性结构
非线性结构包括:二维数组、多维数组、广义表、树结构、图结构。
3、稀疏数组
当一个数组中大部分元素为0,或者为同一个值的数组时可以用稀疏数组来保存该数组。
稀疏数组的处理方法时:
1、记录数组一共有几行几列,有多少个不同的值
2、把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
二维数组放入稀疏数组中的效果,行,列,值分表代表在二维数组中的位置和具体值,从而可以减少二维数组的体积。
稀疏数组的处理方法是:
1)记录数组一共有几行几列,有多少不同的值
2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
二维数组转稀疏数组的思路
1.遍历原始的二维数组,得到有效的数据个数sum
2.根据sum就可以创建稀疏数组sparseArr int[sum+1] [3]
3.将二维数组的有效数据存入到稀疏数组中
稀疏数组转换为原始的二维数据方法
1.先读取稀疏数组的第一行,根据第一行数据,创建原始的二维数组,比如上面的 chessArr2 = int[11][11]
2.在读取稀疏数组后几行的数据,并赋值给二维数组即可
代码实现:
/**
* @Author shy Boy
* @Date 2022/3/6 - 03 - 06 - 18:15
* @Description: com.hy.datastructures.guigu
* @version: 1.0
*/
public class SparesArray {
public static void main(String[] args) {
int rows = 11;
int cols = 11;
// 创建一个原始的二维数组11*11
// 0表示没有棋子 1表示黑子 2表示蓝子
int chessArr1[][] = new int[rows][cols];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
// 输出原始的二维数组
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 < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0){
// 记录值个数量
sum++;
}
}
}
System.out.println("sum = " +sum);
// 创建对于的稀疏数组
int sparseArr [][] = new int [sum+1] [3];
// 给稀疏数组赋值 稀疏数组第一行存的行、列、值
sparseArr[0][0] = rows;
sparseArr[0][1] = cols;
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]);
}
/**
* 将稀疏数组转换为二维数组
* 1、先读取稀疏数组第一行,根据第一行的数据,创建原始的二维数组
*/
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
// 2、在读取稀疏数组后几行的数据(从第二行开始),并赋值给原始的二维数组即可
for (int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
System.out.println();
// 输出原始的二维数组
for (int[] row : chessArr2){
for (int data : row){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}
4、队列
1) 队列是一个有序列表,可以用数组或者是链表来实现
2)遵循先入先出原则。即:现存入队列的数据,要先取出。后存入的要后取出
数组队列特点:front代表时队列的头而rear代表时队列的尾部,在存入数据时rear尾部下标一直在增加,在取出数据时则是front下标增加。
使用数组模拟队列:
/**
* @Author shy Boy
* @Date 2022/3/6 - 03 - 06 - 20:21
* @Description: com.hy.datastructures.guigu.queue
* @version: 1.0
*/
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.getAll();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
arrayQueue.add(value);
break;
case 'g':
try {
Integer integer = arrayQueue.get();
System.out.println("取出的数据是" + integer);
}catch (Exception e){
e.printStackTrace();
}
break;
case 'h':
try {
int res = arrayQueue.headQueue();
System.out.println("取出的头数据是" + res);
}catch (Exception e){
e.printStackTrace();
}
break;
case 'e':
scanner.close();
loop = false;
System.out.println("程序退出");
break;
default:
break;
}
}
}
}
class ArrayQueue{
private int maxSize; // 队列的最大容量
private int front; // 队列头
private int rear; // 队列尾部
private int[] arr; // 用于存放队列数据,模拟队列
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[maxSize];
this.front = -1; // 指向队列头部
this.rear = -1; // 指向队列尾部
}
public Boolean isFull(){
return (rear == maxSize-1);
}
// 判断队列是否为空
public Boolean isEmpty(){
return this.front == this.rear;
}
// 添加数据到队列
public void add(Integer value){
// 判断队列满了没有
if (isFull()){
System.out.println("队列数据已满,不可添加");
return;
}
rear++; // rear后移
arr[rear] = value;
}
// 获取队列的数据 出队列
public Integer get(){
// 队列为空抛出异常
if (isEmpty()){
throw new RuntimeException("Queue is null!");
}
front++;
return arr[front];
}
// 获取队列所有数据
public void getAll(){
if (isEmpty()){
System.out.println("is null!");
}
for (int list : arr){
System.out.println(list);
}
}
// 显示队列头的数据
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("is null!");
}
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.hy.datastructures.guigu.queue;
import java.util.Scanner;
/**
* @Author shy Boy
* @Date 2022/3/8 - 03 - 08 - 19:32
* @Description: com.hy.datastructures.guigu.queue
* @version: 1.0
*/
public class CircleArrayDemo {
public static void main(String[] args) {
System.out.println("测试数组模拟环形队列");
CircleArray arrayQueue = new CircleArray(5);
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.getAll();
break;
case 'a':
System.out.println("输出一个数");
int value = scanner.nextInt();
arrayQueue.add(value);
break;
case 'g':
try {
Integer integer = arrayQueue.get();
System.out.println("取出的数据是" + integer);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 'h':
try {
int res = arrayQueue.headQueue();
System.out.println("取出的头数据是" + res);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 'e':
scanner.close();
loop = false;
System.out.println("程序退出");
break;
default:
break;
}
}
}
}
class CircleArray {
private int maxSize; // 队列的最大容量
private int front; // 队列头
private int rear; // 队列尾部
private int[] arr; // 用于存放队列数据,模拟队列
public CircleArray(int maxSize) {
this.maxSize = maxSize;
this.arr = new int[maxSize];
}
// 判断队列是否满
public Boolean isFull() {
boolean b = (rear + 1) % maxSize == front;
return b;
}
// 判断队列是否为空
public Boolean isEmpty() {
return front == rear;
}
// 添加数据到队列
public void add(Integer value) {
// 判断队列满了没有
if (isFull()) {
System.out.println("队列数据已满,不可添加");
return;
}
arr[rear] = value;
// 将rear后移,这里必须考虑取模
rear = (this.rear + 1) % maxSize;
}
// 获取队列的数据 出队列
public Integer get() {
// 队列为空抛出异常
if (isEmpty()) {
throw new RuntimeException("Queue is null!");
}
// 这里需要分析front是指向队列的第一个元素
/**
* 1.先把front对于的值保存到一个临时变量
* 2.将front后移,考虑取模
* 3.将临时保存的变量返回
* % 代表的是如果下标移动到5然后最大容量也是5这个时候就等于零也就是下标回到原点
*/
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
// 获取队列所有数据
public void getAll() {
if (isEmpty()) {
System.out.println("is null!");
}
for (int i = 0; i < front + arrAySize(); i++) {
System.out.println(i % maxSize +"," );
}
}
// 求出当前队列的有效个数
public int arrAySize() {
return (rear + maxSize - front) % maxSize;
}
// 显示队列头的数据
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("is null!");
}
return arr[front];
}
}
5、单链表
单链表在内存中结构如下
5.1 单链表查询 添加
总结: 单向链表通过找到next域里面的null移动下标找到下一个节点,每次插入到next域里面时需要把下标后移任何再通过判断找到null值并且放入里面
package com.hy.datastructures.guigu.linkelist;
import lombok.ToString;
/**
* @Author shy Boy
* @Date 2022/3/9 - 03 - 09 - 21:24
* @Description: com.hy.datastructures.guigu.linkelist
* @version: 1.0
*/
public class SingleLikenListDemo {
public static void main(String[] args) {
// 先创建一个节点
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
// 创建链表并且加入数据
SingleLikenList singleLikenList = new SingleLikenList();
singleLikenList.add(heroNode1);
singleLikenList.add(heroNode2);
singleLikenList.add(heroNode3);
singleLikenList.add(heroNode4);
singleLikenList.list();
}
}
// 定义SingleLikenList 管理我们的英雄
class SingleLikenList {
// 先初始化一个头节点,头节点不要动,头节点不存放任何数据
private HeroNode head = new HeroNode(0, "", "");
/**
* @param heroNode 当不考虑编号顺序时
* 1.找到这个链表的最后节点
* 2.将最后的节点指向新的节点
*/
public void add(HeroNode heroNode) {
// 因为head节点不能动,因此我们需要一个辅助变量 temp
HeroNode temp = head;
// 遍历链表找到最后
while (true) {
// 因为链表最后一个节点为null 所以找到null就是链表最后一个节点
if (temp.next == null) {
break;
}
// 如果没有找到最后一个节点null,就将temp后移
temp = temp.next;
}
// 当退出了while循环时,temp就指向了链表最后
// 将最后这个节点的next指向了新的节点
temp.next = heroNode;
}
// 显示链表
public void list() {
// 判断链表是否为空
if (head.next == null) {
System.out.println("linkedList is null");
return;
}
// 因为头节点不能为空 因此需要一个辅助变量来遍历
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
// 输出节点信息
System.out.println(temp);
// 将next后移,不然是一个死循环
temp = temp.next;
}
}
}
// 定义HeroNode,每个HeroNode 对象就是一个节点
@ToString
class HeroNode {
public int no;
public String name;
public String nickName;
public HeroNode next; // 指向下一个节点
public HeroNode(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
}
总结:在加入时判断HeroNode.no的值如果等于则提示不能加入,如果当前插入值小于当前下标的值则把两个值的位置互换则实现了排序
/**
* 第二种方式添加英雄时,根据英雄排名插入到指定位置
* 如果以及存在这个排名则添加失败 并给出提升
*
* @param heroNode 英雄信息
*/
public void addByOrder(HeroNode heroNode) {
// 因为头节点不能动 所以要根据一个辅助变量来帮助找到添加的位置
// 我们找到temp时位于添加的前一个节点,否则添加不了
HeroNode temp = this.head;
boolean flag = false; // 标识添加的编号是否存在,默认存在
while (true) {
if (temp.next == null) { // 如果next等于空 ,说明就在链表的最后
break;
}
if (temp.next.no > heroNode.no) { // 如果temp.next.no大于heroNode.no说明要插入的值就在上一个值的中间
break;
} else if (temp.next.no == heroNode.no) { // 如果相等说明编号已经存在
flag = true; // 说明编号存在
break;
}
temp = temp.next; // 后移next ,等于再遍历当前的链表
}
// 判断flag的值
if (flag == true) { // 编号存在
System.out.println("插入英雄编" + heroNode.no + "已经存在");
return;
} else {
// 插入到链表中
int i = 0;
System.out.println(i++);
heroNode.next = temp.next;
temp.next = heroNode;
}
}
public static void main(String[] args) {
// 先创建一个节点
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "卢", "麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
HeroNode heroNode5 = new HeroNode(2, "卢俊义", "玉麒麟");
// 创建链表并且加入数据
SingleLikenList singleLikenList = new SingleLikenList();
singleLikenList.add(heroNode1);
singleLikenList.add(heroNode2);
singleLikenList.add(heroNode3);
singleLikenList.add(heroNode4);
singleLikenList.update(heroNode5);
//
// singleLikenList.addByOrder(heroNode1);
// singleLikenList.addByOrder(heroNode1);
// singleLikenList.addByOrder(heroNode4);
// singleLikenList.addByOrder(heroNode3);
// singleLikenList.addByOrder(heroNode2);
// singleLikenList.list();
}
5.2单链表修改
// 修改链表
public void update(HeroNode heroNode) {
// 判断是否空
if (head.next == null) {
System.out.println("链表为空");
return;
}
// 找到需要修改的节点,根据no编号
// 定义一个辅助变量
HeroNode temp = head.next;
boolean flag = false; // 表示是否找到该节点
while (true) {
if (temp == null) {
break; // 已经遍历完链表
}
if (temp.no == heroNode.no) {
// 找到位置
flag = true;
break;
}
temp = temp.next;
}
// 根据flag判断找到要修改的节点
if (flag) {
temp.name = heroNode.name;
temp.nickName = heroNode.nickName;
} else { // 没有找到
System.out.println("没有找到这个编号的节点");
}
}
5.3单链表删除
public boolean deleteNode(Integer nodeId) {
if (nodeId == null) {
System.out.println("链表不能为空");
}
HeroNode temp = head.next;
Boolean flag = true;
while (flag) {
if (temp.next == null) { //链表已经遍历完毕 结束代码
break;
}
if (temp.no == nodeId) { // 找到节点
flag = false;
break;
}
temp = temp.next; // temp后移
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.println("没有找到要删除的节点");
}
return flag;
}
删除链表的主要代码是temp.next = temp.next.next; 移动到下一个链表的节点并且赋值给当前节点完成删除操作,当没有引用对象时GC会帮我们回收垃圾。
5.4统计链表个数
/**
* 统计链表的个数 不计算头节点
* @param heroNode
* @return
*/
public static int getLength(HeroNode heroNode){
if (heroNode == null) {
return 0;
}
int length = 0;
HeroNode cur = heroNode.next;
while (cur != null) {
length++;
cur = cur.next;
}
return length;
}
5.5得到链表倒数的值
/**
* 得到链表倒数的值
* @param head
* @param index
* @return
*/
public HeroNode findLastIndexNode(HeroNode head,int index) {
// 链表为空返回null
if (head.next == null) {
return null;
}
int size = getLength(head);
// 如果当前下标等于0或者大于总数则返回null
if (index <= 0 ||index > size) {
return null;
}
// 定义辅助变量
HeroNode cur = head.next;
// 总数减去当前下标倒数的数量等于遍历的次数 10 - 1 得9 遍历九次后得到就是倒数第一个下标
for (int i = 0; i < size - index; i++) {
cur = cur.next;
}
return cur;
}
size - index 得到要遍历倒数第x的下标位置
5.6链表反转
/**
* 单链表反转
* @param heroNode
*/
public static void reverseList(HeroNode heroNode){
// 如果当前链表为空或者只有一个则之间返回
if (heroNode.next == null || heroNode.next.next == null) {
return;
}
HeroNode cur = heroNode.next;
HeroNode next = null; // 指向当前cur的下一个节点
HeroNode reverseHead = new HeroNode(0,"","");
while (cur != null){
next = cur.next; //暂时保存当前节点的下一个节点
cur.next = reverseHead.next; // 将cur的下一个节点指向链表的最前端
reverseHead.next = cur;
cur = next; // 让后移
}
// 将head.next指向 reverseHead.next,实现单链表的反转
heroNode.next = reverseHead.next;
}
5.7反向打印输出链表(不能破坏链表的结构)
栈的使用
public class StackDemo {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.add("tom"); // 入栈
stack.add("fancy");
stack.add("smith");
while (stack.size() > 0) {
System.out.println(stack.pop()); // 出栈 特点先进后出
}
}
}
使用栈先进后出的特性反向打印链表并不破坏本身的结构
/**
* 使用栈的先进后出特性反向打印单向链表
*
* @param heroNode
*/
public static void reversePrint(HeroNode heroNode) {
if (heroNode.next == null) {
return;
}
Stack<HeroNode> stack = new Stack<>();
HeroNode cur = heroNode.next;
while (cur != null) {
stack.add(cur);
cur = cur.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
7、栈
1、栈的特点:先进后出(先进去后出来),就像弹夹一样先装弹最后打出来
2、栈(stack)是限制线性表中元素插入和删除只能在线性表同一端进行的特殊线性表。允许插入和删除的一端为可变化的一端,成为栈顶(top),另一端为固定的一端,称为栈底(Bottom)
3、出栈为pop,入栈为push
package com.hy.demoex.stack;
/**
* @author yangdongyong
* @version 1.0
* @date 2022/3/21 10:28
*/
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack();
arrayStack.push(10);
arrayStack.push(20);
arrayStack.push(30);
arrayStack.list();
}
}
class ArrayStack {
private int stackSize = 16;
private int maxSize; // 栈的大小
private int [] stack;
private int top = -1; // top表示栈顶,初始化为-1
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
// 初始化数组大小
stack = new int[this.maxSize];
}
public ArrayStack() {
this.maxSize = this.stackSize;
// 初始化数组大小
stack = new int[this.maxSize];
}
// 栈满
public Boolean isFull() {
return top == maxSize -1;
}
// 栈空
public Boolean isEmpty() {
return top == -1;
}
// 入栈
public void push(int values) {
if (isFull()) {
System.out.println("stack isFull!");
}
top++;
stack[top] = values;
}
// 出栈
public Integer pop() {
if (isEmpty()) {
throw new RuntimeException("栈内数据为空");
}
int value = stack[top];
top--;
return value;
}
// 遍历栈 需要从栈底拿数据
public void list() {
if (isEmpty()) {
throw new RuntimeException("栈内数据为空");
}
// 从栈顶开始遍历获取数据
for (int i = top; i <= 0 ; i--) {
System.out.println("data:" + stack[i]);
}
}
}