searching a linked list for several atributes

This is a discussion on searching a linked list for several atributes within the C Programming forums, part of the General Programming Boards category; I need to provide functionality in a program that needs to search through a linked list of dinners. The problem ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    searching a linked list for several atributes

    I need to provide functionality in a program that needs to search through a linked list of dinners.

    The problem is I need to be able to search for every single attribute. So for the code
    Code:
    Restaurante* encontrarRestauranteString (char* string){
    	No* auxiliar = cabeca;
    	No* anterior = NULL;
    	int encontrou = F;
    	
    	for(; auxiliar != NULL && encontrou == F; auxiliar=auxiliar->proximo)
    		{
    			if(auxiliar->restaurante->localizacao.latitude == coord->latitude )
    			{
    				return auxiliar->restaurante;
    			}
    			else
    				anterior=auxiliar;
    		}
    		return NULL;
    }
    All I need to do is change the attribute of restaurante for every single attribute. Do I need to do this manually or can I somehow tell it in the incoming variables which thing I'm interested in so it would look something like
    Code:
    Restaurante* encontrarRestauranteString (?? attribute, char* value_of_attribute){
    	No* auxiliar = cabeca;
    	No* anterior = NULL;
    	int encontrou = F;
    	
    	for(; auxiliar != NULL && encontrou == F; auxiliar=auxiliar->proximo)
    		{
    			if(auxiliar->restaurante->atribute ==  value_of_attribute)
    			{
    				return auxiliar->restaurante;
    			}
    			else
    				anterior=auxiliar;
    		}
    		
    		return NULL;
    }
    Don't know if I was able to explain my question properly. Please tell me if I didn't.
    Thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could probably write a function that "compare an item with a template item", where the template item has a "special value" for items that are "don't care".

    For example:
    Code:
    struct item
    {
        int lat, lon;
        char descr;
        double price;
    };
    
    
    // Return 0 for match, non-zero for non-match
    // tempItem is a template to match. 
    // In tempItem, a value of -1 for integer and float values means "don't care"
    // whilst a NULL for strings means don't care. 
    int match_items(const struct item *anItem, const struct item *tempItem)
    {
        if (tempItem->lat != -1 && tempItem->lat != anItem->lat)
           return 1;
        if (tempItem->lon != -1 && tempItem->lon != anItem->lon)
           return 1;
        if (tempItem->descr != NULL && strcmp(tempItem->descr, anItem->descr))
           return 1;
        if (tempItem->price != -1.0 && fabs(tempItem->price - anItem->price) > 0.01)
           return 1;
        // If all match, return 0 to indicate a match.
        return 0;
    }
    Note that we avoid direct comparison of floating point except for the -1.0 constant, since direct comparisons of float is possibly not matching for certain values, if one is produced through calculation and another isn't.

    [You may also want to have a "delta" in other comparisons, so that for example a latitude/longitude comparison allows a certain small difference between the target and the actual value]

    There are possibly ways that you could use offsetof() and macros to do something else, but it's a lot more advanced, and would not be particularly easy to explain or write. And you still essentially need to know what each field is in the structure, so I don't think it's a better solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Great idea!

    I thought that I needed some kind of feature of C that I didn't know about but that's pretty straight forward and much much better.

    Thanks lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21