| | |
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.
--Sheila
--Sheila C Syntax (Toggle Plain Text)
/** *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 rel="nofollow" class="t" href="http://www.daniweb.com/techtalkforums/thread72745.html#" target="_blank">Node</a> * current; if(foodItem.head == NULL) head = NULL; else { for(current=head;current;current=current->next) { if(strcmp(head-><a rel="nofollow" class="t" href="http://www.daniweb.com/techtalkforums/thread72745.html#" target="_blank">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; }
Last edited by WaltP; Mar 19th, 2007 at 6:01 pm. Reason: Fixed CODE tags
>Do you mean like this?
Well yeah, if searching through the linked list is your intention...
Another problem with your code:
[edit] Nevermind, I see now that 'head' is a class member, not a local variable. [/edit]
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."
•
•
•
•
How come on this line:
bool FoodList::searchByName(char name[],Food& foodItem)const
you used the [] after the name and not these ?()
Yes.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- Quicksorting linked list - simple algorithm (C)
- Linked List (C)
- linked list library (C)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C Forum
- Previous Thread: Help with array bounds in C
- Next Thread: Getting Computername
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard kilometer km license linked linkedlist linux looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation owf pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






