链表的插入 :
1. 先创建 head 节点,遍历链表
2. 通过创建 temp 辅助节点,帮助遍历整个链表
3. 添加每个节点,添加到链表末尾。
//加入链表
public void add(HeroNode heroNode){
// 先遍历得到当前链表的尾结点
HeroNode temp = head;
while (true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
链表的查询(显示整个链表) :
1. 通过 temp 辅助节点,遍历链表
2. 每遍历到的非 null 的节点,输出节点内容
// 遍历链表
public void showLickedList(){
// 先判断当前链表是否为空
if(head.next == null){
System.out.println("当前链表为空~~~");
return;
}
HeroNode temp = head.next;
while (temp != null){
System.out.println(temp);
temp = temp.next;
}
System.out.println();
}
链表的修改:
1. 通过以 heroNode 节点为参数,比较遍历到的节点的 no 属性与要修改节点的 no 属性
2. flag 作为是否找到要修改节点的标志
// 修改链表节点,根据 no 来修改
public void updataNode(HeroNode heroNode){
if(head.next == null){
System.out.println("当前链表为空,无法修改~~");
return;
}
HeroNode temp = head;
boolean flag = false;
while (true){
// 遍历到了尾结点
if(temp.next == null) break;
if (temp.no == heroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.name = heroNode.name;
temp.nickName = heroNode.nickName;
}else{
System.out.printf("未找到编号为 %d 的节点,无法修改", temp.no);
System.out.println();
}
}
链表的删除:
1. 通过以 no 为参数,比较遍历到的节点的 no 属性与 no 参数。
2. flag 作为是否找到要删除节点的标志
// 删除链表的某一个节点
public void deleteNode(int no){
if(head.next == null){
System.out.println("当前链表为空,无法删除~~");
return;
}
HeroNode 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.printf("未找到编号为 %d 的节点,无法删除", no);
}
System.out.println();
}
整体代码:
/*
* 项目名称:ANA
* 文件名称:SingleLickedListDemo.java
* Date:2023/9/23 下午3:08
* Author:yan_Bingo
*/
package Learning;
/**
* @author Yan_Bingo
* @version 1.0
* Create by 2023/9/23 15:08
*/
public class SingleLickedListDemo {
public static void main(String[] args) {
SingleLickedList lickedList = new SingleLickedList();
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
// 加入
// lickedList.add(heroNode1);
// lickedList.add(heroNode2);
// lickedList.add(heroNode3);
// lickedList.add(heroNode4);
// 按编号加入
lickedList.addByorder(heroNode1);
lickedList.addByorder(heroNode4);
lickedList.addByorder(heroNode2);
lickedList.addByorder(heroNode3);
lickedList.showLickedList();
HeroNode newHerNode = new HeroNode(5, "老吴", "智多");
lickedList.updataNode(newHerNode);
System.out.println();
System.out.println("修改后的链表");
lickedList.showLickedList();
lickedList.deleteNode(1);
lickedList.deleteNode(4);
System.out.println("删除后的链表");
lickedList.showLickedList();
lickedList.deleteNode(4);
System.out.println("删除后的链表");
lickedList.showLickedList();
lickedList.queryNode(1);
}
}
class SingleLickedList{
private HeroNode head = new HeroNode(0, "", "");
//加入链表
public void add(HeroNode heroNode){
// 先遍历得到当前链表的尾结点
HeroNode temp = head;
while (true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
// 按照英雄的编号加入链表
public void addByorder(HeroNode heroNode){
HeroNode temp = head;
boolean flag = false; // 标志要加入的英雄编号是否已经存在
while (true){
// 如果已经遍历到了链表的末尾了
if(temp.next == null){
break;
}else if(temp.next.no > heroNode.no){
break;
}else if(temp.next.no == heroNode.no){
flag = true; // 说明要加入的英雄的编号已经存在
break;
}
temp = temp.next;
}
if(flag){
System.out.println("要加入的英雄的编号已经存在~~~");
return;
}else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
// 修改链表节点,根据 no 来修改
public void updataNode(HeroNode heroNode){
if(head.next == null){
System.out.println("当前链表为空,无法修改~~");
return;
}
HeroNode temp = head;
boolean flag = false;
while (true){
// 遍历到了尾结点
if(temp.next == null) break;
if (temp.no == heroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.name = heroNode.name;
temp.nickName = heroNode.nickName;
}else{
System.out.printf("未找到编号为 %d 的节点,无法修改", temp.no);
System.out.println();
}
}
// 删除链表的某一个节点
public void deleteNode(int no){
if(head.next == null){
System.out.println("当前链表为空,无法删除~~");
return;
}
HeroNode 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.printf("未找到编号为 %d 的节点,无法删除", no);
}
System.out.println();
}
// 查询某个节点
public void queryNode(int no){
if (head.next == null){
System.out.println("当前链表为空,无法查询~~~");
return;
}
HeroNode 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){
System.out.println(temp.next);
System.out.println();
}else{
System.out.printf("无法找到编号为 %d 的节点", no);
System.out.println();
}
}
// 遍历链表
public void showLickedList(){
// 先判断当前链表是否为空
if(head.next == null){
System.out.println("当前链表为空~~~");
return;
}
HeroNode temp = head.next;
while (temp != null){
System.out.println(temp);
temp = temp.next;
}
System.out.println();
}
}
class HeroNode{
public int no;
public String name;
public String nickName;
public HeroNode next;
// 构造器
public HeroNode(int hNo, String hName, String hNickname){
this.no = hNo;
this.name = hName;
this.nickName = hNickname;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}