How to make a two-way list with only the number of elements, Java.



  • There's a DDS DoubleNode, which is a two-way list:

    public class DoubleNode {
    public int value;
    public DoubleNode prev;
    public DoubleNode next;
    

    public DoubleNode(int value, DoubleNode prev, DoubleNode next) {
    this.value = value;
    this.prev = prev;
    this.next = next;
    }

    But what I'm trying to do is build a number like this, knowing only the number of elements in it (the meanings of the elements are randomly chosen). But I want to build a sensual approach, so I'm gonna put it this way:

        public static DoubleNode doubleNodeGenRec (int length) {
    return (length == 0) ? null : new DoubleNode((int)(Math.random()*10), null, doubleNodeGenRnd(--length));
    }

    to refer the reference to DoubleNode prev;?

    Turns out that Node is built on on the same side, because the method is simply null, and as a result, it's just a tied list.



  • public DoubleNode(int value) {
      this.value = value;
    }
    
    public static DoubleNode doubleNodeGen(int length) {
      return doubleNodeGenRec(length, null);
    }
    
    private static DoubleNode doubleNodeGenRec(int length, DoubleNode prev) {
      if (length == 0) {
        return null;
      }
    
      int value = (int) (Math.random() * 10);
      DoubleNode node = new DoubleNode(value);
      node.prev = prev;
      node.next = doubleNodeGenRec(length - 1, node);
      return node;
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2