Thread: find entry in linked list

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    find entry in linked list

    I'm working on a function that finds entries in an address book given the linked list (first), the fieldId (i.e. a number form 1-7, such that 1 = First..name, 2 = Last..name, 3 = Street..address, 4 = City, 5 = State, 6 = Zip..code, 7 = Phone..number), and the searchKey string array that the user intends to find in the list.

    so my code looks something like this:

    Code:
    void find(struct Entry* first, int fieldId, char searchKey[101])
    {	
    	switch (fieldId)
    	{
    		case 1: {
    			while ( first != NULL ) {
    				if ( first->firstName == searchKey )
    					print_this_horseradish (first); 
    				first = first->next;
    			}
    			break;
    		}
    		case 2: {
    			// similar
    		}
    		case 3: {
    			// similar
    		}
    		case 4: {
    			// similar
    		}
    		case 5: {
    			// similar
    		}
    		case 6: {
    			// similar
    		}
    		case 7: {
    			// similar
    		}
    		default:	printf ("This cannot happen.");
    	}
    so, in that comparison something's not right. I get just the very first entry on the linked list printed out when the condition ( first->firstName == searchKey ) is satisfied, and only once.

    print_this_horseradish looks like this:

    Code:
    void print_this_horseradish (struct Entry* first)
    {
    	printf ("%s, %s\n", first->lastName, first->firstName);
    	printf ("%s\n", first->streetAddress);
    	printf ("%s, %s %s\n", first->city, first->state, first->zipCode);
    	printf ("%s\n", first->phoneNumber);
    	printf ("\n");
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To compare strings, you need strcmp. You cannot compare entire arrays or strings with ==.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    still doesn't fix it. maybe i need to declare a pointer that points to first and use that for the comparisons... ?

    maybe something like
    Code:
     
    
    const struct Entry *p1
    
    //. ... much codingz
    
    if (strmp (p1->next, p1) ... blah blah ??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void foo( struct node *list, char *find )
    {
        for( ; list; list = list->next )
        {
            if( strcmp( list->name, find ) == 0 )
            {
                printf( "Yay!\n" );
                return;
            }
        }
        printf( "Boo!\n" );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by fcommisso View Post
    still doesn't fix it. maybe i need to declare a pointer that points to first and use that for the comparisons... ?

    maybe something like
    Code:
    const struct Entry *p1
    
    //. ... much codingz
    
    if (strmp (p1->next, p1) ... blah blah ??
    What do you mean "still doesn't fix it"?!
    That's like being given a hammer and still not being able to hit in a nail. Hmmm..., are you even holding the right end?

    You should at least post some real code to demonstrate whether you understand how to use it or not. The fact that you've mistyped it here as "strmp" and aren't showing what comes after it, suggests you are in fact using it wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM