繁体   English   中英

addLast - 双循环链表 - NullPointerException

[英]addLast - doubly circular linkedlist - NullPointerException

如何在双循环链表的末尾插入一个项目? 为什么我运行它时会出现NullPointerException

   public void addLast( String title, double length ){

    if( isEmpty()){
        head = new Episode(title, length, head, head);
      }else{
      Episode last = head.prev;
      Episode new_episode = new Episode(title, length, head, last);

      }
      }

该代码设置新节点的引用,但不更新列表中的现有引用。 修复在评论中指出:

    if( isEmpty()){            // assuming if empty, head == null
        head = new Episode(title, length, head, head);
        head.next = head;      // fix
        head.prev = head;      // fix
    } else {
      Episode last = head.prev;
      Episode new_episode = new Episode(title, length, head, last);
      last.next = new_episode; // fix
      head.prev = new_episode; // fix
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM