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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog