本章学习目录
一,单链表
- 概念:是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
- 组成:链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
- 分类:有头节点的单链表和没头节点的单链表
- 逻辑结构:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4888414493d6667f74ebee91fe6f00ec.png)
- 解决单链表问题:大多数都要依靠一个自己定义的辅助节点
二,定义节点类
package com.链表;
public class Node{
public int no;
public String name;
public String message;
public Node next;
public Node(int node, String name, String message) {
this.no = node;
this.name = name;
this.message = message;
}
@Override
public String toString() {
return "Node{" +
"node=" + no +
", name='" + name + '\'' +
", message='" + message + '\'' +
'}';
}
}
三,链表的增删改查
package com.链表;
import java.util.Stack;
public class SingleLinkedList {
Node head = new Node(0, "", "");
public Node getHead() {
return head;
}
public void add(Node node) {
Node temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = node;
}
public void printNode() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
Node temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
public void addByOrder(Node node) {
Node temp = head;
while (temp.next != null) {
if (temp.next.no > node.no) {
break;
}
if (node.no == temp.next.no) {
System.out.println("该节点编号已经存在" + temp.next);
return;
}
temp = temp.next;
}
node.next = temp.next;
temp.next = node;
}
public void updata(Node node) {
Node temp = head.next;
boolean flag = false;
while (true){
if (temp==null){
break;
}
if(temp.no==node.no){
flag=true;
break;
}
temp=temp.next;
}
if (flag){
temp.name=node.name;
temp.message=node.message;
}else{
System.out.println(node.no+"没有找到");
}
}
public void del(int no){
Node temp=head;
boolean flag=false;
while (true){
if (temp.next==null){
break;
}
if (temp.next.no==no){
flag=true;
break;
}
temp=temp.next;
}
if (flag){
temp.next=temp.next.next;
}else {
System.out.println(no+"节点不存在");
}
}
}
四,链表的进阶操作
package com.链表;
import java.util.Stack;
public class SingleLinkedList {
Node head = new Node(0, "", "");
public Node getHead() {
return head;
}
public static int nodeLength(Node head) {
if (head.next == null) {
return 0;
}
int length = 0;
Node countNode = head.next;
while (countNode != null) {
length++;
countNode = countNode.next;
}
return length;
}
public static Node findLastIndex(Node head, int lastIndex) {
if (head.next == null) {
return null;
}
Node curNode = head.next;
int length = nodeLength(head);
if (lastIndex > length || lastIndex <= 0) {
return null;
}
for (int i = 1; i <= length - lastIndex; i++) {
curNode = curNode.next;
}
return curNode;
}
public static void reverseNodeList(Node head) {
Node newList = new Node(0, "", "");
Node temp = head.next;
Node tem = null;
while (temp != null) {
tem = temp.next;
temp.next = newList.next;
newList.next = temp;
temp = tem;
}
head.next = newList.next;
}
public static void reversePrint(Node head) {
Node temp = head.next;
Stack<Node> stack = new Stack<>();
while (temp != null) {
stack.add(temp);
temp = temp.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
}
- 注:数据结构的话,建议大家有了一定的java基础,再看看B站尚硅谷的课程,满满干货;当然本人也是学完,自己加深记忆再写一遍的