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

时间:2019-05-29 19:51:12

标签: java list cons

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

假设这个练习就像是重击, 我有一个用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 (),尺寸()... “ NEXT”必须在当前元素之后具有下一个值,但我无法解决。

欢迎任何解决方案。

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)

2 个答案:

答案 0 :(得分:1)

以下代码将创建具有 N 个元素的递归列表,其中 N 值由在int数组中找到的元素数量的大小定义在elements类中称为RecursiveList。调用startRecursion()方法以创建具有已定义元素的递归列表,然后调用count()以获取数组中紧随其后的较低数字的元素数量。< / p>

主班

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

public static void main(String[] args) {

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

递归列表类

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;
    }
}

缺点类

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,该类或其任何方法或字段声明均未进行任何修改,并且未添加任何此类新元素。我只是在练习中您应该添加的地方添加了实现。

主班

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!");
}

缺点类

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;
        }
    }
}

无课

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();
    }
}

答案 1 :(得分:0)

public int NEXT(){
 if(next!=null)
   return next.first()
 else 
   throw new Exception("No next element")
}