944,039 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1634
  • C RSS
Mar 19th, 2007
0

Searching linked list

Expand Post »
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. --Sheila

  1. /**
  2. *searchByName:search for foods by name
  3. *in:name
  4. *out:foodItem
  5. *return:true if there is a match, or else false
  6. **/
  7. bool FoodList::searchByName(char name[],Food& foodItem)const
  8. {
  9. int i;
  10. size = foodItem.size;
  11. <a rel="nofollow" href="http://www.daniweb.com/techtalkforums/thread72745.html#" target="_blank">Node</a> * current;
  12. if(foodItem.head == NULL)
  13. head = NULL;
  14. else
  15. {
  16. for(current=head;current;current=current->next)
  17. {
  18. if(strcmp(head-><a rel="nofollow" href="http://www.daniweb.com/techtalkforums/thread72745.html#" target="_blank">data</a>.name,name) == 0)
  19. {
  20. strcpy(head->data.name,foodItem.head->data.name);
  21. head->data.category = foodItem.head->data.category;
  22. head->data.calories = foodItem.head->data.calories;
  23. head->data.carbohydrates = foodItem.head->data.carbohydrates;
  24. head->data.fat = foodItem.head->data.fat;
  25. head->data.cholesterol = foodItem.head->data.cholesterol;
  26. head->data.sodium = foodItem.head->data.sodium;
  27. head->data.protein = foodItem.head->data.protein;
  28. return true;
  29. }
  30. else{
  31. }
  32. return false;
  33. }
Last edited by WaltP; Mar 19th, 2007 at 6:01 pm. Reason: Fixed CODE tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbenware is offline Offline
16 posts
since Nov 2006
Mar 19th, 2007
0

Re: Searching linked list

>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?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 19th, 2007
0

Re: Searching linked list

Do you mean like this?

if(strcmp(current->data.name,name) == 0)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbenware is offline Offline
16 posts
since Nov 2006
Mar 19th, 2007
0

Re: Searching linked list

>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]
Last edited by John A; Mar 19th, 2007 at 11:00 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 20th, 2007
0

Re: Searching linked list

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?
Reputation Points: 10
Solved Threads: 1
Light Poster
nottoshabi is offline Offline
44 posts
since Mar 2007
Mar 20th, 2007
0

Re: Searching linked list

Click to Expand / Collapse  Quote originally posted by nottoshabi ...
How come on this line:
bool FoodList::searchByName(char name[],Food& foodItem)const

you used the [] after the name and not these ?()
That means name is to be used as an array in the function.

Click to Expand / Collapse  Quote originally posted by nottoshabi ...
Joe head is asigned to a NULL. Does NULL mean is a empty paramiter?
Yes.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Mar 20th, 2007
0

Re: Searching linked list

Thanks did not know that.
Reputation Points: 10
Solved Threads: 1
Light Poster
nottoshabi is offline Offline
44 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with array bounds in C
Next Thread in C Forum Timeline: Getting Computername





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC