Searching linked list

Reply

Join Date: Nov 2006
Posts: 16
Reputation: sbenware is an unknown quantity at this point 
Solved Threads: 0
sbenware's Avatar
sbenware sbenware is offline Offline
Newbie Poster

Searching linked list

 
0
  #1
Mar 19th, 2007
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" class="t" 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" class="t" 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Searching linked list

 
0
  #2
Mar 19th, 2007
>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?
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 16
Reputation: sbenware is an unknown quantity at this point 
Solved Threads: 0
sbenware's Avatar
sbenware sbenware is offline Offline
Newbie Poster

Re: Searching linked list

 
0
  #3
Mar 19th, 2007
Do you mean like this?

if(strcmp(current->data.name,name) == 0)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Searching linked list

 
0
  #4
Mar 19th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 44
Reputation: nottoshabi is an unknown quantity at this point 
Solved Threads: 1
nottoshabi's Avatar
nottoshabi nottoshabi is offline Offline
Light Poster

Re: Searching linked list

 
0
  #5
Mar 20th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Searching linked list

 
0
  #6
Mar 20th, 2007
Originally Posted by nottoshabi View Post
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.

Originally Posted by nottoshabi View Post
Joe head is asigned to a NULL. Does NULL mean is a empty paramiter?
Yes.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 44
Reputation: nottoshabi is an unknown quantity at this point 
Solved Threads: 1
nottoshabi's Avatar
nottoshabi nottoshabi is offline Offline
Light Poster

Re: Searching linked list

 
0
  #7
Mar 20th, 2007
Thanks did not know that.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC