账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    写了个Java的单链表,在指定位置插入时出现问题,不知道错在哪
    62
    0

    写了个Java的单链表,在指定位置插入时出现问题,不知道错在哪

    在学数据结构 Java 版的,学到单链表,把自己搞蒙了

    相关代码

    /**

    • 节点
    • @author huaian

    *
    */
    public class Node {

    private Node node;
    // node data
    int data;
    // next node
    Node nextNode;
    // node size
    static int size = 0;
    
    public Node() {
        super();
    }
    
    public Node(int data) {
        super();
        this.node = this;
        this.data = data;
        size++;
    }
    
    /**
     * 追加节点
     * 
     * @param node 待添加的node
     */
    public Node append(Node node) {
        // 获取当前节点
        Node currentNode = this.node;
        while (true) {
            // 获取下一个节点
            Node nextNode = currentNode.nextNode;
            // 判断是否是最后一个节点
            if (nextNode == null) {
                break;
            }
            currentNode = nextNode;
        }
        // 添加节点
        currentNode.nextNode = node;
        size++;
        return this;
    }
    
    /**
     * 往指定位置添加节点
     * @param index
     * @param newNode
     * @return
     */
    public Node add(int index, Node newNode) {
        Node headNode = this.node;
        Node currentNode = headNode;
        Node preNode = null;
        // 索引越界或者当前节点不存在则抛出异常
        if (index > size || currentNode == null || index < 0) {
            System.out.println("size = " + size);
            throw new IndexOutOfBoundsException(index);
        }
        
        int count = 0;
        // 找到待删除节点的前一个节点
        while (!currentNode.isLast() && count < index) {
            preNode = currentNode;
            currentNode = currentNode.nextNode;
            count++;
        }
        if (preNode == null) {
            headNode = newNode;
            headNode.nextNode = currentNode;
            this.nextNode = headNode;
        } else {
            preNode.nextNode = newNode;
            preNode.nextNode.nextNode = headNode;
            this.node = preNode;
        }
        size++;
        return node;
    }
    
    /**
     * 删除指定索引下标的元素
     * @param index 索引
     * @return 新的链表
     */
    public Node remove(int index) {
        Node headNode = this.node;
        Node currentNode = headNode;
        Node preNode = null;
    
        // 索引越界或者当前节点不存在则抛出异常
        if (index > size || index <= 0) {
            throw new IndexOutOfBoundsException(index);
        }
    
        int count = 1;
        // 找到待删除节点的前一个节点
        while (!currentNode.isLast() && count < index) {
            preNode = currentNode;
            currentNode = currentNode.nextNode;
            count++;
        }
        if (preNode == null && index == count) {
            headNode = currentNode.nextNode;
        } else {
            preNode.nextNode = currentNode.nextNode;
        }
        this.node = headNode;
        size--;
        return this.node;
    }
    
    /**
     * 获取下一个节点
     * 
     * @return 下一个节点
     */
    public Node next() {
        return this.nextNode;
    }
    
    /**
     * 获取当前节点的数据
     * 
     * @return 节点数据
     */
    public int getData() {
        return this.data;
    }
    
    /**
     * 判断当前节点是否是最后一个节点
     * 
     * @return 是否为最后一个节点
     */
    public boolean isLast() {
        return nextNode == null;
    }
    
    /**
     * 链表节点数
     * 
     * @return 节点数
     */
    public int size() {
        return size;
    }
    
    /**
     * 判空
     * 
     * @return 是否为空链表
     */
    public boolean isEmpty() {
        return size == 0;
    }

    }

    当单链表中有数据 >= 4条时,使用 add(int index, new Node(5)) 插入节点时候时候,位置不对,其他数字不存在问题。求解答。

    3
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 逍遥人世欢 普通会员 1楼

      首先,我需要知道你在做什么。你是在Java中写了一个单链表,但是在插入指定位置时出现问题。你能提供一些更多的信息吗?比如,你在插入位置的代码,以及链表的定义和操作。这样我才能更好地帮助你解决问题。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部