java实现单链表

这篇博客介绍了如何使用Java创建和操作单链表。首先定义了一个Node类,包含数据和next指针。接着,详细阐述了如何添加节点(仅考虑尾部插入)、删除节点(包括删除头结点和非头结点)以及打印链表和获取链表长度的方法。通过示例代码展示了这些操作的实现,并在main方法中进行了测试,验证了功能的正确性。
摘要由CSDN通过智能技术生成

java小白,记录实现单链表操作;

处理步骤
1、定义一个节点

定义一个Node类,包含两部分,data和next
通过构造函数来传值

  class Node{
        //存储真实数据
        int data;
        //指针引用 next 指向下一个节点对象
        Node next;
        //无参构造函数 (通过构造函数传值)
        public Node(int data){
            this.data = data;
        }
    }
2、增加节点

逻辑是先处理头结点,然后通过遍历找到尾节点,给尾节点赋值;
只为实现单链表,其他情况比较麻烦,这里只考虑尾插情况;

//2.1定义一个头结点(必须先有一个头结点)
    Node head;
    //链表的长度
    int size;
    //2.2增加节点,这里只考虑尾插的情况,按顺序在尾部追加数据
    public void addNode(int data){
        //2.2.1头结点的处理
        if (head == null){
            head = new Node(data);
            size++;
            return;
        }
        //2.2.2找到尾节点 遍历尾节点
        Node temp = head;
        while (temp.next != null){
            temp = temp.next;
        }
        //2.2.3找到尾节点,对其赋值
        temp.next = new Node(data);
        size++;
    }
3、删除节点

依据链表的长度size来删除,增加节点就+1,删除节点就-1

删除时要考虑一些特殊情况:空链表,异常索引,删除头结点

 //3.删除节点
    public Node deleteNode(int index){
        //处理空链表
        if (size == 0){
            return null;
        }
        //处理异常索引
        if (index < 1 || index > size){
            return null;
        }
        //删除头结点
        if (index == 1){
            Node temp = head;
            head = head.next;
            size--;
            return  temp;
        }
        //删除非头节点
        //从头结点的下一个节点开始遍历
        Node cureNode = head.next;
        //记录当前循环节点的上一个节点,用于删除当前节点
        Node preNode =head;
        int i = 2;
        while (cureNode != null){
            if (i == index){
                //表明删除的就是此节点
                preNode.next = cureNode.next;
                size--;
                break;
            }else{
                preNode = cureNode;
                cureNode = cureNode.next;
            }
        }
        return cureNode;
    }
4、打印链表,返回链表长度
//4.打印链表
    public void printList(){
        Node cureNode = head;
        //循环遍历到尾节点
        while (cureNode != null){
            System.out.println(cureNode.data + "");
            cureNode = cureNode.next;
        }
    }

    //返回链表长度
    public int getSize(){
        return size;
    }
完整代码
public class LinkedList {
    //1.定义一个节点
    class Node{
        //存储真实数据
        int data;
        //指针引用 next 指向下一个节点对象
        Node next;
        //无参构造函数 (通过构造函数传值)
        public Node(int data){
            this.data = data;
        }
    }

    //2.1定义一个头结点
    Node head;
    //链表的长度
    int size;

    //2.2增加节点,这里只考虑尾插的情况,按顺序在尾部追加数据
    public void addNode(int data){
        //头结点的处理
        if (head == null){
            head = new Node(data);
            size++;
            return;
        }
        //找到尾节点 遍历尾节点
        Node temp = head;
        while (temp.next != null){
            temp = temp.next;
        }
        //找到尾节点,对其赋值
        temp.next = new Node(data);
        size++;
    }
    //3.删除节点
    public Node deleteNode(int index){
        //处理空链表
        if (size == 0){
            return null;
        }
        //处理异常索引
        if (index < 1 || index > size){
            return null;
        }
        //删除头结点
        if (index == 1){
            Node temp = head;
            head = head.next;
            size--;
            return  temp;
        }
        //删除非头节点
        //从头结点的下一个节点开始遍历
        Node cureNode = head.next;
        //记录当前循环节点的上一个节点,用于删除当前节点
        Node preNode =head;
        int i = 2;
        while (cureNode != null){
            if (i == index){
                //表名删除的就是此节点
                preNode.next = cureNode.next;
                size--;
                break;
            }else{
                preNode = cureNode;
                cureNode = cureNode.next;
            }
        }
        return cureNode;
    }

    //4.打印链表
    public void printList(){
        Node cureNode = head;
        //循环遍历到尾节点
        while (cureNode != null){
            System.out.println(cureNode.data + "");
            cureNode = cureNode.next;
        }
    }

    //返回链表长度
    public int getSize(){
        return size;
    }

}
新建main方法测试
public class MyList {
    public static void main(String[] args){
        LinkedList linkedList = new LinkedList();
        //插入节点
        linkedList.addNode(10);
        linkedList.addNode(20);
        linkedList.addNode(30);
        //打印链表并返回链表长度
        linkedList.printList();
        System.out.println(linkedList.getSize());
        
        System.out.println("--------------------------");
        //删除节点2
        linkedList.deleteNode(2);
        //打印链表并返回链表长度
        linkedList.printList();
        System.out.println(linkedList.getSize());
    }
}

执行结果如下

10
20
30
3
--------------------------
10
30
2

完结,撒花

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值