如何用Node实现方法?

时间:2019-06-01 13:52:03

标签: java oop

我正在Node上使用Java,但代码有问题: 我必须实现MyList的“修改”方法,该方法会将列表前面的元素的总和添加到列表的每个元素。它是代码:

class Node {
  private int elem;
  private Node next;

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

  public int getElem()
     { return elem; }

  public Node getNext()
    { return next; }

   public void setElem(int elem)
     { this.elem = elem; }

   public void setNext(Node next)
     { this.next = next; }
 }

// MyList.java
 class MyList {
  private Node first;

  public MyList()
     { this.first = null; }

  public void insert(int elem)
     { first = new Node(elem, first); }


  public String toString()
    {
       String res = "";
      for (Node p = first; p != null; p = p.getNext()) {
        res += p.getElem();
        if (p.getNext() != null) res += ", ";
      }
    return res;
    }

  public void modifica(){
   //it is the code i've to create
  }

}

0 个答案:

没有答案