Tutorial :Pointers and recursion



Question:

I am using a recursive function in a list (with ADT file) by modifying the search function and adding Printing function:

template <class TYPE, class KTYPE>   bool List<TYPE, KTYPE> :: _search (NODE<TYPE> **pPre,                                     NODE<TYPE> **pLoc,                                      KTYPE        key)  {     if ((*pLoc) == NULL)          return false;     else          if (key == (*pLoc)->data.key)              return true;                _search ((*pPre)->link ,(*pLoc)->link, key);                return false;  }    template <class TYPE, class KTYPE> bool List<TYPE, KTYPE> :: _search (NODE<TYPE> **pPre, NODE<TYPE> **pLoc, KTYPE key) { if ((*pLoc) == NULL) return false; else if (key == (*pLoc)->data.key) return true; _search ((*pPre)->link ,(*pLoc)->link, key); return false; }  

But I get an error in

_search ((*pPre)->link ,(*pLoc)->link, key);  

the error:

error C2664: 'List<TYPE,KTYPE>::_search' : cannot convert parameter 1 from 'NODE<TYPE> *' to 'NODE<TYPE> **'  

I don't Know why? I tried adding and taking away * but I still don't get it right.

and I initialize it :

NODE <TYPE>  * pPre;      pPre = NULL;      NODE <TYPE>  * pLoc ;      pLoc = NULL;    NODE <TYPE> * pPre; pPre = NULL; NODE <TYPE> * pLoc ; pLoc = NULL;  

before the function is called

this function returns the location of pPre and pLoc in memory, so the function signature must use **.

I know I must change the way I am calling it, but how?

template <class  TYPE>       struct NODE         {         TYPE    data;         NODE   *link;        };         struct Student         {         string name ;            int level;         int key;        };     template <class  TYPE, class KTYPE>       class List           {           private:             NODE<TYPE> *head;             NODE<TYPE> *pos;             NODE<TYPE> *rear;             int         count;                       ....                       ....                       ....                       ....                       ....    };    template <class TYPE> struct NODE { TYPE data; NODE *link; }; struct Student { string name ; int level; int key; }; template <class TYPE, class KTYPE> class List { private: NODE<TYPE> *head; NODE<TYPE> *pos; NODE<TYPE> *rear; int count; .... .... .... .... .... };       ostream & operator << (ostream  & out , Student & Data)  {      out << "The name is : " << Data.name << "\nThe Level is : " << Data.level<< "\nThe ID is :"<<Data.key;      return out;  }    template<class TYPE, class KTYPE>   void List <TYPE, KTYPE > :: ReversePrint ( NODE <TYPE> * node )  {  if ( node== NULL)            return ;    ReversePrint (node->link);    cout << node-> data;       return ;  }  


Solution:1

_search ((*pPre)->link ,(*pLoc)->link, key);  

The first parameter is of type pointer to NODE whereas it should be a pointer to a pointer to NODE.


Solution:2

_search ((*pPre)->link ,(*pLoc)->link, key);  

change the above to :

_search (&(*pPre)->link ,(*pLoc)->link, key); <== see & in first parameter  

It first parameter requires pointer to NODE*

EDIT: After checking your code after EDIT it looks like both 1st and 2nd the parameters passing to _search is not correct. The method requires pointer to pointer to NODE. Hence, the correct way of passing parameter is:

_search (&(*pPre)->link , &(*pLoc)->link, key);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »