[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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
0
The_Kingpin08
Asked:
The_Kingpin08
  • 3
  • 2
  • 2
  • +3
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now