以递归方式将列表的元素与以下元素进行比较

compare an element of a list with the following in a recursively way

, 更新:感谢您的所有建议

假设这个练习就像一个画谜, 我有一个用 Cons 和 Nil 的概念制作的数字列表,

List l = new Cons(**3**, new Cons(**2**,new Cons(**1**, new
Cons(**4**, new Cons(**1**, new Nil())))));

我想递归地计算其中有多少紧跟一个较小的数字。

例如

[5,0,5,3].count() == 2, [5,5,0].count() == 1

count()方法是我自己做的(不能有任何参数),其他都是默认的,我不会做其他方法或使用已经定义了一个像 add(),size()... "NEXT" 必须在当前 elem 之后有下一个值,但我找不到解决方案。

欢迎任何解决方案。

abstract class List {

    public abstract boolean empty();

    public abstract int first();

    public abstract int count();

}

class Cons extends List {

    private int elem;

    private List next;

  public Cons(int elem, List next) {

   this.elem = elem;

   this.next = next;

}

public boolean empty(){
 return false; 
}

public int first(){
 return elem;
}

@Override
public int count() {
  if(elem>NEXT) {
      return 1 + next.count();  
  }else {
      return next.count();      
 }

}

```![enter image description here](https://i.stack.imgur.com/kWo0v.jpg)
public int NEXT(){
 if(next!=null)
   return next.first()
 else 
   throw new Exception("No next element")
}

以下代码将创建一个包含 N 个元素的递归列表,其中 N 值由找到的元素数量的大小定义在 int 数组中称为 elementsRecursiveList class 中。调用 startRecursion() 方法创建一个包含已定义元素的递归列表,并调用 count() 获取数组中 紧跟较小数字 [=55= 的元素数量].

主要Class

这是您的应用程序入口点:

public static void main(String[] args) {

    int count = RecursiveList.startRecursion().count();
    System.out.printf("List has %d recursive elements", count);
}

递归列表Class

abstract class RecursiveList {

    protected static int index = -1;
    protected static int[] elements = new int[]{ 5,2,1,4,3,2,6 };

    public static RecursiveList startRecursion() {
        return new Cons();
    }

    public abstract boolean empty();

    public abstract int count();

    public abstract Integer getElement();

    public static int incIndex() {
        return index += 1;
    }
}

缺点Class

public class Cons extends RecursiveList {

    private static int result;

    private final Integer elem;
    private final RecursiveList prev;
    private final RecursiveList next;

    private Cons(Cons parent) {

        prev = parent;
        elem = incIndex() < elements.length ? elements[index] : null;
        System.out.printf("Creating new Cons with element %d(%d)%n", elem, index);
        next = elem != null ? new Cons(this) : null;
    }

    Cons() {
        this(null);
    }

    public boolean empty() {
        return false;
    }

    @Override
    public /*@Nullable*/ Integer getElement() {
        return elem;
    }

    @Override
    public int count() {

        if (elem != null)
        {
            if (prev != null && elem < prev.getElement())
                result += 1;

            if (next != null) {
                return next.count();
            }
        }
        return result;
    }
}

编辑

好的,这就是您真正想要的答案。这完全符合对您提供的 this 练习施加的限制。该解决方案使用纯 Java,class 及其任何方法或字段声明均未以任何方式修改,也未添加此类新元素。我只在练习说你应该的地方添加了实现。

主要Class

public static void main(String[] args) {

    List l = new Cons(3, new Cons(2,new Cons(1, new
            Cons(4, new Cons(1, new Nil())))));

    assert l.count() == 3;

    l = new Cons(5, new Nil());
    assert l.count() == 0;

    l = new Cons(5, new Cons(5, new Cons(0, new Nil())));
    assert l.count() == 1;

    l = new Cons(5, new Cons(0, new Cons(5, new Cons(3, new Nil()))));
    assert l.count() == 2;

    System.out.println("All tests completed successfully!");
}

缺点Class

import java.util.NoSuchElementException;

public class Cons extends List {
    private int elem;
    private List next;

    public Cons(int elem, List next) {
        this.elem = elem;
        this.next = next;
    }

    public boolean empty()
    { return false; }

    public int first()
    { return elem; }

    public int count()
    {
        try {
            if (first() > next.first()) {
                return 1 + next.count();
            }
            else return next.count();
        }
        catch (NoSuchElementException e) {
            return 0;
        }
    }
}

无 Class

import java.util.NoSuchElementException;

public class Nil extends List {
    public boolean empty()
    { return true; }

    public int first()
    { throw new NoSuchElementException(); }

    public int count()
    {
        throw new IllegalAccessError();
    }
}