【问题标题】:Linked Lists Java Replacing Nodes链表 Java 替换节点
【发布时间】:2013-02-10 23:47:51
【问题描述】:

我在使用链接列表程序时遇到了困难。我想编写一个方法,用列表尾部值的总和破坏性地替换每个节点 n 中的值。所以如果列表是2,3,5,7;我想将其更改为 17、15、12、7。我得到了一个程序,我必须在其中添加一个执行此操作的方法。我可以改变第一个数字,但我不能改变其他三个,我被卡住了。如果有人可以帮助我,那就太好了。

原创节目

public class IntList {
private int value;     
private IntList next;


public IntList(int v, IntList n) {          // Constructor
    value = v;
    next = n;
  }

public int getValue() { return value; }       // Getters
public IntList getNext() { return next; }
public void setValue(int v) { value = v; }    // Setters
public void setNext(IntList n) { next = n; }

// Find the last node of a linked list.
public IntList findLast() {
   if (getNext() == null) return this;
   else return getNext().findLast();
 }

// Add a new node with value v at the end of l;

public void addEnd(int v) {
    findLast().setNext(new IntList(v,null));
  }

// Add up the values in a list, recurring down the owner
public int sumList() {
   if (getNext() == null) return getValue();
   else return getValue() + getNext().sumList();
  }


// Convert list of int to string

// Recursive method for constructing the end of the string, after the
// initial open bracket.

 public String toString1() {
   if (getNext() == null)
      return getValue() + "]";
   else return getValue() + ", " + getNext().toString1();
 }

// Top level rountine that starts with the "[" and then calls toString1 to
// do the rest.

  public String toString() {
    return "[" + toString1();
  }

// Recursive method for finding the sum of a list, using recursion down
// an argument. Note that this is a static method, associated with the class
// not with an object owner.

 static public int sumListArg(IntList l) {
    if (l==null) return 0;
    else return l.getValue() + sumListArg(l.getNext());
   }

 static public void main(String[] args) {
   IntList l = new IntList(2,null);
   l.addEnd(3);
   l.addEnd(5);
   l.addEnd(7);
   System.out.println("h");
   System.out.println(l.toString());
   System.out.println("Sum = " + l.sumList());
} // end main
} // end RecursiveIntList    

这是我到目前为止的方法(我认为这在逻辑上是可以的,但它是不正确的):

 public static void runningSum(IntList l)
{ 
     l.setValue(l.sumList());

    while(l.getNext() != null) 
     {
        l.setNext(l.getNext()); //Set Next to be the next reference
        l.getValue();  //Get the Next's value
        l.setValue(l.sumList()); //Add the rest of the numbers together
     }

     if(l.getNext() == null)
     {
         l.setValue(l.getValue());
     }

     System.out.println(l.toString());
}

【问题讨论】:

    标签: java linked-list


    【解决方案1】:

    有一个漂亮而优雅的解决方案:

    public int sumList() {
        if (getNext() == null) {
            return getValue();
        }
        value = getValue() + getNext().sumList();
        return value;
     }
    

    在这个方法中递归遍历List,总结当前元素后面的所有元素,同时设置值

    【讨论】:

    • 我相信他不能真正改变数据结构。
    • 我无法将结构更改为双链表。不过感谢您的帮助。
    • 递归解法怎么样?
    • 这可行,但它必须是一个迭代解决方案。感谢您的帮助。
    【解决方案2】:

    这是代码:

    public static void runningSum(IntList l)
    { 
        IntList head = l;
        int rSum = l.sumList();
    
        while(l != null) 
        {
            int curRS = rSum;
            curRS -= l.getValue();
            l.setValue(rSum);
            rSum = curRS;
            l = l.getNext();
        }
    
        System.out.println(head.toString());
    }
    

    我将把它分成几个部分来解释发生了什么。我们想要编写一个程序,该程序以列表的开头并以您描述的方式更改列表;基本上第一个元素必须成为所有原始元素的总和;第二个元素必须是除第一个元素之外的所有元素的总和,依此类推。最后一个元素,尾部,必须保持不变。

    public static void runningSum(IntList l)
    {
    

    我们需要记住传递给函数的头部的函数;我们将 l 保存在一个名为 head 的变量中。

        IntList head = l;
    

    第一个元素的运行总和是所有元素的总和;所以我们调用 sumList 并将结果存储在一个名为 rSum 的变量中。

        int rSum = l.sumList();
    

    这是数据结构编程中非常典型的习语;当元素不为空时,你循环。

        while(l != null) 
        {
    

    下一个元素的运行总和是rSum减去当前元素的值。

            int nextRS = rSum - l.getValue();
    

    现在我们可以将当前元素的运行总和设置为rSum

            l.setValue(rSum);
    

    对于下一次迭代,当前运行总和为 nextRS。最后我们更新 l 以指向下一个元素。

            rSum = nextRS;
            l = l.getNext();
        }
    

    如果我们不跟踪 head,现在我们将不知道要打印什么。

        System.out.println(head.toString());
    }
    

    【讨论】:

    • 哇!你能代替我的教授吗?哈哈。非常感谢,我现在完全明白了。
    • 谢谢,我试着按照你的建议在开头调用 sumList 来做到这一点。教学上我认为这是一个很好的例子,但 Code-Guru 的答案是最有效和最紧凑的,它也有你相同的原型(void 函数)。我最初倾向于像他一样的东西,但它是一个 int 函数,所以我放弃了它。如果你想接受他的回答,我可以接受。
    【解决方案3】:

    链表适合递归。递归解决方案可能如下所示:

    public static void runningSum(IntList l) {
        if (l.getNext() != null) {
            runningSum(l.getNext());
            l.setValue(l.getValue() + l.getNext().getValue());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2017-12-07
      相关资源
      最近更新 更多