954,198 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Searching linked list

I'm having a problem with my search function for my linked list. It has to do with my foodItem object. I'm not sure what to replace it with since it's part of the function protocol. The previous version of this code used an array and part of my problem is correctly converting to a linked list format. I'm really stuck, so any advice would be much appreciated. I hope I didn't leave out anything helpful. :confused: --Sheila

/**
*searchByName:search for foods by name
*in:name
*out:foodItem
*return:true if there is a match, or else false
**/
bool FoodList::searchByName(char name[],Food& foodItem)const
{
int i;
size = foodItem.size;
 <a href="http://www.daniweb.com/techtalkforums/thread72745.html#">Node</a>  * current;
if(foodItem.head == NULL)
head = NULL;
else
{
for(current=head;current;current=current->next)
{
if(strcmp(head-> <a href="http://www.daniweb.com/techtalkforums/thread72745.html#">data</a> .name,name) == 0)
{
strcpy(head->data.name,foodItem.head->data.name);
head->data.category = foodItem.head->data.category;
head->data.calories = foodItem.head->data.calories;
head->data.carbohydrates = foodItem.head->data.carbohydrates;
head->data.fat = foodItem.head->data.fat;
head->data.cholesterol = foodItem.head->data.cholesterol;
head->data.sodium = foodItem.head->data.sodium;
head->data.protein = foodItem.head->data.protein;
return true;
}
else{
}
return false;
}
sbenware
Newbie Poster
16 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

> if(strcmp(head->data.name,name) == 0)
Hm, maybe you want to be comparing the name with the current node instead of 'head' each iteration?

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Do you mean like this?

if(strcmp(current-> <a href="http://www.daniweb.com/techtalkforums/post331325.html#">data</a> .name,name) == 0)
sbenware
Newbie Poster
16 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

>Do you mean like this?
Well yeah, if searching through the linked list is your intention...

Another problem with your code: head is never assigned anything. There's no way your code is going to work unless you somehow give it a value.

[edit] Nevermind, I see now that 'head' is a class member, not a local variable. [/edit]

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

How come on this line:
bool FoodList::searchByName(char name[],Food& foodItem)const

you used the [] after the name and not these ?()
Joe head is asigned to a NULL. Does NULL mean is a empty
paramiter?

nottoshabi
Light Poster
44 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

How come on this line: bool FoodList::searchByName(char name[],Food& foodItem)const

you used the [] after the name and not these ?()


That meansname is to be used as an array in the function.

Joe head is asigned to a NULL. Does NULL mean is a empty paramiter?


Yes.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

Thanks did not know that.

nottoshabi
Light Poster
44 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You