尝试在链表中使用replace方法,但程序只替换了第一个元素。

huangapple go评论123阅读模式
英文:

trying to do a replace method in linked list but program replaces only the first element

问题

以下是翻译好的代码部分:

public class ListOfNVersion03PartA
{   
    private int thisNumber;              // 存储在此节点中的数字
    private ListOfNVersion03PartA next;  // 形成对象的链表
    private int []list;
    private final int nodeID;            // 列表中每个对象的唯一标识符
    
    private static int nodeCount = 0;    // 已创建的列表对象数目
    
    /**
     * @param  num   要存储在此对象中的值
     */
    public ListOfNVersion03PartA(int num)
    {
        thisNumber = num;
        next = null;

        ++nodeCount;
        nodeID = nodeCount;
    }

    public ListOfNVersion03PartA(int [] num)
    {
        this(num[0]);  // 在这种情况下,“this”调用另一个构造函数
        list = new int[num.length];
        for (int i = 1; i < num.length; ++i)
        {
            insertLast(num[i]);
        }
    }

    public int replaceAll(int replaceThis, int withThis)
    {
        int count = 0;
        int x = 0;

        for (int i = 0; i < nodeCount; ++i)
        {
            if (thisNumber == replaceThis)
            {
                thisNumber = x;
                x = withThis;
                ++count;
            }
        }

        return count;
    }
}

请注意,这是您提供的代码的翻译版本。如果您有其他需要帮助的地方,请随时提问。

英文:

My current code only replaces the first element of the linked list. I am trying to create a replace method with two parameters but failing. My current logic shows that I am trying to replace the element with my new input but I am assuming I am failing to traverse through the list as only the first one gets replaced.

public class ListOfNVersion03PartA
{   
    private int thisNumber;              // the number stored in this node
    private ListOfNVersion03PartA next;  // forms a linked list of objects
    private int []list;
    private final int nodeID;            // a unique ID for each object in the list

    private static int nodeCount = 0;    // the number of list objects that have been created

    /**
     * @param  num   the value to be stored in this object
     */
    public ListOfNVersion03PartA(int num)
    {
        thisNumber = num;
        next = null;

        ++nodeCount;
        nodeID = nodeCount;

    }



 public ListOfNVersion03PartA(int [] num)
{
	
    this(num[0]);  // in this context, &quot;this&quot; invokes the other constructor    
    list = new int[num.length];
    for (int i=1 ; i&lt;num.length ; ++i)
    	{
        insertLast(num[i]);
    	}
}

 public int replaceAll(int replaceThis, int withThis)
        {
        	int count = 0; 
            int x = 0;
            
        
        for ( int i=0 ; i &lt; nodeCount; ++i) 
        {
            if ( thisNumber == replaceThis )
        {
        	thisNumber = x;
        	x = withThis;
        	++count;
        	
        }
        }
        
        return count;
    }

答案1

得分: 0

一旦你评估了当前节点(也称为this),然后你应该执行对下一个节点的评估,直到没有更多的节点需要评估。以下是使用你当前代码的示例:

public int replaceAll(int replaceThis, int withThis) {
    int result = 0;
    if (thisNumber == replaceThis) {
        thisNumber = withThis;
        result = 1;
    }
    if (next != null) {
        result += next.replaceAll(replaceThis, withThis);
    }
    return result;
}
英文:

Once you've evaluated the current node (aka this), then you should perform the evaluation of the next node until there are no more nodes to evaluate. Here's an example using your current code:

public int replaceAll(int replaceThis, int withThis) {
    int result = 0;
    if (thisNumer == replaceThis) {
        thisNumber = withThis;
        result = 1;
    }
    if (next != null) {
        result += next.replaceAll(replaceThis, withThis);
    }
    return result;
}

huangapple
  • 本文由 发表于 2020年5月29日 23:58:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/62090002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定