C++ 在链表中查找值的递归方法 模板 int StringList::find(类型值) { 整数计数=0; //链表的开始 节点*电流=头部; //遍历列表直到结束(NULL) while(当前!=NULL) { //如果发现,增加计数器 如果(当前->数据==值) { 计数++; } //如果不是,则移动到下一个节点 当前=当前->下一步; } cout

C++ 在链表中查找值的递归方法 模板 int StringList::find(类型值) { 整数计数=0; //链表的开始 节点*电流=头部; //遍历列表直到结束(NULL) while(当前!=NULL) { //如果发现,增加计数器 如果(当前->数据==值) { 计数++; } //如果不是,则移动到下一个节点 当前=当前->下一步; } cout,c++,recursion,linked-list,find,singly-linked-list,C++,Recursion,Linked List,Find,Singly Linked List,为了使用递归,您需要更改find函数的签名(或添加具有不同签名的新函数),以将节点指针作为参数: template<class Type> int StringList<Type>::find(Type value) { int count = 0; // Start of linked list Node<Type> *current = head; // Traverse list until end (NULL) while (current !=

为了使用递归,您需要更改find函数的签名(或添加具有不同签名的新函数),以将节点指针作为参数:

template<class Type>
int StringList<Type>::find(Type value)
{
int count = 0;

// Start of linked list
Node<Type> *current = head;

// Traverse list until end (NULL)
while (current != NULL)
{
    // Increase counter if found
    if (current->data == value)
    {
        count++;
    }

    // If not, move to the next node
    current = current->next;
}

cout << value << " was found " << count << " times" << endl;

return 0;





// same function but using Recursive method





// Start of linked list
Node<Type> *current = head;
int count = 0;

// Thinking this is the base case, since its similar to the while loop
if (current == NULL)
{
    return 0;
}

// same as the while loop, finding the value increase the count, or in this case just prints to console
if ((current->data == value))
{
    cout << "Found match" << endl;
    return 0;
}
else
{   // If it didnt find a match, move the list forward and call the function again
    current = current->next;
    return find(value);
}

}
模板
int StringList::find(类型值,节点*where)
{
如果(其中!=nullptr)
{
//做事
}
}
然后,遍历列表时,将
where->next
传递给函数。一旦到达列表末尾,使用
nullptr
值,堆栈将展开

递归的一个关键方面是,所使用的函数或方法只需处理容器中的单个节点。然后,它使用要处理的下一个节点调用自己,直到没有更多节点为止。为了实现这一点,该函数需要将节点作为参数处理,这就是当前代码遇到问题的地方


请记住,递归的优雅和简单并不是免费的。方法对自身的每次调用都会消耗堆栈,因此,如果进程的堆栈耗尽,足够大的容器可能会导致崩溃。

对于初学者,与其使用返回类型
int
,不如使用无符号类型,例如
大小\u t

您可以使用以下方法。定义两个方法。第一个是公共非静态方法
find
定义如下

template<class Type>
int StringList<Type>::find(Type value, Node<Type> *where)
{
    if (where != nullptr)
    {
    // Do things
    }
}
template<class Type>
size_t StringList<Type>::find( const Type &value ) const
{
    return find( head, value );
}
模板
大小\u t字符串列表::查找(常量类型和值)常量
{
返回查找(头、值);
}
第二个是一个私有静态方法,有两个定义如下的参数

template<class Type>
int StringList<Type>::find(Type value, Node<Type> *where)
{
    if (where != nullptr)
    {
    // Do things
    }
}
template<class Type>
size_t StringList<Type>::find( const Type &value ) const
{
    return find( head, value );
}
模板
静态大小\u t StringList::查找(节点*当前、常量类型和值)
{
返回当前==nullptr?0:(当前->数据==value)+查找(当前->下一步,值);
}
如何将使用while循环的第一个方法转换为 做同样的事情但使用递归的东西

下面将更接近您想要的内容。您真的应该提供一个[MCVE]…缺少它会迫使您对代码进行许多猜测和假设

template<class Type>
static size_t StringList<Type>::find( Node<Type> *current, const Type &value )
{
    return current == nullptr ? 0 : ( current->data == value ) + find( current->next, value );
}
//看起来StringList是一个类(我忽略了模板问题),
//而且,您的类似乎拥有诸如head之类的“锚”
//StringList可能是公共接口。
//   
//要查找并计算targetValue,代码将启动
//在头节点,并通过节点列表递归。
//我会将下面的方法作为一个公共方法。
//
int StringList::findAndCountTargetValue(int targetValue)
{
int-retVal=0;
如果(nullptr!=head)//存在要搜索的元素?
retVal=head->countTV(targetValue);//递归节点
//否则就不可能有对手
返回(retVal);
}
//访问列表中的每个节点
int节点::countTV(const int targetValue)
{
int retVal=0;//初始化计数
if(data!=targetValue)//不匹配
{
if(nullptr!=next)//更多元素?
retVal+=next->countTV()//继续递归计数
}
其他的
{

std::你能做些什么吗?是的,向下滚动看起来你已经完成了大部分工作,但我认为你不想在找到匹配项时立即返回。相反,继续执行直到null,并让每个迭代返回到目前为止找到的数字。嗯,听起来很好。有没有一种方法可以做到这一点?LearningCode你可以定义静态方法并使用它,尽管我在回答中提到的定义两个方法更为自然。好吧,我明白你的意思,但我应该在哪一部分再次调用该函数?@LearningCode如果你更改方法的函数签名以包含指向节点的指针,你可以直接使用Vlad回答中描述的函数。他的second方法,公开,起到了作用。