首页 / JAVA / 单链表的全套Java实现
单链表的全套Java实现
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了单链表的全套Java实现,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2784字,纯文字阅读大概需要4分钟。
内容图文
![单链表的全套Java实现](/upload/InfoBanner/zyjiaocheng/595/f8a5a649dfc94aeebce0ad464093b562.jpg)
单链表
单链表的无序插入
单链表的创建直接添加到尾部
public class SinleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode=new HeroNode(1,"a","a");
HeroNode heroNode2=new HeroNode(2,"b","b");
HeroNode heroNode3=new HeroNode(3,"c","c");
HeroNode heroNode4=new HeroNode(4,"d","d");
SinleLinkedList sinleLinkedList=new SinleLinkedList();
sinleLinkedList .add(heroNode);
sinleLinkedList .add(heroNode2);
sinleLinkedList .add(heroNode3);
sinleLinkedList .add(heroNode4);
sinleLinkedList.list();
}
}
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;
}
//重写tostring方法
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
//定义SingleList
class SinleLinkedList{
//初始化
HeroNode head=new HeroNode(0,"","");
//添加节点到单向链表
public void add(HeroNode heroNode){
//因为头结点不能动,因此我们需要一个辅助变量来遍历
HeroNode temp=head;
while (true){
if(temp.next==null){
temp.next=heroNode;
break;
}else {
temp=temp.next;
}
}
}
//显示链表
public void list(){
HeroNode temp=head;
if(head.next==null){
System.out.println("链表为空");
return;
}
//因为头结点不能动,因此我们需要一个辅助变量来遍历
while (temp.next != null) {
System.out.println(temp.next);
temp = temp.next;
}
}
}
单链表的有序插入
思路:
- 找到新添加的节点的位置(通过辅助指针)
- 新的节点的.next=temp.next
- 将temp.next=新的节点
//添加节点到单向链表
public void add2(HeroNode heroNode){
HeroNode temp=head;
boolean flag=false;
while (true){
if(temp.next==null){ //说明temp已经在链表的最后
break;//*****关键,最后一个节点就不需要temp后移了
}
if (heroNode.no<temp.next.no){
break;
}else if(heroNode.no==temp.next.no){
flag=true;
break;
}
temp=temp.next;
}
if(flag){
System.out.println("准备插入的节点编号已存在");
}else {
heroNode.next=temp.next;
temp.next=heroNode;
}
}
单链表的修改
//修改节点信息,根据no编号来修改,即no编号不能改
public void update(HeroNode heroNode){
if(head.next==null){
System.out.println("链表为空");
return;
}
HeroNode temp=head;
boolean flag=false;//判断是否找到
while (temp!=null){
if(temp.no==heroNode.no){
flag=true;
break;
}
temp=temp.next;
}
if(flag){
temp.name=heroNode.name;
temp.nickname =heroNode.nickname;
}
}
单链表的删除
思路:
- 首先找到需要删除的节点的前一个节点temp
- temp.next=temp.next.next
- 被删除的节点,将不会有其他引用指向
public void delete(int no){
if (head.next==null){
System.out.println("链表为空,无法删除节点");
return;
}
HeroNode temp=head;//不能这样写HeroNode temp=head.next;
//这样会导致第一个节点无法删除
boolean flag=false;
while (true){
if(temp.next==null){
break;
}else if(temp.next.no==no){
flag=true;
break;
}
temp=temp.next;
}
if (flag){
temp.next=temp.next.next;
}else {
System.out.println("链表中无该元素,不可删除");
}
}
内容总结
以上是互联网集市为您收集整理的单链表的全套Java实现全部内容,希望文章能够帮你解决单链表的全套Java实现所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。