Java手写单向链表

一.添加功能

(1)在尾部添加实现思路

从头开始遍历链表,当前节点的next节点为null,说明到最后一个节点了,

cur.next = node就将新节点添加了进去。

(2)有序的添加节点实现思路

因为单向链表是只能向后遍历的,所以在对节点进行比较的时候,需要拿到前一个节点,比较条件是 cur.next.id > node.id ,这个时候跳出循环,然后让 node.next = cur.next , cur.next = node,这样就能将节点添加进去了

二.删除功能

(1)删除尾部的节点

当cur节点的下一个的下一个节点为null的时候,让cur.next = cur.next.next就删除了尾部节点

(2)根据某个id删除

当cur节点的下一个节点id等于node节点id时,可跳出循环,让cur.next = cur.next.next就可以实现了

三.代码实现

class SingleLinkedList {
    private Node head = null;

    public SingleLinkedList() {
        head = new Node(0, null);
    }

    //在尾部添加节点
    public void add1(Node node) {
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //升序添加
    public void add2(Node node){
        Node cur = head;
        boolean flag = false;
        while(cur.next != null){
            if(cur.next.id > node.id){
                break;
            }else if(cur.next.id == node.id){
                flag = true;
                break;
            }
            cur = cur.next;
        }
        if(flag){
            System.out.println("id为:"+node.id+"的节点已经存在了");
        }else {
            node.next = cur.next;
            cur.next = node;
        }
    }

    //从尾部删除节点
    public Node remove1(){
        if(head.next == null){
            throw new RuntimeException("链表为空...");
        }
        Node cur = head;
        while(cur.next.next != null){
            cur = cur.next;
        }
        Node returnNode = cur.next;
        cur.next = cur.next.next;
        return returnNode;
    }

    //指定删除一个节点
    public void remove2(int id){
        if(head.next == null){
            throw new RuntimeException("链表为空...");
        }
        Node cur = head;
        boolean flag = false;
        while(cur.next != null){
            if(cur.next.id == id){
                flag = true;
                break;
            }
            cur = cur.next;
        }
        if(flag){
            cur.next = cur.next.next;
        }else {
            System.out.println("没有找id为"+id+"的节点");
        }
    }

    public void print(){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        Node cur = head.next;
        while(cur != null){
            System.out.println(cur.toString());
            cur = cur.next;
        }
    }

    public void update(Node node){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        Node cur = head.next;
        while(cur != null){
            if(cur.id == node.id){
                cur.name = node.name;
                break;
            }
            cur = cur.next;
        }
    }
}

class Node {
    int id;
    String name;
    Node next;

    public Node(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值