初学者CS61B week 2笔记

IntList————

public class IntLIst(){
    public int first;// 声明变量,类型是int
    public IntList rest;//类型是intlist
    public IntList(int f, IntList r){
        First=f;
        rest=r;
        //constructor,初始化变量
    }
    
}

实现size和iterativesize方法

public int size(){
    if (rest == 0){
        return 1;
    }else{
        return 1 + this.rest.size();//recursion—方法里面套用方法,以此循环,this调用成员变量
    }
}
//迭代的size方法
public int iterativeSize(){
    IntList p = this;//this cannot be reassigned in java
    int totalSize = 0;
    while (p != null){//this是关键字并不难用做变量
        totalSize += 1;
        p = p.rest;
    }
    return totalSize;    
}

Write a method get(Int i) that returns the itch item of the list.

public int get(int i){
    if (i == 0){
        return first;
    }
    return rest.get(i - 1);//recursion, always return the first item of the list until the rest is null
}

This method is slow when there are 100000 items in the list.

下面2.2改良IntList为SLList
大概思路个人理解为就是创建一个新的SLList类,然后将更名的IntNode置入类中,构建List时省略了List后面的空链表,便于后续写入add,get等方法,具体实施如下。

//换一下类名称,这里省略了其他的方法
public class IntNode {
    public int item;
    public IntNode next;
    //constructor
    public IntNode(int i, IntNode n) {
        item = i;
        next = n;
    }
}

新的SLList---------

//新的SLList类,隐藏了裸链表
public class SLList(){
//声明first变量(这里还不是很理解为什么要用       
    public IntNode first;     
 //constructor
    public SLList(int x); 
 //初始化first为新的链表而不是之前的int            
        first = new IntNode(x, null); 

    public void addFirst(int x){   
        first = new IntNode(x, first);
}
    public void getFirst(){
// item属性在IntNode里是第一个int,所以这里只要返回列表first的第一个int即可    
        return first.item;
}
    public static void main(String[] args){
/*只有一个int的list,*/
/*need not to think about references*/
        SLList L = new SLList(15);
        L.addFirst(10);
        L.addFirst(5);
    	int x = L.getFirst();     
    }    
}

对比以上main方法,在IntList中实现如下

//从后往前的链表
IntList L = new IntLIst(15, null);
L = new IntLIst(10, L);
L = new IntList(5, L);
int x = L.first;

下面来做一下exercise
Exercise 2.2.1: The curious reader might object and say that the IntList would be just as easy to use if we simply wrote an addFirst method. Try to write an addFirst method to the IntList class. You’ll find that the resulting method is tricky as well as inefficient.

    public void addFirst(int x){
		first = x;
		rest = new IntList(first, rest);
}

不确定是不是最优解, 但是运行结果是没问题的。
这里inefficient的点可能在于方法内同时改变了变量first和rest并且创建了新的链表。
Improvement #4: Nested Classes 嵌套类

public class SLList {
       public class IntNode {
            public int item;
            public IntNode next;
            public IntNode(int i, IntNode n) {
                item = i;
                next = n;
            }
       }

       private IntNode first; 

       public SLList(int x) {
           first = new IntNode(x, null);
       } 
...

这里比较简单,直接在类中嵌套辅助用的类即可。(嵌套类可以声明static,只在其不需要访问SLList的其他方法和变量时)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值