不就是转码 2022-07-06 10:28 采纳率: 100%
浏览 8
已结题

双向链表:找不到修改结点

问题:老是说找不到修改的结点
update02方法不能正常运行

```java
 
```java
package Linkedlist;
 
public class DoubleLinkedListDemo {
    //test area:
    public static void main(String[] args) {
        System.out.println("DLL test:");
        //create nodes
        HeroNode02OfDLL no1 = new HeroNode02OfDLL(1, "q", "q");
        HeroNode02OfDLL no2 = new HeroNode02OfDLL(2, "xx", "aaq");
        HeroNode02OfDLL no3 = new HeroNode02OfDLL(3, "zzq", "qzz");
        //create DLL
        DoubleLinkedList DLLForTest = new DoubleLinkedList();
        DLLForTest.add02(no1);
        DLLForTest.add02(no2);
        DLLForTest.add02(no3);
        DLLForTest.list02();
        //update method
        HeroNode02OfDLL newHeroNode = new HeroNode02OfDLL(2, "xxx", "xxx");
        DLLForTest.update02(newHeroNode);
        System.out.println("update version:");
        DLLForTest.list02();
        //delete method
        DLLForTest.delete02(3);
        System.out.println("deleted version:");
        DLLForTest.list02();
    }
}
class DoubleLinkedList {//create a DLL
    private HeroNode02OfDLL head = new HeroNode02OfDLL(0, "", "");
 
    public HeroNode02OfDLL getHead() {
        return head;
    }
    //1.go through method
    public void list02() {
        if (head.next == null) {
            System.out.println("the DLL is empty!");
            return;
        }
        HeroNode02OfDLL temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
    public void add02(HeroNode02OfDLL heroNode) { //2.add at the last
        HeroNode02OfDLL temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }
    //3.update
    public void update02(HeroNode02OfDLL newHeroNode) {
        if (head.next == null) {
            System.out.println("the DLL is empty!");
            return;
        }
        HeroNode02OfDLL temp = head.next;
        boolean flag = false;//flag: weather find the node
        while (true) {
            if (temp == null) {//finished
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        } else {
            System.out.println("can't find the node :" + newHeroNode + "and can't update");
        }
    }
    public void delete02(int no) {//4.delete method
        if (head.next == null) {
            System.out.println("DLL is empty!");
            return;
        }
        HeroNode02OfDLL temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {//走到最后的最后
                break;
            }
            if (temp.no == no) ;//找到待删除节点,双向链表可以自我删除
            flag = true;
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            if (temp.next != null) {
                temp.next.pre = temp.pre;//如果是最后一个节点就不用执行这一句,否则出现空指针
            }
        } else {
            System.out.printf("the node %d should be delete doesn't exist\n", no);
        }
    }
}
    class HeroNode02OfDLL {
        public int no;
        public String name;
        public String nickname;
        public HeroNode02OfDLL next;//default is null
        public HeroNode02OfDLL pre;//default is null
 
        public HeroNode02OfDLL(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 + "]";
        }
    }

```

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-07-06 10:53
    关注

    update02函数中,你只有把flag赋值为false的语句,并没有flag赋值为true的语句啊,当然会报找不到了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月14日
  • 已采纳回答 7月6日
  • 创建了问题 7月6日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c