• CS61b lab3 分享一个bug,足足找了一个多小时,希望各位别入坑哈


    part1:

    写一个测试程序,比较简单就不贴代码啦,运行结果:

    part2:

    改进InserEnd method,我是按照课上讲的把原来的singlyList变成doubleList,在SListNode中多加入一个prev变量,修改后SListNode:

    class SListNode {
      Object item;
      SListNode next;
      SListNode prev; 
      
    
      SListNode(Object obj) {
        item = obj;
        next = null;
        prev=null;
      }
    
     
      SListNode(Object obj, SListNode next,SListNode prev) {
        item = obj;
        this.next = next;
        this.prev=prev;
      }
      public void setNext(SListNode s){
          this.next=s;
      }
      public void setPrev(SListNode s){
          this.prev=s;
      }
    
    }

    之后将原SList中的head变量作为一个

    sentinel,修改后head.next为首项,head.prev为末项,不过在编写SList的构造函数时一开始犯了一个错误:

    错误代码:

    public SList() {
        size = 0;
        head = new SListNode(null,head,head);
      }

    本来想着是初始化的时候就将head的next和prev均指向自己,不过在这里head由于本来就是null,所以在未被创建出来的时候不能将其prev和next指向自己(我暂时是这样理解的,各位要有更好的解释给我说一下哈)由于这个bug还是能通过编译器而且不抛出任何错误,所以最后找了好久才意识到233333

    修改后代码如下:

     public SList() {
        size = 0;
        head = new SListNode(null,null,null);
        head.setPrev(head);
        head.setNext(head);
      }

    修改后的inserFront和insertEnd:

     public void insertFront(Object obj) {
        SListNode node=new SListNode(obj,head.next,head);
        head.next.setPrev(node);
        head.setNext(node);
        size++;
      }
    
      
      public void insertEnd(Object obj) {
        SListNode node=new SListNode(obj,head.prev,head);
        head.prev.setNext(node);
        head.setPrev(node);
        size++;
      }

    其余方法调整下判定条件就行,代码太多就不全贴了,最后运行结果:

  • 相关阅读:
    关于xmlhttprequest的readystate属性的五个状态(转载)
    MySQL在windows下 1045 access denied for user 'root'@'localhost' using password yes 解决办法 (转)
    栈 堆
    代码安全问题
    TSQL 编程规范(摘自网络)
    UCenter 来自网络
    如何调试 asp 程序 摘自: http://hi.baidu.com/artmis_/blog/item/dd859df57c317b7edcc474f0.html
    《大话设计模式》6个原则 转帖
    SliverLight的bug OR Vs2008的bug?
    ifconfig
  • 原文地址:https://www.cnblogs.com/lyz1995/p/7152615.html
Copyright © 2020-2023  润新知