CS 61B homework5
part one two
提醒注意cast的地方:
这里如果不cast就用不了newNode
Comparable cast
注意sentinel的myList也是null
注意有很多地方都要用到InvalidNodeException,就在那个函数处catch就好,不要throw到main不然无法继续进行while里面语句。(给自己的tip:左边标号栏error有时候显示和光标处不同,先改鼠标移动出现的光标处错误)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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);} }