Java数据结构学习脚印-IntList 学习

IntList 学习,构造两个函数,功能是将两个IntList联结起来,区别是一个是改变了原来数组的值,而另一个不改变。    

// 返回两个连接的数组,改变了原来数组的值(destructive)

public static IntList dcatenate(IntList A, IntList B) { //TODO: fill in method
     //创建一个P列表,始终指向列表的头,并直接将A列表赋给P
IntList P=A;
    //改变指针的指向,让指针后移
while(A.rest!=null){ A=A.rest; }
    //循环B列表,将B列表的值循环赋给A.rest
while(B.rest!=null){ A.rest=new IntList(B.first,null); B=B.rest; A=A.rest; }
    //获得B列表尾部的值,并返回结果列表P A.rest
=new IntList(B.first,null); return P; }
    /**
     * 返回两个数组的连接,不改变原来数组的值(non-destructive)
     * * 
     */
    public static IntList catenate(IntList A, IntList B) {
    //新建一个C列表,用于做赋值的中间变量,用q列表指向列表的头部
//TODO: fill in method IntList C=new IntList(A.first,null); IntList q=C; while(A.rest!=null){ A=A.rest; C.rest=new IntList(A.first,null) ; C= C.rest; } C.rest=new IntList(B.first,null); C=C.rest; while(B.rest!=null){ B=B.rest; C.rest=new IntList(B.first,null); C=C.rest; } return q; }
原文地址:https://www.cnblogs.com/zyycumt/p/13274419.html