📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: hurricane_e
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61B_Spring 2015] Discussion2 - Diagramming, Iterative vs. Recursive

🔗
baiery 2015-5-30 08:48:55 | 只看该作者
全局:
hurricane_e 发表于 2015-5-30 03:23
你的code有一点点问题,我觉得应该是这样:
public static IntList squareDestructiveRecursive(IntList ...

你是用sublime来compile和查错的嘛?我配置了一下环境,发现各种错误啊。。。而且也不能查语法错误,你是怎么弄的啊?
回复

使用道具 举报

🔗
baiery 2015-5-30 08:50:42 | 只看该作者
全局:
hurricane_e 发表于 2015-5-30 03:23
你的code有一点点问题,我觉得应该是这样:
public static IntList squareDestructiveRecursive(IntList ...

而且我不懂啊,为什么我的不行啊?我的里面也修改了L.head为平方值了啊,还是说我return的东西一定要是一个IntList类是吗?不可以return 方法?
回复

使用道具 举报

🔗
karte_polo 2015-6-2 20:14:19 | 只看该作者
全局:
自己写了个LINKEDLIST 勉勉强强能完成这个LAB吧

最后一个NonDestructive的逆转链表好奇怪- -我在METHOD里面DEBUG出来结果是正确的,跳回MAIN之后链表就变成一个长度只有一的链表了- - 求JAVA 大神指点

import java.util.*;

public class CS61B_LAB02 {
        public static class IntList{
                private class Node{
                        private int Data;
                        Node(int data){
                                this.Data = data;
                        }
                       
                }
                private Node Header;
                private IntList tail;
                public IntList(IntList L1,IntList L2){
                        L1.tail = L2;
                }
                public IntList(int d,IntList nl){
                        this.Header = new Node(d);
                        this.tail = nl;
                }
               
                public void print(){
                        IntList p = this;
                        System.out.println();
                        while (p!=null){
                                System.out.print(" "+p.Header.Data);
                                p = p.tail;
                        }
                       
                }
               
        }
// above is an awkward implementation of LinkedLists
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                //test code below
                IntList L = new IntList(4, null);
                L.print();
                L = new IntList(3, L);
                L.print();
                L = new IntList(2, L);
                L.print();
                L = new IntList(1, L);
                L.print();
                SquareDestructive(L);
                L.print();
                reverseDestructive(L);
                L.print();

        }
        public static IntList SquareDestructive(IntList L) {
                while (L != null){
                        L.Header.Data =L.Header.Data*L.Header.Data;
                        L = L.tail;
                }
                return L;
        }
        public static IntList SquareNonDestructive(IntList L) {
                if (L == null){
                        return L;
                }
                int d = L.Header.Data;
                return new IntList(d*d,SquareNonDestructive(L.tail));
        }
       
        public static IntList reverseNonDestructive(IntList L) {
                IntList N = null;
                while (L!=null){
                        N = new IntList(L.tail,N);
                        L = L.tail;
                }
                return N;
        }
        public static void reverseDestructive(IntList L) {
                IntList tmp = L.tail;
                L.tail = null;
                IntList p;
                while (tmp!=null){
                        p = tmp.tail;
                        tmp.tail = L;
                        L = tmp;
                        tmp = p;               
                }
        }
}
回复

使用道具 举报

🔗
whosays 2015-6-12 04:35:04 | 只看该作者
全局:
/** Solution to Discussion 2, CS 61B 2015Sprin **/

/** Squaring a list **/
public class IntList {
   public int head;
   public IntList tail;
   public IntList(int h, IntList t) {
      head = h;
      tail = t;
   }
   
   // Squaring a list
   // Destructively, iteratively
   public static IntList SquareDestructive(IntList L) {
      while (L != null) {
         L.head = L.head * L.head;
         L = L.tail;
     }
     return L;
   }

   /** Non-destructively squares each element of the given IntList L.
* Don’t modify the given IntList.
* Should be written recursively*/
   public static IntList SquareNonDestructive(IntList L) {
      if (L.tail == null) {
         return new IntList(L.head * L.head, null);
      }
      return new IntList (L.head * L.head, SquareNonDestructive(L.tail));
   }

   // Destructively, recursion
   public static IntList SquareDestructiveRec (IntList L) {
      L.head = L.head * L.head;
      if (L.tail == null) {
         return L;
      }
      L.tail = SquareDestructiveRec(L.tail);
      return L;
   }

   // Nondestructively, iteratively
   public static IntList SquareNonDestructiveIte (IntList L) {
      IntList CopyList = new IntList (L.head, new IntList (L.tail.head, L.tail.tail));
      while (CopyList != null) {
         CopyList.head = CopyList.head * CopyList.head;
         CopyList = CopyList.tail;
      }
      return CopyList;
   }

   // Reversing Linked Lists
   /** Takes in an IntList and non-destructively returns an IntList whose
elements have been reversed.*/
   public static IntList reverseNonDestructive(IntList lst) {
      IntList CopyList = new IntList (lst.head, new IntList (lst.tail.head, lst.tail.tail));
      IntList prev = null;
      while (CopyList != null) {
         IntList temp = CopyList.tail;
         CopyList.tail = prev;
         prev = CopyList;
         CopyList = temp;
      }
      return CopyList;
   }

   
   // Main function needs to be modified to fit the tests
   public static void main(String[] args) {
      
   }
}
回复

使用道具 举报

🔗
a0106660 2015-6-12 19:11:36 | 只看该作者
全局:
我设了个临时指正temp,没有把原来linklist头指针改变,方便写程序验证。bonus 还在思考中。。。

更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
Casualet 2015-6-16 19:37:57 | 只看该作者
全局:
本帖最后由 Casualet 于 2015-6-16 19:43 编辑
a0106660 发表于 2015-6-12 19:11
我设了个临时指正temp,没有把原来linklist头指针改变,方便写程序验证。bonus 还在思考中。。。

回复错地方了,忽略我=_=
回复

使用道具 举报

🔗
Casualet 2015-6-16 19:41:27 | 只看该作者
全局:
本帖最后由 Casualet 于 2015-6-16 19:43 编辑


public class d2{

    public static IntList SquareDestructive(IntList l){
        if(l.tail==null){
             l.head=l.head*l.head;
             return null;
        }  
        l.head=l.head*l.head;
        SquareDestructive(l.tail);
        return l;
    }//recursive

     
    public static IntList SquareDestructiveV2(IntList l){
        IntList L=l;
       while(l.tail!=null){
          l.head=l.head*l.head;
          l=l.tail;
       }  
          l.head=l.head*l.head;
        return L;
    }//iterative

    public static IntList SquareNonDestructive(IntList l){
           if(l.tail==null)
              return new IntList(l.head*l.head,null);
           return new IntList(l.head*l.head,SquareNonDestructive(l.tail));            
           

    }//recursive
   
   public static IntList SquareNonDestructiveV2(IntList l){
         IntList L,next,temp;
         L=new IntList(l.head*l.head,null);
         next=L;              
         while(l.tail!=null){
           l=l.tail;
           temp= new IntList(l.head*l.head,null);
           next.tail=temp;
           next=next.tail;
       }
       //  next.tail=new IntList(l.head*l.head,null);   
        return L;
   }//iterative

   public static IntList reverseNonDestructive(IntList lst){
         IntList cur,next,myCur,myNext=null;
         cur=lst;
         next=cur.tail;
         myCur=new IntList(cur.head,null);
         if(next==null) return myCur;
         while(next!=null){              
          myNext=new IntList(next.head,null);
          myNext.tail=myCur;
          myCur=myNext;
          cur=cur.tail;
          next=next.tail;           
        }

          return myNext;
    }
   
    public static IntList reverseDestructive(IntList lst){
          IntList cur,next,nextTemp;
          cur=lst;
          next=cur.tail;
          cur.tail=null;
          if(next==null) return cur;
          while(next!=null){
            if(next.tail!=null){
                nextTemp=next.tail;
                next.tail=cur;
                cur=next;
                next=nextTemp;   
           }else{

             break;
         }
            
               
       }
          next.tail=cur;
          return next;
              
    }

    public static void printList(IntList l){
       while(l.tail!=null){
            System.out.print(l.head+" ");
            l=l.tail;
       }
            System.out.println(l.head);   
      
    }

    public static void main(String args[]){
                     
          IntList l=new IntList(4,null);
          l=new IntList(3,l);
          l=new IntList(2,l);
          l=new IntList(1,l);   

          printList(l);
          SquareDestructive(l);
          printList(l);
          SquareDestructiveV2(l);
          printList(l);
          IntList L=SquareNonDestructive(l);
          printList(L);
          L=SquareNonDestructive(L);
          printList(L);
          L= reverseNonDestructive(L);
          printList(L);
          L= reverseDestructive(L);
          printList(L);         

}
}

第一题不知道为什么倒了;图片是测试结果,0是因为overflow; V2表示的是bonus版本;

回复

使用道具 举报

🔗
西风吹草 2015-6-19 06:19:12 | 只看该作者
全局:
2a.
        public static IntList SquareDestructive(IntList L){
                IntList M = L;
                while (L != null){
                        L.head = L.head * L.head;
                        L = L.tail;
                }
                return M;
        }

2b.
        public static IntList SquareNonDestructive(IntList L){
                if (L == null){
                        return null;
                }else{
                        return new IntList(L.head * L.head, SquareNonDestructive(L.tail));
                }
        }

3a.
        public static IntList reverseNonDestructive(IntList lst){
                IntList M = new IntList(lst.head, null);
                IntList N = lst.tail;
                int i = 1;
                while (i < lst.size()){
                        M = new IntList(N.head, M);
                        N = N.tail;
                        i += 1;                       
                }
                return M;
        }

3b.
        public static void reverseDestructive(IntList L) {
                int i = 1;
                while (i < L.size()){
                        move0thToNth(L, L.size() - i);
                        i += 1;
                }
    }
       
        public static void exchangeTheFirstTwo(IntList lst){
                int temp = lst.head;
                lst.head = lst.tail.head;
                lst.tail.head = temp;
                //return lst;
        }
       
        public static void move0thToNth(IntList lst, int n){
                if (n != 0){
                        exchangeTheFirstTwo(lst);
                        move0thToNth(lst.tail, n - 1);
                }
        }

最后一题我自己又建了两个函数,方法略显麻烦。
回复

使用道具 举报

🔗
ilyak 2015-6-25 16:19:34 | 只看该作者
全局:
求帮助啊啊啊啊啊啊,一个问题是square的destructive iteratively里面,最后需要return L么,我觉得这个时候应该没有L可以return了,最后一个L=L.tail的时候,L已经是null了?有点混乱
还有一个就是后面reverse的,代码如下
public static IntList reverseNonDestructive(IntList L){
  //IntList N=new IntList();
  Intlist N=null;
  N.tail=new IntList(L.head,null);
  L=L.tail;
  while(L.tail!=null){
    N.tail=new IntList(L.head,N.tail);
    L=L.tail;
  }
  N=N.tail;
  return N;
}


测试的时候永远报错啊永远报错,原因是

non-static variable this cannot be referenced from a static context

是因为new的关系么!!!我已经混乱了什么时候要new什么时候不用了。。。求帮助QWQ
回复

使用道具 举报

🔗
pigeyes 2015-7-29 17:58:38 | 只看该作者
全局:
最後的bonus想到一個覺得不是很有效率的方法....
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表