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

[CS61B_Spring 2015] Lab3-Debugging, Doubly-Linked Lists

🔗
HNAKXR 2016-2-1 16:46:45 | 只看该作者
全局:
  1. public class DoubleChain {
  2.        
  3.         private DNode head;
  4.        
  5.         public DoubleChain(double val) {
  6.                 /* your code here. */
  7.                 head = new DNode(null, val, null);
  8.         }

  9.         public DNode getFront() {
  10.                 return head;
  11.         }

  12.         /** Returns the last item in the DoubleChain. */               
  13.         public DNode getBack() {
  14.                 /* your code here */
  15.                 DNode p = head;
  16.                 while (p.next != null) {
  17.                         p = p.next;
  18.                 }
  19.                 return p;
  20.         }
  21.        
  22.         /** Adds D to the front of the DoubleChain. */       
  23.         public void insertFront(double d) {
  24.                 /* your code here */
  25.                 head = new DNode(null, d, head);
  26.         }
  27.        
  28.         /** Adds D to the back of the DoubleChain. */       
  29.         public void insertBack(double d) {
  30.                 /* your code here */
  31.                 DNode p = head;
  32.                 while (p.next != null) {
  33.                         p = p.next;
  34.                 }
  35.                 p = new DNode(p, d, null);
  36.                 p.prev.next = p;
  37.         }
  38.        
  39.         /** Removes the last item in the DoubleChain and returns it.
  40.           * This is an extra challenge problem. */
  41.         public DNode deleteBack() {
  42.                 /* your code here */
  43.                 DNode p = head;
  44.                 while (p.next != null) {
  45.                         p = p.next;
  46.                 }
  47.                 p.prev.next = null;
  48.                 return new DNode(p.prev, p.val, null);
  49.         }
  50.        
  51.         /** Returns a string representation of the DoubleChain.
  52.           * This is an extra challenge problem. */
  53.         public String toString() {
  54.                 /* your code here */               
  55.                 DNode p = head;
  56.                 String result = String.valueOf(p.val);
  57.                 while (p.next != null) {
  58.                         p = p.next;
  59.                         result = result + " " + String.valueOf(p.val);
  60.                 }
  61.                 return result;
  62.         }

  63.         public static class DNode {
  64.                 public DNode prev;
  65.                 public DNode next;
  66.                 public double val;
  67.                
  68.                 private DNode(double val) {
  69.                         this(null, val, null);
  70.                 }
  71.                
  72.                 private DNode(DNode prev, double val, DNode next) {
  73.                         this.prev = prev;
  74.                         this.val = val;
  75.                         this.next = next;
  76.                 }
  77.         }
  78.        
  79. }
复制代码

  1. import static org.junit.Assert.*;

  2. import org.junit.Test;

  3. /** Perform tests of the DoubleChain class
  4. */

  5. public class TestDoubleChain {

  6.     /** Tests the constructor of DoubleChain */
  7.     @Test
  8.     public void testConstructor() {
  9.         DoubleChain d = new DoubleChain(5);
  10.         assertEquals(5,d.getFront().val, 1e-6);
  11.         assertEquals(null, d.getFront().prev);
  12.         assertEquals(null, d.getFront().next);
  13.     }

  14.     /** Tests some basic DoubleChain operations. */
  15.     @Test
  16.     public void testBasicOperations() {
  17.         DoubleChain d = new DoubleChain(5);
  18.         assertEquals(5, d.getFront().val, 1e-11);
  19.         assertEquals(5, d.getBack().val, 1e-11);

  20.         d.insertFront(2);
  21.         d.insertFront(1);
  22.         d.insertBack(7);
  23.         d.insertBack(8);
  24.         System.out.println(d.toString());
  25.         assertEquals(1, d.getFront().val, 1e-11);
  26.         assertEquals(8, d.getBack().val, 1e-11);
  27.     }

  28.     public static void main(String[] args) {
  29.         jh61b.junit.textui.runClasses(TestDoubleChain.class);
  30.     }
  31. }
复制代码

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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