我需要创建一个方法,该方法遍历链表中的所有节点,并替换与SEARCH匹配的项,并用REPLACE替换它们。当我试图"替换它"时,我的代码什么都不做。。。我真的很感激在我已经拥有的代码上得到一些帮助。请记住,Node类已经创建
public void replace(String search, String replace)
{
Node current = first;
if (size == 0)
return;
else
{
if (current.item.equals(search))
{
current.item = replace;
current = current.next;
}
replace(search, replace); //Recursive call here
}
}
如果您真的想用递归实现它,您必须将当前节点传递给递归方法。
public void replace(String search, String replace) {
replaceRecur(first, search, replace);
}
private void replaceRecur(Node cur, String search, String replace) {
if (cur == null)
return;
if (cur.item.equals(search))
cur.item = replace;
replaceRecur(cur.next, search, replace);
}