java实现链表

设置节点类

public class SingleLinkedList {


    /**
     * 节点类
     */
    private static class Move{

        //当前节点的值
        int value;

        //下一个节点
        Move next;

        public Move(int value, Move next){
            this.value=value;
            this.next = next;
        }
    }
}

在第一个添加一个节点

/**
 * java实现单链表
 */
public class SingleLinkedList {


    // 头节点
    private Move head = null;

    /**
     * 节点添加到第一个
     * @param value 节点的值
     */
    public void addFirst(int value){

        //判断是否为空链表
       /* if(head != null){
            head = new Move(value, head);
        }
        else {
            //头节点
            head = new Move(value, null);
        }*/
        head = new Move(value, head);    //new Move(value, head); 每次new都会分配一个地址,head不是覆盖关系

       // System.out.println("添加节点的地址:"+head +" 插入值:"+head.value );

    }

}

在链表后添加一个节点

/**
 * java实现单链表
 */
public class SingleLinkedList {


    // 头节点
    private Move head = null;


    /**
     * 节点添加到最后第一个
     * @param value 节点的值
     */
    private void addLast(int value){

        // 插入的节点
        Move interposition = new Move(value,null);

        // 为空链表
        if(head == null){
            head = interposition;
        }
        // 非空链表
        else {
            Move move  = head;  //移动指针
            //有下一个节点
            while (move.next != null){
                move = move.next;
            }
            //插入节点
            move.next = interposition;
        }
    }
}

根据索引寻找节点

 /**
     * 根据索引寻找节点
     * @param index
     * @return
     */
    private Move indexNode(int index){

        int i = 0; //索引
        for(Move move = head; move != null; move = move.next,i++){
            if(index == i){
                return move;
            }
        }
        return null;
    }

全部代码


/**
 * java实现单链表
 */
public class SingleLinkedList {


    // 头节点
    private Move head = null;

    /**
     * 节点类
     */
    private static class Move{

        //当前节点的值
        int value;

        //下一个节点
        Move next;

        public Move(int value, Move next){
            this.value=value;
            this.next = next;
        }
    }


    /**
     * 节点添加到第一个
     * @param value 节点的值
     */
    public void addFirst(int value){

        //判断是否为空链表
       /* if(head != null){
            head = new Move(value, head);
        }
        else {
            //头节点
            head = new Move(value, null);
        }*/
        head = new Move(value, head);    //new Move(value, head); 每次new都会分配一个地址,head不是覆盖关系

       // System.out.println("添加节点的地址:"+head +" 插入值:"+head.value );


    }

    /**
     * 节点添加到最后第一个
     * @param value 节点的值
     */
    private void addLast(int value){

        // 插入的节点
        Move interposition = new Move(value,null);

        // 为空链表
        if(head == null){
            head = interposition;
        }
        // 非空链表
        else {
            Move move  = head;  //移动指针
            //有下一个节点
            while (move.next != null){
                move = move.next;
            }
            //插入节点
            move.next = interposition;
        }
    }

    /**
     * 根据索引寻找节点
     * @param index
     * @return
     */
    private Move indexNode(int index){

        int i = 0; //索引
        for(Move move = head; move != null; move = move.next,i++){
            if(index == i){
                return move;
            }
        }
        return null;
    }

    /**
     * 输出索引后的节点
     */
    private int getValue(int index){
        Move move = indexNode(index);
        if(move == null){

            throw IllegalIndex(index);
        }

        System.out.println("索引"+index+"的节点值为:"+move.value);
        return move.value;
    }

    /**
     * 异常
     * @param index
     * @return
     */
    private IllegalArgumentException IllegalIndex(int index) {
        return new IllegalArgumentException(
                String.format("此索引不合法:index = "+ index)
        );
    }


    /**
     * 插入节点
     * @param index 位置
     * @param value 插入值
     */
    private void addNode(int index, int value){
        if(index == 0){
            head = new Move(value, head);
            return;
        }
        //插入前index-1位置的节点
        Move move = indexNode(index-1);
        if(move == null){
            throw IllegalIndex(index);
        }
        Move interposition = new Move(value, move.next);
        move.next = interposition;
        System.out.println("添加成功");
    }

    /**
     * 删除节点
     * @param index 位置
     */
    private void remove(int index){

        //链表为空
        if(head == null){
            throw IllegalIndex(index);
        }
        //删除节点前的一个节点
        Move move = indexNode(index-1);
        if(move == null){
            throw IllegalIndex(index);
        }
        if(move.next == null){
            throw IllegalIndex(index);
        }
        // 删除节点
        Move removeNode = move.next;
        move.next = removeNode.next;
        // removeNode = null;
        System.out.println("删除成功");
    }


    /**
     * 遍历链表
     */
    private void traversal(){
        //指针
        Move pointer = head;
        while (pointer != null){
            System.out.print(pointer.value+" ");
            pointer = pointer.next;
        }

       /* for(Move move = head; move != null; move = move.next){
            System.out.println(move.value);
        }*/

    }




    public static void main(String[] args) {

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addFirst(1);
        singleLinkedList.addFirst(2);
        singleLinkedList.addFirst(3);
        singleLinkedList.addFirst(4);

        //遍历链表
        singleLinkedList.traversal();
        System.out.println("");

        singleLinkedList.addLast(5);
        singleLinkedList.addLast(6);
        singleLinkedList.addLast(7);
        singleLinkedList.addLast(8);
        singleLinkedList.addLast(9);

        //遍历链表
        singleLinkedList.traversal();
        System.out.println("");

        //根据索引查找节点
        singleLinkedList.getValue(5);


        singleLinkedList.addNode(9,10);

        singleLinkedList.remove(10);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android 《开发》

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值