Java实现单链表基础操作
作者:缘分锝天空(李延胜) 时间:2021-08-07 13:46:04
关于链表
链表是有序的列表链表是以节点的方式来存储每个节点包含data域,next域(指向下一个节点)分带头节点的链表和没有头节点的链表
定义一个节点:
package linkedQueue;
public 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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
定义一个链表:
实现以下功能:
添加节点
按序添加节点
删除节点
修改节点
遍历节点
反向打印节点
链表反转
统计节点个数
打印倒数第n个节点
程序:
package linkedQueue;
import java.util.Stack;
public class SingleLinkedList {
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return head;
}
//反向打印节点
public static void reversePrint(HeroNode head) {
if (head.next == null) {
return;
}
// 创建一个栈
Stack<HeroNode> heroNodes = new Stack<>();
HeroNode cur = head.next;
while (cur != null) {
heroNodes.push(cur); //入栈
cur = cur.next;
}
// 出栈打印
while (heroNodes.size() > 0) {
System.out.println(heroNodes.pop());
}
}
/**
* 添加节点
* @param heroNode
*/
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
/**
* 有序添加节点
* @param herNode
*/
public void addByOrder(HeroNode herNode) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > herNode.no) {
break;
} else if (temp.next.no==herNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
System.out.println("你要插入的节点的编号已经存在!!!!");
} else {
herNode.next = temp.next;
temp.next = herNode;
}
}
/**
* 更新节点数据
* @param newHeroNode
*/
public void update(HeroNode newHeroNode) {
if (head.next == null) {
System.out.println("链表为空!!!!");
return;
}
HeroNode temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.no == newHeroNode.no) {
// 找到
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
} else {
// 没有找到
System.out.println("没有找到编号" + newHeroNode.no + "的节点,不能修改");
}
}
/**
* 刪除节点
* @param no
*/
public void del(int no) {
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;
}
}
/**
* 遍历节点
*/
public void showList() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
/**
* 统计有效节点的个数
*/
public static int getLength(HeroNode head) {
if (head.next == null) { //空链表
return 0;
}
int length = 0;
HeroNode herNode = head.next;
while (herNode != null) {
length++; // 计数
herNode=herNode.next; // 指针后移
}
return length;
}
/**
* 求倒数第index个节点
* @param head 头节点
* @param index 倒数第k个
* @return
*/
public static HeroNode findLastIndexNode(HeroNode head, int index) {
// 判断是否是空链表
if (head.next == null) {
return null;
}
// 拿到链表的长度
int size = getLength(head);
// index校验,看是否在范围内
if (index <= 0 || index > size) {
return null;
}
// 定位倒数第index个节点
HeroNode herNode = head.next;
for (int i = 0; i < size - index; i++) {
herNode = herNode.next;
}
return herNode;
}
//单链表反转
public static void reverseList(HeroNode head) {
// 如果当前链表为空或者只有一个节点,直接返回
if (head.next == null || head.next.next == null) {
return;
}
// 定义辅助变量来遍历链表
HeroNode cur = head.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链接到新的链表
cur = next;
}
head.next = reverseHead.next;
}
}
来源:https://blog.csdn.net/weixin_44107140/article/details/122831673
标签:java,单链表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java中switch的新特性
2023-11-24 23:41:54
![](https://img.aspxhome.com/file/2023/4/60154_0s.webp)
利用Spring Boot和JPA创建GraphQL API
2023-04-01 07:11:41
![](https://img.aspxhome.com/file/2023/7/69917_0s.png)
Java深入讲解异常处理try catch的使用
2023-11-04 13:00:32
![](https://img.aspxhome.com/file/2023/1/58921_0s.png)
Java实现根据模板自动生成新的PPT
2022-10-22 18:31:31
![](https://img.aspxhome.com/file/2023/7/65797_0s.jpg)
浅谈maven的jar包和war包区别 以及打包方法
2022-07-20 20:14:44
![](https://img.aspxhome.com/file/2023/8/63428_0s.png)
Mybatis初始化知识小结
2023-11-01 13:59:27
![](https://img.aspxhome.com/file/2023/8/58708_0s.png)
深入理解Spring事务原理
2022-01-18 04:16:39
![](https://img.aspxhome.com/file/2023/7/61777_0s.jpg)
Java8中Optional类的使用说明
2023-07-25 13:31:32
![](https://img.aspxhome.com/file/2023/1/57821_0s.png)
MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题
2023-11-25 05:43:49
Java由浅入深讲解继承下
2023-11-25 12:21:26
![](https://img.aspxhome.com/file/2023/2/59742_0s.png)
spring mvc中直接注入的HttpServletRequst安全吗
2021-12-29 07:48:16
![](https://img.aspxhome.com/file/2023/6/63506_0s.png)
Java中的显示锁ReentrantLock使用与原理详解
2021-11-14 07:04:00
![](https://img.aspxhome.com/file/2023/4/69694_0s.jpg)
Spring Security前后分离校验token的实现方法
2023-06-26 17:00:30
![](https://img.aspxhome.com/file/2023/5/60665_0s.png)
SpringBoot使用@ResponseBody返回图片的实现
2023-11-28 04:41:24
![](https://img.aspxhome.com/file/2023/4/60414_0s.jpg)
深入探究如何使用Java编写MapReduce程序
2022-10-17 18:40:04
![](https://img.aspxhome.com/file/2023/7/66047_0s.webp)
Java安全之Tomcat6 Filter内存马问题
2022-11-20 07:29:23
![](https://img.aspxhome.com/file/2023/7/61027_0s.jpg)
Base64编码解码原理及C#编程实例
2022-05-07 03:58:53
![](https://img.aspxhome.com/file/2023/8/70438_0s.png)
详解利用spring-security解决CSRF问题
2023-07-31 14:31:19
Groovy的规则脚本引擎实例解读
2023-07-11 21:24:04
![](https://img.aspxhome.com/file/2023/6/65186_0s.jpg)
JAVA实现社会统一信用代码校验的方法
2023-04-10 08:38:21