zoukankan      html  css  js  c++  java
  • CS 61B homework5

    part one two

    提醒注意cast的地方:

    这里如果不cast就用不了newNode

    Comparable cast

    注意sentinel的myList也是null

    注意有很多地方都要用到InvalidNodeException,就在那个函数处catch就好,不要throw到main不然无法继续进行while里面语句。(给自己的tip:左边标号栏error有时候显示和光标处不同,先改鼠标移动出现的光标处错误)

      public void insert(Comparable c) {
        // Your solution here.
          try{                   
          if(NewSet.isEmpty()){
              NewSet.insertBack(c);
          }else{    
              ListNode CurrentNode = NewSet.front();
              while(true){            
                  Comparable CurrentItem = (Comparable) CurrentNode.item();
                  if(c.compareTo(CurrentItem)<0){
                      CurrentNode.insertBefore(c);
                      break;
                  }else if(c.compareTo(CurrentItem)==0){
                      break;
                  }else if(!CurrentNode.next().isValidNode()){
                      CurrentNode.insertAfter(c);
                      break;
                  }
                  CurrentNode=CurrentNode.next();
                  }                   
                  }
              }catch(InvalidNodeException e){System.err.println(e);}
          
      }
    
      /**
       *  union() modifies this Set so that it contains all the elements it
       *  started with, plus all the elements of s.  The Set s is NOT modified.
       *  Make sure that duplicate elements are not created.
       *
       *  Performance:  Must run in O(this.cardinality() + s.cardinality()) time.
       *
       *  Your implementation should NOT copy elements of s or "this", though it
       *  will copy _references_ to the elements of s.  Your implementation will
       *  create new _nodes_ for the elements of s that are added to "this", but
       *  you should reuse the nodes that are already part of "this".
       *
       *  DO NOT MODIFY THE SET s.
       *  DO NOT ATTEMPT TO COPY ELEMENTS; just copy _references_ to them.
       **/
      public void union(Set s) {
        // Your solution here.
          if(s.cardinality()==0){return;}
          else if(this.cardinality()==0){this.NewSet=s.NewSet;this.head=s.head;}
          
          ListNode CurrentSNode = s.NewSet.front();
          ListNode CurrentThisNode = this.NewSet.front();
          try{
          while(CurrentSNode.isValidNode()&&CurrentThisNode.isValidNode()){
              Comparable SItem = (Comparable) CurrentSNode.item();
              Comparable ThisItem = (Comparable) CurrentThisNode.item();
              if(SItem.compareTo(ThisItem)<0){
                  CurrentThisNode.insertBefore(SItem);
                  CurrentSNode=CurrentSNode.next();
              }else if(SItem.compareTo(ThisItem)==0){
                  CurrentSNode=CurrentSNode.next();
    //              CurrentThisNode=CurrentThisNode.next();
              }else if(SItem.compareTo(ThisItem)>0){
                  if(CurrentThisNode.next().isValidNode()){
                  CurrentThisNode=CurrentThisNode.next();
                  }else{
                      CurrentThisNode.insertAfter(SItem);
                      CurrentThisNode=CurrentThisNode.next();
                      CurrentSNode = CurrentSNode.next();
                  }
              }
          }
          }catch(InvalidNodeException e1){System.err.println(e1);}
          
      }
    
      /**
       *  intersect() modifies this Set so that it contains the intersection of
       *  its own elements and the elements of s.  The Set s is NOT modified.
       *
       *  Performance:  Must run in O(this.cardinality() + s.cardinality()) time.
       *
       *  Do not construct any new ListNodes during the execution of intersect.
       *  Reuse the nodes of "this" that will be in the intersection.
       *
       *  DO NOT MODIFY THE SET s.
       *  DO NOT CONSTRUCT ANY NEW NODES.
       *  DO NOT ATTEMPT TO COPY ELEMENTS.
       **/
      public void intersect(Set s) {
        // Your solution here.
          if(s.cardinality()==0){
              this.NewSet=s.NewSet;
              head=s.head;
              }
          if(this.cardinality()==0){return;}
          
          
          ListNode CurrentSNode = s.NewSet.front();
          ListNode CurrentThisNode = this.NewSet.front();
          try{
          while(CurrentSNode.isValidNode()&&CurrentThisNode.isValidNode()){
              Comparable SItem = (Comparable) CurrentSNode.item();
              Comparable ThisItem = (Comparable) CurrentThisNode.item();
              if(SItem.compareTo(ThisItem)==0){
                  CurrentSNode=CurrentSNode.next();
                  CurrentThisNode=CurrentThisNode.next();
              }else if(SItem.compareTo(ThisItem)<0){
                  CurrentSNode=CurrentSNode.next();
              }else{
                  if(!CurrentThisNode.next().isValidNode()){
                      return;
                  }
                  CurrentThisNode=CurrentThisNode.next();
                  CurrentThisNode.prev().remove();
              }
    
          }
          }catch(InvalidNodeException e1){System.err.println(e1);}
      }
    insert+union+intersect
  • 相关阅读:
    python学习之ajax和可视化管理工具
    操作系统-保护模式中的特权级下
    redis 分布式锁的 5个坑,真是又大又深
    数据库之数据表控制语句
    【NoSQL】Consul中服务注册的两种方式
    netstat命令使用方法以及详解
    Dockerfile与Dockerfile实战
    Spring boot+redis实现消息发布与订阅
    怎么寻回位置不可用移动硬盘的数据
    python字符前面u,r,f等含义
  • 原文地址:https://www.cnblogs.com/developerchen/p/7246104.html
Copyright © 2011-2022 走看看