Function to search into a linked list

Hi all, I tried making a function that search a struct into a linked list and return and return the correct struct if founded.

PtrModel Search (PtrModel list, int val, PtrModel* previous)
            {
                  *previous = NULL;
                  while (list && list->modelName != val)
                  {
                        *previous= list;
                        list = list->next;
                  }
                  return list;
            }

However, it doesn't seems to work. If anyone knows what's wrong, any help would be appreciate. Also, if I wanted to get an int as the returned value, would I only have to change the return value ?

Thanks for your help.
Frank
The_Kingpin08Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IndrawatiCommented:
Assuming your linked list stores integers and they are stored in PtrModel.modelName, you can do this:

PtrModel* Search(PtrModel* first, int value)
{
      PtrModel* CurPtr = first;
      while(CurPtr)
      {
            if(CurPtr->modelName == value)
                  return CurPtr;
            CurPtr = CurPtr->next;
      }
      return 0;
}


0
kjayaramanCommented:
I am skeptical about the signature of the function

PtrModel Search (PtrModel list, int val, PtrModel* previous)

PtrModel list - This is the link list [Should have been PtrModel* list to give the pointer of the head]
int val - value you are searching for
PtrModel* previous - This is your structure that stores the previous structure's pointer. I dont see any reason to have this parameter.




0
IndrawatiCommented:
To clarify my answer above:
- first is the first element in the linked list, and val is the value to be searched.
- the above function will return 0 when val is not found in the linked list, otherwise it returns a pointer to the first PtrModel which holds val.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

manav_mathurCommented:
>Also, if I wanted to get an int as the returned value, would I only have to change the return value ?

Yes.
And going with the code that Indrawati has put down here,

return CurPtr;

would be modified as

return CurPtr->modelName;

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
manav_mathurCommented:
And also ur function prototype will change as

int Search(PtrModel* first, int value)
{
...
...


btw, is there any specific use which you want the previous pointer(in your original post) to put to??
Coz it doesnt make sense in a simple search algorithm.

Manav
0
PaulCaswellCommented:
So what DOES it do wrong? It looks ok to me.

Paul
0
Peter-DartonCommented:
I'd guess that the reason one might want to have the "previous" pointer returned is so one can then remove the element one has just found by setting previous->next to previous->next->next.

As others have said, however, the search function looks fine, assuming the definition of PtrModel is something like
typedef struct Model {
  struct Model *next;
  int modelName;
  ...
} *PtrModel;


I would suggest that the most likely source of your problems is not going to be the search function (which, after all, does not modify the data in any way), but your insert and remove functions.
Whilst linked-list insert and remove functions are trivial and thus easy to get right, they're also deceptively easy to get wrong as well, especially if the list is meant to be sorted.

Write your self a simple "print out the list" method, e.g.
void dumpList(PtrModel listHead)
{
  unsigned int index = 0;
  printf("Linked list starting at 0x%p =\n",listHead);
  while( listHead )
  {
    printf("  [%u] = model %d at 0x%p\n",index,listHead->modelName, listHead);
    /*if PtrModel contains other interesting data, print it as well */
    index++;
  }
}
and then call that on either side of your insert-element and remove-element methods so you can check that they're doing everything as expected.
In particular, call this "dump" method just before you call your search method - I bet it reveals that there's a problem with the linked list itself, not the search function...
0
The_Kingpin08Author Commented:
Thanks all :) Works great now

Frank
0
The_Kingpin08Author Commented:
Oups problem, when compiling I get

error C2040: 'Search' : 'PtrModel (PtrModel,int *)' differs in levels of indirection from 'int ()'

Anyone has a clue on the problem ? Thanks

Frank
0
manav_mathurCommented:
I think there's a problem with some copy constructor you have defined somewhere.

Can you copy paste the code and also the code wherein you are declaring objects for PtrModel class.


Manav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.