楼主: jaly50
跳转到指定楼层
上一主题 下一主题
收起左侧

Berkeley CS 61B Data Structures(in Java) Lab4 讨论帖

 
🔗
shi198 2017-11-20 04:35:29 | 只看该作者
全局:
总结一下就是有dummy node 的doubly linked list因为是一个circle 所以不用检验各种null pointer的情况了


回复

使用道具 举报

🔗
dachou 2017-12-9 13:39:54 | 只看该作者
全局:
无sentinel时,insertFront需要考虑链表是否为空,因为为空的时候,insertFront新的node之后tail和head都需要更新。removeFront的时候,需要考虑链表为空,和只有一个node和多个node三种情况。
有sentinel时候,insertFront不需要考虑链表是否为空的情况,因为一直都会有head节点,此时size=0。而removefront的时候需要考虑链表为空的情况,但是不需要考虑size=1的情况。
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
仙道彰 2017-12-25 22:55:24 | 只看该作者
全局:
花了一晚上才搞定了lab4……思路其实很简单,结果在用了sentinel的insertFront方法的几条语句的顺序上面晕了很久
更多图片 小图 大图
组图打开中,请稍候......

评分

参与人数 2大米 +10 收起 理由
wangguoxia + 5 给你点个赞!
LouisWang + 5 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
keepgoing123 2018-1-16 18:15:27 | 只看该作者
全局:
Lab4 对理解链表很有帮助
DList1



DList2


回复

使用道具 举报

🔗
kaiwhu 2018-1-31 15:05:55 | 只看该作者
全局:
Env: JDK 9.0.1


回复

使用道具 举报

🔗
greatlim 2018-2-9 17:15:20 | 只看该作者
全局:
  1. /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=52029:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/Lim/@inbox/cs61b14/lab/lab4/out/production/lab4 DList1
  2. ### TESTING insertFront ###
  3. Empty list is [  ]

  4. Inserting 9 at front.
  5. List with 9 is [  9  ]

  6. Inserting 8 at front.
  7. List with 8 and 9 is [  8  9  ]


  8. ### TESTING removeFront ###
  9. List with 1 and 2 is [  1  2  ]

  10. Removing front node.
  11. List with 2 is [  2  ]

  12. Removing front node.
  13. Empty list is [  ]

  14. Removing front node.
  15. Empty list is [  ]

  16. Process finished with exit code 0
复制代码
  1. /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51938:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/Lim/@inbox/cs61b14/lab/lab4/out/production/lab4 DList2
  2. ### TESTING insertFront ###
  3. Empty list is [  ]

  4. Inserting 9 at front.
  5. List with 9 is [  9  ]

  6. Inserting 8 at front.
  7. List with 8 and 9 is [  8  9  ]


  8. ### TESTING removeFront ###
  9. List with 1 and 2 is [  1  2  ]

  10. List with 2 is [  2  ]

  11. Empty list is [  ]

  12. Empty list is [  ]

  13. Process finished with exit code 0
复制代码

总结下:
(2)Dlist1的remove需要考虑size==1的情况,移除唯一的元素后,head = null, 这时候 head.prev 是不存在的,因为null既不是对象也不是一种类型,它仅是一种特殊的值。同时也必须对tail进行讨论,使得tail = null,不然java可能不能回收这个空间。
(1)Dlist2有了sentinel后可以避免上述的讨论
回复

使用道具 举报

全局:
本帖最后由 爱树上的兔子 于 2018-3-16 20:25 编辑
  1. ### TESTING insertFront ###
  2. Empty list is [  ]

  3. Inserting 9 at front.
  4. List with 9 is [  9  ]

  5. Inserting 8 at front.
  6. List with 8 and 9 is [  8  9  ]


  7. ### TESTING removeFront ###
  8. List with 1 and 2 is [  1  2  ]

  9. Removing front node.
  10. List with 2 is [  2  ]

  11. Removing front node.
  12. Empty list is [  ]

  13. Removing front node.
  14. Empty list is [  ]
复制代码
  1. ### TESTING insertFront ###
  2. Empty list is [  ]

  3. Inserting 9 at front.
  4. List with 9 is [  9  ]

  5. Inserting 8 at front.
  6. List with 8 and 9 is [  8  9  ]


  7. ### TESTING removeFront ###
  8. List with 1 and 2 is [  1  2  ]

  9. List with 2 is [  2  ]

  10. Empty list is [  ]

  11. Empty list is [  ]
复制代码
回复

使用道具 举报

🔗
Jason_Lee 2018-3-22 11:13:44 | 只看该作者
全局:
本帖最后由 Jason_Lee 于 2018-3-22 16:25 编辑

Lab4
把DList1 & DList2放在一个lab里意思就是要对比分析分析吧。。
DList1是在SList上的一个拓展,Node增加了prev instance variable,head和tail的概念仍然没变,只是作为一个标签(变量),用来表征首尾两个node,方便对整个list进行access。
DList2是circularly linked list,存在sentinel,lab中用head作为变量名;与DList1不同,sentinel本身也是一个node处在list中,需要确定其next和prev。

简单来说,在DList1中,head是list中第一个node的变量名;DList2中,head自身是一个node对象,变量名是head。

算法上的区别:
DList2在insertFront和removeFront中都不需要讨论size==1的情况了。

结果图如下:
DList1


DList2



回复

使用道具 举报

🔗
martinma 2018-7-31 17:57:23 | 只看该作者
全局:

回复

使用道具 举报

🔗
desperado721 2018-8-2 00:11:56 | 只看该作者
全局:
DList2弄了好久,在网上找到的答案全部都是分了size=1和size>1的情况,而这是不符合要求的。最后强做,终于搞定了,其实只要两行代码,
1.把head.next指向head后面的后面的节点(head.next.next)
2.head后面的后面的节点的prev(head.next.prev,注意此时head.next已经是head后面的后面那个节点了,因为1已经把head.next重新定向了)指向head就可以了,因为是循环链表,当只有head和1时,head.next.next会指向她自己,结束

运行图

public void insertFront(int i) {
            // Your solution here.
            DListNode2 first = new DListNode2(i);
            head.next.prev = first;
            first.next = head.next;
            head.next = first;
            first.prev = head;
            size++;
        }

       public void removeFront() {
            // Your solution here.
            // finally, I did it
            if (size ==0){
                return;
            }else {
                head.next = head.next.next;
                head.next.prev = head;
                size--;
            }
        }

微信截图_20180802000937.png (18.82 KB, 下载次数: 2)

微信截图_20180802000937.png
回复

使用道具 举报

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

本版积分规则

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