1. 程式人生 > >hw3打卡

hw3打卡

出錯 數組 pre length ima slist alt splay win

PART I

smoosh多用幾個循環就可以了,創建一個新的數組用於臨時存儲。

技術分享
public static void smoosh(int[] ints) {
    int[] reads= new int[ints.length];
    for (int i=0;i<ints.length;i++) {
        reads[i]=-1;
    }
    reads[0]=ints[0];
    int j=1;
    for (int i=1;i<ints.length;i++) {
        if(ints[i-1]!=ints[i]) {
            reads[j]
=ints[i]; j++; } } for(int i=0;i<ints.length;i++) { ints[i]=reads[i]; } }
View Code

技術分享

PART II

之前沒有加size>0的判斷條件,結果最後一項list9的squish總是報錯,因為它是空的,head本身就是null,不存在null.next,所以有currentNode.next的判斷一定會出錯。

技術分享
public void squish() {
      SListNode currentNode=head;
      
if(size>0) { while(currentNode.next!=null) { if(currentNode.item.equals(currentNode.next.item)) { currentNode.next=currentNode.next.next; size--;} else currentNode=currentNode.next; } } }
View Code

技術分享

PART III

技術分享
 public
void twin() { SListNode currentNode=head; while(currentNode!=null) { currentNode.next=new SListNode(currentNode.item,currentNode.next);; currentNode=currentNode.next.next; size=size*2; } }
View Code

技術分享

hw3打卡