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 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题