Hi all,
How do you find a node that contain a certain info and then return only one member of the structure? Thanks
Hi all,
How do you find a node that contain a certain info and then return only one member of the structure? Thanks
Walk down the list until you find the node with the stuff you want in it, and return the member that you need...
-Govtcheez
[email protected]
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
This assumes that the item in the structure is an int, but it can be anything you want as long as you change the comparison accordingly. Start at the root of the list and traverse through the next pointers until the item of the iterator node and the node being searched for match. If the node to be searched for is not in the list, stop at the end and return a NOT_FOUND macro. I defined it like so:Code:int searchList ( struct node *list, struct node *find ) { struct node *iter; for ( iter = list; iter != NULL; iter = iter->next ) if ( iter->item == find->item ) return iter->item; return NOT_FOUND; }
Also make sure that the value for NOT_FOUND is an invalid value for a node in the list or you won't know whether or not you actually found the node.Code:#define NOT_FOUND -1
-Prelude
My best code is written with the delete key.