find values in the concatenated list by a recursive function

Asked 3 months ago, Updated 3 months ago, 6 views

I would like to find the values in the templateed concatenation list using the recursive function (recursive method).What I'm worried about is the temporary argument of the re-function.A common solution is to pass the head of the concatenated list in the call of the function of the restart.However, what I want to do this time is to give only the value I want to find when I call a function outside that function.The following code will cause an error in the findValue function.Here's why:Invalid use of non-static data member 'head'
In other words, the head is a non-static member, so it cannot be used as a temporary argument.

Since it is templateized, I would like to find out if there is a value for various data types, and return true, if not false.I thought I could do it if I could set the currentLink as head, a temporary argument for findValue, but I got an error saying that I couldn't do this.
If you are familiar with this area, please take care of me.

Note: The professor gave me a hint.By creating a new method in the private of the list class and calling it within the findValue provisional argument, currentLink=head is set, and it seems that this re-function works.In other words, I think it will work if we can create a method called within this findValue temporary argument.If you understand, please take care of me.

List.h

template<typename T>
class Link
{
private:
    T value;
    Link*next;
public:
    // constructor
    Link(T value, Link*next=nullptr): value(value), next(next){}
    // destructor
    virtual~Link(){}
    // return value
    T getValue() {return this ->value;}
    // return next
    Link*getNext() {return this ->next;}
    // set next
    void setNext(Link*next) {this->next=next;}
};

template<typename T>
class list
{
private:
    Link <T>*head;
public:
    List():head(nullptr){}
    // virtual to List();
    void addHead(T value)
    {
        Link <T>*newLink;
        newLink = newLink<T>(value);
        newLink ->setNext(head);
        head = newLink;
    }

  

    T getHead()
    {
       
        return head ->getValue();
    }

    pool findValue (T value, Link<T>*currentLink=head)
    {
        pool find = false;

        if(currentLink==nullptr)
        {
            return false;
        }
        else if (currentLink ->getValue()==value)
        {
            find = true;
        }
        else
        {
            find = findValue(value, currentLink->getNext());
        }
        return find;
    }

};

main.cpp

# include <iostream>
# include "List.h"

using namespace std;

int main() {

    int size = 5;
    List <int > numbers;
    for (inti=0; i<size;i++)
    {
        numbers.addHead(i);
    }

    if(number.findValue(2)==true)
    {
        cout<<"find"<<endl;
    }
    else
    {
        cout<<"not found"<<endl;
    }
    return 0;
}

c++

2022-09-30 13:57

2 Answers

We have found a solution.What I was trying to do seemed a little off the mark.

Define recFindValue in private, similar to findValue above.The recFindValue provisional argument sets the value to look for, the value and the link to find the value, and the currentLink.The default value of the argument is mentioned in the comment, but the default value of the argument is not used.The others are the same as the findValue above.

What the actual findValue does is just take the value you look for and pass it to recFindValue, plus the head at the beginning of the consolidated list.That is, call recFindValue.
Then return the result returned from recFindValue

In other words, this is the code.Only the periphery of the unusual part will be displayed here.

List.h

template<typename T>
class list
{
private:
    Link <T>*head;

    bool recFindValue (T value, Link<T>*currentLink)
    {
        if(currentLink==nullptr)
        {
            return false;
        }
        else if (currentLink ->getValue()==value)
        {
            return true;
        }
        else
        {
            return recFindValue(value, currentLink->getNext());
        }

    }
    
public:
    List():head(nullptr){}

    bool findValue (T value)
    {
        return recFindValue(value,head);
    }
}


2022-09-30 13:57

The hint you gave me is a bit subtle if I were to think of recursion as the subject.

In terms of tips, C++ has the following features that allow you to create functions with the same name with different parameters:
Function overload
How to specify a member variable as the default argument

It's different from recursive, but it's calling a function with the same name.

bool findValue(T value)//Define pointerless function for start node
{
    Return findValue(value,head); // head as the start node and call the function below
}

bool findValue (T value, Link<T>*currentLink) // Do not use default arguments
{
    if(currentLink==nullptr)
    {
        return false;
    }
    else if (currentLink ->getValue()==value)
    {
        return true;
    }
    return findValue(value, currentLink->getNext());
}

This article will be helpful if you want to discuss the subject of recursive.
I want to set the default value to the pointer of the structure in the argument
Understanding the Default Arguments for the C++ Class

If the default argument is set to a value that is not valid, then use the initial value that you want to use.

bool findValue(T value, Link<T>*currentLink=nullptr) // Default argument is nullptr
{
    If (currentLink==nullptr) // Action if parameter is nullptr
    {
        If(head!=nullptr)// If head is not nullptr, set start node to head
        {
            currentLink = head;
        }
        If else//head is nullptr, the element is empty, so false
        {
            return false;
        }
    }

    if (currentLink ->getValue()==value)
    {
        return true;
    }
    else if(currentLink->getNext()==nullptr)// If next is nullptr, exit as there is no next node
    {
        return false;
    }
    return findValue(value, currentLink->getNext();// Recursive
}


2022-09-30 13:57

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.