单链表的增删改查

单链表的增删改查

package linkedList;

public class SingleLinkedListdemo {
    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();

//        //直接添加
//        singleLinkedList.add(new HeroNode(4, "dd", "ddd"));
//        singleLinkedList.add(new HeroNode(1, "aa", "aaa"));
//        singleLinkedList.add(new HeroNode(3, "cc", "ccc"));
//        singleLinkedList.add(new HeroNode(2, "bb", "bbb"));
        //按照编号顺序添加
        singleLinkedList.addByNo(new HeroNode(4, "dd", "ddd"));
        singleLinkedList.addByNo(new HeroNode(1, "aa", "aaa"));
        singleLinkedList.addByNo(new HeroNode(3, "cc", "ccc"));
        singleLinkedList.addByNo(new HeroNode(2, "bb", "bbb"));
        //修改3号节点
        singleLinkedList.updateByNo(new HeroNode(3, "cc", "cccupdate"));
        singleLinkedList.show();
        System.out.println("*******************************");
        //删除3号节点
        singleLinkedList.delete(3);
        singleLinkedList.show();
    }
}

//定义一个SingleLinkedList管理
class SingleLinkedList {
    //初始化头节点,不存放具体数据
    private HeroNode head = new HeroNode(0, "", "");

    //添加节点到单项链表
    public void add(HeroNode heroNode) {
        //需要一个辅助节点代替head节点移动
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //退出while循环时,temp指向链表的最后一个节点
        temp.next = heroNode;
    }

    //根据编号顺序添加
    public void addByNo(HeroNode heroNode) {
        HeroNode temp = head;
        boolean flag = false;//判断插入的编号是否存在
        while (true) {
            //temp已经到链表最后
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                break;
            }
            if (temp.next.no == heroNode.no) {
                flag = true;
            }
            temp = temp.next;
        }
        if (flag == true) {
            //不能添加,编号存在
            System.out.println("准备插入的数据的编号:" + heroNode.no + "已经存在");
            return;
        }
        heroNode.next = temp.next;
        temp.next = heroNode;
    }

    //单链表的修改(根据编号来修改)
    public void updateByNo(HeroNode heroNode) {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //找到需要修改的节点
        HeroNode temp = head.next;
        boolean isFind = false;//表示是否找到该节点
        while (true) {
            if (temp == null) {
                break;//表示已经遍历完链表
            }
            if (temp.no == heroNode.no) {
                isFind = true;
                break;
            }
            temp = temp.next;
        }
        if (isFind) {
            temp.no = heroNode.no;
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        } else {
            System.out.println("没有找到该编号的节点");
        }
    }

    //根据编号删除节点
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("该链表为空");
        }
        HeroNode temp = head;
        boolean isFind = false;//判断是否找到该节点
        while (true) {
            if (temp.next == null) {
                System.out.println("未找到指定节点");
                break;
            }
            if (temp.next.no == no) {
                isFind = true;
                break;
            }
            temp = temp.next;
        }
        if (isFind) {
            temp.next = temp.next.next;
        } else {
            System.out.println("未找到指定节点");
        }
    }

    //显示链表
    public void show() {
        if (head.next == null) {
            System.out.println("链表为空");
        }
        HeroNode temp = head.next;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

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 + '\'' + "}";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值