lluvia900125 2023-01-18 15:11 采纳率: 45%
浏览 47
已结题

java链表递归的另一种实现方法

这是一个linked list, 其中的一个method是给每个值+上一个x, 我理解递归思路,但是我想知道不用递归,iterative 的方式如何写。


public class IntList {
public int first;
public IntList rest;

public IntList(int f, IntList r) {
first = f;
rest = r;
}

/** Return the size of the list using... recursion! */
public int size() {
if (rest == null) {
return 1;
}
return 1 + this.rest.size();
}

/** Return the size of the list using no recursion! */
public int iterativeSize() {
IntList p = this;
int totalSize = 0;
while (p != null) {
totalSize += 1;
p = p.rest;
}
return totalSize;
}

/** Returns the ith value in this list.*/
public int get(int i) {

}

/** Returns an IntList identical to L, but with
* each element incremented by x. L is not allowed
* to change. */
/**我想知道这个incrList method不用递归,如何用iterative 的方式写出来 */
public static IntList incrList(IntList L, int x) {
if (L == null) {
return L;
}
IntList incr = new IntList(L.first + x, incrList(L.rest, x));
return incr; 
}

public static void main(String[] args) {
IntList L = new IntList(15, null);
L = new IntList(10, L);
L = new IntList(5, L);

System.out.println(L.iterativeSize());
}
} 

  • 写回答

5条回答 默认 最新

  • 游一游走一走 2023-01-18 15:30
    关注
    
    public class IntList {
        public int first;
        public IntList rest;
    
        public IntList(int f, IntList r) {
            first = f;
            rest = r;
        }
    
        /**
         * Return the size of the list using... recursion!
         */
        public int size() {
            if (rest == null) {
                return 1;
            }
            return 1 + this.rest.size();
        }
    
        /**
         * Return the size of the list using no recursion!
         */
        public int iterativeSize() {
            IntList p = this;
            int totalSize = 0;
            while (p != null) {
                totalSize += 1;
                p = p.rest;
            }
            return totalSize;
        }
    
    
    /**
     * Returns an IntList identical to L, but with
     * each element incremented by x. L is not allowed
     * to change.
     *
     */
        /**
         * 我想知道这个incrList method不用递归,如何用iterative 的方式写出来
         */
        public static IntList incrList(IntList L, int x) {
            if (L == null) {
                return L;
            }
            IntList incr = new IntList(L.first + x, incrList(L.rest, x));
            return incr;
        }
    
        public static IntList incrList2(IntList L, int x) {
            if (L == null) {
                return L;
            }
            IntList temp = L;
            IntList incr = new IntList(temp.first + x, null);
            IntList temp2 = incr;
            while (temp.rest != null) {
                temp = temp.rest;
                temp2.rest = new IntList(temp.first + x, null);
                temp2 = temp2.rest;
            }
            return incr;
        }
    
        @Override
        public String toString() {
            return "IntList{" +
                    "first=" + first +
                    ", rest=" + rest +
                    '}';
        }
    
        public static void main(String[] args) {
            IntList L = new IntList(15, null);
            L = new IntList(10, L);
            L = new IntList(5, L);
            System.out.println(L);
            System.out.println(IntList.incrList2(L, 1));
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 1月26日
  • 已采纳回答 1月18日
  • 创建了问题 1月18日

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同