【数据结构与算法】单向链表的超详细底层源码,手工实现

目录

一、链表类简介:(王者英雄类)

1、HeroNode类属性:

2、HeroNodeLinkedList类方法:

二:代码实现:

1、  创建HerNode类

2、创建HeroNodeLinkedList类,实现两种添加方法

3、实现遍历操作

5、实现删除操作

6、实现链表的翻转

7、实现链表的逆序打印


一、链表类简介:(王者英雄类)

1、HeroNode类属性:

             作为演示,属性、方法权限设置为public

                        编号:id;

                        英雄名:name;

                        昵称:otherName;

                        下一个结点:next;

2、HeroNodeLinkedList类方法:

              增、删、改、查、翻转、倒序打印

二:代码实现:

1、  创建HerNode类

public class HeroNode{
public int id;//英雄编号
public String name;//英雄名称
public String otherName;//昵称
public HeroNode next;//结点指针

//创建一个有参构造
public HeroNode(int id,String name,String otherName){
this.id = id;
this.name = name;
this.otherName = otherName;
}

//重写一个toString方法
public String toString(){
return "id="+id+";name="+name+";otherName="+otherName;
}
}

2、创建HeroNodeLinkedList类,实现两种添加方法

class HeroNodeLinkedList{
//初始化头结点,头结点仅作为头,不存放数据
HeroNode head = new HeroNode (0,"","");

    //add方法  按添加顺序添加
    public void add(HeroNode hero){
//作为头结点,不能移动,需要创建一个临时结点,代替头结点移动
        HeroNode temp = head;
        while(true){
            if(temp.next==null){ //找到最后一个结点
                temp.next = hero;
                break;
            }
            temp = temp.next;//否则指针后移
        }
    }





  //addByNum 方法,按照id编号顺序来添加
    public void  addByNum(HeroNode hero){
        HeroNode temp = head;
//遍历
        while(true){
            if(temp.next == null){
                //hero.next=temp;
                temp.next = hero;
                break;
            }
            if(temp.next.no>hero.no ){
                hero.next = temp.next;
                temp.next = hero;
                break;
            }else if(temp.next.no==hero.no){
                System.out.println("英雄编号:"+hero.no+"的英雄已存在,无法重复添加");
                break;
            }
            temp = temp.next;
        }
    }
}

3、实现遍历操作

 public void list(){
//创建一个临时指针 temp
        HeroNode temp = head;
//判断链表是否为空
        if(head.next == null){
            System.out.println("链表为空!");
            return;
        }
        while(true){
            if(temp.next == null){
                break;
            }
            System.out.println(temp.next);
//指针后移
            temp=temp.next;
        }
    }

 4、实现修改操作

    public void set(HeroNode hero){
        HeroNode temp = head.next;
        //链表是否为空
        if(head.next == null){
            System.out.println("链表为空,无法修改!");
            return;
        }
        while(true){
            if(temp.next == null){
                System.out.println("当前英雄不在链表中,无法修改!");
                return;
            }
            if(temp.id == hero.id){
                //找到了
                temp.name = hero.name;
                temp.otherName = hero.otherName;
                System.out.println("修改成功!");
                break;
            }
            temp = temp.next;
        }
    }

5、实现删除操作

    public void remove(int id){
//创建临时结点
        HeroNode temp = head;
//判断链表是否为空
        if(head.next==null){
            System.out.println("链表为空,无法执行删除操作!");
            return;
        }

        while(true){
            if(temp.next==null){
                System.out.println("您要删除的编号为:"+id+"的英雄不在列表中,无法执行删除操作!");
                return;
            }
            if(temp.next.id == id){
                temp.next=temp.next.next;
                System.out.print("删除成功!");
                break;
            }
            temp=temp.next;
        }

    }

6、实现链表的翻转

public void reversetList(){
//判断链表是否只有一个元素或者为空链表
        if(head.next == null || head.next.next == null){
            return;
        }
//创建一个临时指针
        HeroNode temp = head.next;
//创建一个新链表
        HeroNode rehead = new HeroNode(0,"","");
        HeroNode renext=null;
        while(true){
            if(temp==null){
                break;
            }
            //  将当前指针的下一个结点存给临时变量renext;
            renext = temp.next;
            // 让新链表的前一个结点指向就链表的next结点
            temp.next = rehead.next;
            rehead.next=temp;
//指针后移
            temp = renext;
        }
        head.next = rehead.next;
    }

7、实现链表的逆序打印

 public void reversePrint(){
//创建一个临时结点
        HeroNode temp = head.next;
        if(head.next == null){
            return;
        }
        //创建栈
        Stack<HeroNode> stack = new Stack<>();
        while(true){
            if(temp == null){
                break;
            }
            //将元素压入栈中
            stack.push(temp);
            //指针后移
            temp = temp.next;
        }

        while(stack.size()>0){
            //弹栈
            System.out.print(stack.pop());
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值