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

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

🔗
pigeyes 2015-7-29 18:06:19 | 只看该作者
全局:
ilyak 发表于 2015-6-25 16:19
求帮助啊啊啊啊啊啊,一个问题是square的destructive iteratively里面,最后需要return L么,我觉得这个时 ...

Destructive的話,用作void也是合理的,課程網頁上的readings裡面有說到。
另外,在destructive的method執行完後,L.tail確實是指向null, 但是注意這個L是copy原來我們呼叫這個方法給的parameter的地址,因此並沒有直接改變原來的指標。你可以寫一個test試看看,原來的指標還是在開頭的位置。
回复

使用道具 举报

🔗
HNAKXR 2016-1-28 15:56:25 | 只看该作者
全局:
2 Squaring a List

  1. public static IntList SquareDestructive(IntList L) {
  2.         IntList p = L;
  3.         while (p != null) {
  4.                 p.head = p.head * p.head;
  5.                 p = p.tail;
  6.         }
  7.         return p;
  8. }

  9. public static IntList SquareNonDestructive(IntList L) {
  10.         if (L == null) {
  11.                 return null;
  12.         } else {
  13.                 IntList tail = SquareNonDestructive(L.tail);
  14.                 IntList p = new IntList(L.head * L.head, tail);
  15.         }
  16.         return p;
  17. }
复制代码


3 Reversing Linked Lists

  1. public static IntList reverseNonDestructive(IntList lst) {
  2.         IntList p = null;
  3.         if (lst == null) {
  4.                 return null;
  5.         }
  6.         while (lst != null) {
  7.                 IntList p = new IntList(lst.head, p);
  8.                 lst = lst.tail;
  9.         }
  10.         return p;
  11. }
复制代码

评分

参与人数 1大米 +20 收起 理由
zzwcsong + 20

查看全部评分

回复

使用道具 举报

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

本版积分规则

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